代码之家  ›  专栏  ›  技术社区  ›  AutoTester213

在BeforeTestRun Hook-Specflow中创建具有不同变量的多个用户的最佳方法

  •  0
  • AutoTester213  · 技术社区  · 4 年前

    目前我正在 users admin 位置。

    @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);
           ...
                
    
       }
    

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

    此外,更多的位置将被添加在未来,因此我期待着通过表变量作为一个参数到BeforeTestRun挂钩,我还没有成功到目前为止,不知道是否可能与specflow做,而且我相信可能有一个更好的解决方案,我想避免重复,因为我可以。

    任何想法/帮助都将不胜感激。

    0 回复  |  直到 4 年前
        1
  •  1
  •   diabolist    4 年前
    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