【问题标题】:How do i specify particular step definition file in Cucumber runner class如何在 Cucumber runner 类中指定特定的步骤定义文件
【发布时间】:2017-06-29 02:22:37
【问题描述】:

我正在尝试将特定步骤定义文件与 cucumber Options 中的特定功能文件粘合在一起,当我使用 JUnit Test 运行测试运行程序类时,它不被视为我在测试运行程序类中提供的用于执行的步骤定义文件。我的控制台还会打印代码 sn-p 以开发测试运行器类中提到的功能文件的步骤。

我刚刚创建了 JavaProject,没有使用 maven。

我的功能文件路径(由 4 个不同的功能文件组成): 项目/功能

我的 StepDefinition 文件路径(有 4 个不同的步骤定义文件): 项目/src/StepDefinition

我的 Runner 类文件路径: 项目/src/cucumberTest

这里,我想将特征文件 4 与 runner 类中的 stepDefinition 文件 4 映射

我的 Runner 类代码:

package cucumberTest;

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "Feature/Test4.feature"
        ,glue = {"stepDefinition/Test4_Step"}
        ,monochrome = true
        )
public class Test4_TestRunner {

} 

我是 Cucumber 的新手,谁能帮我实现这一点?

【问题讨论】:

    标签: java selenium bdd gherkin cucumber-junit


    【解决方案1】:

    您正在将粘合路径设置为类而不是包名称。请尝试使用下面给出的包名称。

    package cucumberTest; 
    import org.junit.runner.RunWith;  
    import cucumber.api.CucumberOptions; 
    import cucumber.api.junit.Cucumber; 
    @RunWith(Cucumber.class) 
    @CucumberOptions( 
       features = "Feature/Test4.feature" ,
       glue =
    {"stepDefinition"} ,
      monochrome = true ) 
     public class Test4_TestRunner { } 
    

    【讨论】:

    • 当我将包名指定给粘合路径时,测试运行器正在考虑包中的第一步定义类文件,但是,它应该运行包中的第四步定义文件。注意:我的第 1 步和第 4 步定义文件的前几个步骤是相同的​​。
    • 然后你必须为每个类创建多个具有不同名称的包。但这不是好办法。你能添加你的功能文件和类吗?
    • 尝试使用glue = {"stepDefinition.Test4_Step"} 注意:请记住使每个测试步骤类中的步骤定义唯一。
    【解决方案2】:

    当您向@CucumberOptions features 提供路径参数时,该特定包(路径)中的所有 .feature 文件将由测试运行程序执行。您不能通过@CucumberOptions feature 选项引用特定文件,仅引用包(路径)。为了将执行限制为特定的 .feature 文件,您可以执行以下操作之一。

    1. 将您愿意执行的特定 .feature 文件移动到单独的包中,然后通过 @CucumberOptions features 选项引用该包(路径)。请注意,如果您没有明确指定@CucumberOptions features,黄瓜将尝试在与您的包结构匹配的子目录中查找功能。在这种情况下,例如,如果您的测试运行程序(例如:JUnit)在包 cucumberTest 中,那么您的功能文件必须位于文件夹 cucumberTest 的类路径中。
    2. 在您的 .feature 文件中使用标记标记 featurescenario。例如@execute。这将允许您通过@Cucumber.Options(tags = {"@execute"}) 让 cucumber 知道您只想执行特定标记的feature(如果标签应用于功能)或scenario(如果标签应用于场景)。要达到相反的效果,即从执行中排除特定功能/场景,只需添加~,例如@Cucumber.Options(tags = {"~@notest"})。您可以引用任意数量的标签。

    More information about cucumber tags

    一般而言,我建议您遵循标准的 maven 项目结构,即将您的步骤定义放在 src/test/java 中,将您的功能文件放在 src/test/resources 中。您可以从您最喜欢的 IDE 或命令行工具创建新的 maven 项目。

    More information how to get started with maven project.

    【讨论】:

      【解决方案3】:

      您需要将以下代码添加到 CucumberOptions

      格式 = { "漂亮", "html:target/site/cucumber-pretty", "json:src/test/resources/json/cucumber.json" }

      【讨论】:

      • 不就是生成html和json报告吗?我的意图不是运行文件夹中的所有功能和相应的 stepDefinition,而是运行特定的功能和相应的 stepDefinition 文件。您能帮我吗,如何在 cucumberrOptions 中映射特定文件?
      • 我宁愿说,你 mavenise 你的项目。将你的功能文件保存在 serc/test/resources/feature 下。当你在类之上运行时,它会根据你的步骤 Defn 类中的实现功能文件中的场景。
      【解决方案4】:

      您的语法不正确。您应该将特征指定为数组的单个元素:

      features = {"Feature/Test4.feature"}
      

      不喜欢:

      features = "Feature/Test4.feature"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-13
        • 1970-01-01
        • 2022-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多