【问题标题】:Cucumber BDD different features requiring similar stepCucumber BDD 不同的特征需要相似的步骤
【发布时间】:2020-07-30 17:41:49
【问题描述】:

在我的应用程序中,我有两个功能文件:

processing.feature
copy.feature

对于这些,我有两个步骤定义文件:

加工特点:

public class ProcessingFeature {

    @Qualifier("moverA")
    @Autowired
    private IFileMover mover;


    @Given("check file is available")
    public void load() {

    }

    @When("the file is there at the location")
    public void moveFile() {
        output= mover.move(info);

    }   
}

复制功能:

public class CopyFeature {

    @Qualifier("moverB")
    @Autowired
    private IFileMover mover;


    @Given("check file is available")
    public void load() {

    }

    @When("the file is there at the location")
    public void moveFile() {
        output= mover.move(info);

    }   
}

在我的两个功能文件中,我将check file is available 作为@Given 步骤运行。我的第一个问题是,它们是位于两个步骤 def 文件中的相同步骤定义。什么是正确的模式,所以这段代码不会重复,两个功能都可以使用它?

其次,两个功能都运行the file is there at the location,但一个使用MoverA,另一个使用MoverB。但这个想法保持不变。同样,我不喜欢重复,所以我可以通过一些抽象如何最好地重用?

当然我得到了cucumber.runtime.DuplicateStepDefinitionException: Duplicate step definitions 异常,但我很想知道解决这类问题的正确模式是什么

【问题讨论】:

    标签: java spring cucumber bdd gherkin


    【解决方案1】:

    步骤只能定义一次。步骤定义是全局的,并不特定于场景或功能。由于一个步骤只能有一个定义,因此代码重复不是问题。

    【讨论】:

      【解决方案2】:

      您需要阅读 Cucumber 的以下页面:https://cucumber.io/docs/gherkin/step-organization/ 它将帮助您正确组织胶水代码。 正如 Greg 所说,步骤定义是全局的,创建特定于功能的步骤是一种反模式: https://cucumber.io/docs/guides/anti-patterns/#feature-coupled-step-definitions

      在您的功能文件中,我认为When 步骤(操作:即复制文件或处理文件)和 Then 步骤(预期结果:文件应位于哪个位置)之间存在混淆。 您的步骤定义应该更像:

      @When("I copy the following file:(.*)")
      public void copyFile(String fileName) {
          xxxx calling system under test xxxx
      }   
      
      @When("I process the following file:(.*)")
      public void processFile(String fileName) {
          xxxx calling system under test xxxx
      }   
      
      @Then("the file is at the following location:(.*)")
      public void checkFileExistence(String fileLocation) {
          Assert.assertTrue(xxxxx);
      }   
      

      要在步骤之间共享状态,您可以使用 World 对象模式: 参考https://cucumber.io/docs/cucumber/state/

      【讨论】:

        【解决方案3】:

        在使用纯文本文件和步骤映射进行 BDD 实施时,这是不利的。由于附加功能文件层是使用pure java implementation of scenario,因此替代此问题和类似问题。它将消除额外的功能文件层,当然还有步骤定义文件,并允许您遵循行为驱动的开发。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-28
          • 2021-04-28
          • 1970-01-01
          • 1970-01-01
          • 2021-09-11
          • 1970-01-01
          相关资源
          最近更新 更多