【问题标题】:How to use BeforeAll and AfterAll cucumber anonotation?如何使用 BeforeAll 和 AfterAll 黄瓜注解?
【发布时间】:2018-07-31 00:48:35
【问题描述】:

我想在功能文件之前执行以下一些操作。

public void beforeFeature() {
    File path = new File("D:\\AccelareWorkFitComplete\\ExtentsReports\\");

    File[] filename =path.listFiles();

    for(File fi:filename)
    {
        if (fi.toString().contains("Automation")) 
        {
            fi.delete();
        }
    }
}

【问题讨论】:

    标签: cucumber bdd


    【解决方案1】:

    beforeAllafterAll 钩子用于整个黄瓜套件,而不是围绕每个功能。

    之前的几个版本,有 beforeFeatureafterFeature 挂钩,但在最近的版本中已被删除。

    总而言之,您可以返回几个版本以利用 before/after 功能,也可以在 before 挂钩中创建解决方法。

    【讨论】:

      【解决方案2】:

      我会使用背景(或@Before 在幕后执行)。官方文档是here

      Feature File:
      
          Background:
             Given Files are deleted
      
          Scenerio: Test 1
             Then There are no files.
      

      在我的步骤定义文件中,我将执行以下操作:

      private static boolean testsInitialized = false
      
      @Given("^Given files are deleted;$")
      public void deleteFiles() {
      
      if (!testsInitialized) {
          initializetest();
          testInitialized = true;
      }
      

      如果您绝对需要 Junit @BeforeClass 等效项,从 Cucumber 2.4.0 开始,以下类可以工作。

      你可以把它放在 test/java/cucumber/runtime 中。

      package cucumber.runtime; //cannot change.  can be under /test/java
      
      import cucumber.runtime.io.ResourceLoader;
      import cucumber.runtime.snippets.FunctionNameGenerator;
      import gherkin.pickles.PickleStep;
      
      import java.util.List;
      
      public class NameThisClassWhatever implements Backend {
          private Glue glue;
          private List<String> gluePaths;
      
          @Override
          public void loadGlue(Glue glue, List<String> gluePaths) {
              this.glue = glue;
              this.gluePaths = gluePaths;
      
              //Any before steps here
          }
      
          @Override
          public void disposeWorld() {
              //any after steps here
          }
      
          @Override
          public void setUnreportedStepExecutor(UnreportedStepExecutor executor) { }
      
          @Override
          public void buildWorld() { }
      
          @Override
          public String getSnippet(PickleStep p, String s, FunctionNameGenerator fng) {
              return null;
          }
      
          public NameThisClassWhatever(ResourceLoader resourceLoader)  { }
      }
      

      【讨论】:

      • Background 部分本质上是一个 Before 钩子,适用于任何特定功能文件中的所有场景,在 Before 钩子之后但在场景执行之前运行 - 本质上是你的答案错误的。它根本不会在该功能之前运行一次。以这种格式运行的测试可以看出这一点。如果您有一个登录并导航到页面的Background,它将登录并导航到功能文件中包含的每个Scenario 的页面。
      • @KyleFairns 你是绝对正确的,我想我记得后台只执行过一次。使用布尔检查器更正。
      猜你喜欢
      • 1970-01-01
      • 2014-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-03
      • 1970-01-01
      相关资源
      最近更新 更多