首页 文章

如何在不使用Cucumber中的轮廓的情况下多次运行相同的场景?

提问于
浏览
0

考虑以下黄瓜情景:

Scenario Outline: Execute a template
 Given I have a <template>
 when I execute the template
 Then the result should be successful.
 Examples:
   | template  |
   | templateA |  
   | templateB |

因此,这将使用表中提到的值在场景大纲之上运行 . 但是这需要我知道我需要提前执行的所有模板并将它们填入表中 . 有没有办法动态加载模板列表,然后为每个模板执行方案?

这种方法的优点是,每当我添加新模板时,我都不需要更新功能测试 .

谢谢

1 回答

  • 1

    是的,将模板列表放在步骤定义中,甚至在应用程序中更好 . 然后你就可以写了

    Scenario: Execute all templates
        When I execute all the templates
        Then there should get no errors
    

    有趣的是你如何实现这一点 . 就像是:

    module TemplateStepHelper
      def all_templates
        # either list all the templates here, or better still call a method in 
        # the app to get them
        ...
      end
    end
    World TemplateStepHelper
    
    When "I execute all the templates" do
      @errors = []
      all_templates.each do |t|
        res = t.execute
        @errors << res unless res
      end
    end
    
    Then "there should be no errors" do
      expect(@errors).to be_empty
    end
    

    可以修改代码的确切详细信息以满足您的需求 .

    我建议将代码从步骤defs移到步骤助手中的方法中 .

    最后,如果您从应用程序获取模板,则在添加新模板时甚至不必更新功能 .

相关问题