在BeforeTestRun挂钩-Specflow

时间:2020-08-26 14:34:57

标签: c# cucumber specflow

嗨,我有一个问题,

当前,我正在users位置下创建一个用户,并且我也想在admin位置下创建一个用户。

我的Specflow方案如下。

@createUser
Scenario Outline: Do Something to created users
    Given A user has already been created under <location>
    When I do something with the user
    Then I expected something to happen
Examples:
| location |
| admin    |
| users    |

我的BeforeTestRun看起来像这样:

[BeforeTestRun()]
   public static void BeforeTestRun(string location // I would like to pass this 
variable from the scenario examples table)
   {
       CreateEmployeeForTestRun(location);
       ...
            

   }

我只希望在执行开始时创建一次用户,然后在结束时清理它们。以上场景只是我的复杂用户的简化版本。

我还有多种情况,将在不同的位置使用这2个不同的用户。

将来还会添加更多位置,因此我希望将表变量作为参数传递到BeforeTestRun挂钩中,到目前为止我还没有成功,并且不确定是否可以与specflow关联,我我确信可能会有更好的解决方案,我想尽可能避免重复。

任何想法/帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

Given there is an admin user
And there is a user

是一个很好的起点。如果您可以更精确地了解非管理员用户,例如

Given there is an admin user
And there is a customer

您应该执行这些步骤,例如

Given 'there is an admin' do
  @admin = create_admin
end

Given 'there is a customer' do
  @customer = create_customer
end

并推迟如何将用户创建为辅助方法。

一旦拥有就可以创建

Given there is an admin and a customer

并实现为

Given 'there is an admin and a customer' do
  @admin, @customer = create_admin, create_customer
end

您的助手方法将类似于

module UserCreationStepHelper
  def create_admin
    ...

  def create_customer
    ...
end
World UserCreationStepHelper

腔室:上方是红宝石黄瓜,您需要翻译。您应该可以在黄瓜.io上获得帮助。