java 我如何在 Cucumber runner 类中指定特定的步骤定义文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44815352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i specify particular step definition file in Cucumber runner class
提问by Sri
I am trying to glue particular step Definition file with specific feature file in cucumber Options and when I run the test runner class using JUnit Test, it was not considered my step definition file provided in test runner class for execution. Also my console prints the code snippet to develop steps for feature file mentioned in test runner class.
我试图将特定步骤定义文件与黄瓜选项中的特定功能文件粘合在一起,当我使用 JUnit 测试运行测试运行程序类时,它不被视为测试运行程序类中提供的用于执行的步骤定义文件。此外,我的控制台打印代码片段以开发测试运行程序类中提到的功能文件的步骤。
I just created JavaProject, not using maven.
我刚刚创建了 JavaProject,没有使用 maven。
My Feature file path (which consist of 4 different feature files): Project/Feature
我的功能文件路径(由 4 个不同的功能文件组成):项目/功能
My StepDefinition file path (which has 4 different step definition files): Project/src/StepDefinition
我的 StepDefinition 文件路径(有 4 个不同的步骤定义文件):Project/src/StepDefinition
My Runner class file path: Project/src/cucumberTest
我的Runner类文件路径:Project/src/cucumberTest
Here, I want to map feature file 4 with stepDefinition file 4 in runner class
在这里,我想在 runner 类中使用 stepDefinition 文件 4 映射特征文件 4
My Runner class code:
我的跑步者类代码:
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 {
}
I am new to Cucumber, Can anyone please help me how achieve this?
我是 Cucumber 的新手,谁能帮我实现这个目标?
回答by Murthi
You are setting glue path to class instead of package name. Please try with package name as given below.
您正在将胶水路径设置为类而不是包名称。请尝试使用下面给出的包名称。
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 { }
回答by fg78nc
When you supply path argument to @CucumberOptions features
ALL .feature files in that particular package(path) will be executed by test runner. You can't refer to particular file via @CucumberOptions feature
option, reference is to package(path) only. In order to limit execution to particular .feature file you can do ONE of the following.
当您@CucumberOptions features
为该特定包(路径)中的所有 .feature 文件提供路径参数时,测试运行程序将执行。您不能通过@CucumberOptions feature
选项引用特定文件,仅引用包(路径)。为了限制对特定 .feature 文件的执行,您可以执行以下操作之一。
- Move particular .feature file you are willing to execute to a separate package and then refer to that package(path) via
@CucumberOptions features
option. Note that if your do not specify@CucumberOptions features
explicitly cucumber will try to locate features in subdirectories matching your package structure. In this case, for example if your test runner (ex: JUnit) is in packagecucumberTest
then your feature file has to be on classpath in the foldercucumberTest
. - Mark
feature
orscenario
within your .feature file with a tag. For example@execute
. This will allow you to let cucumber know that you want to execute only particular taggedfeature
(if tag is applied to feature) orscenario
(if tag is applied to scenario) via@Cucumber.Options(tags = {"@execute"})
. To achieve opposite effect, i.e. exclude particular feature/scenario from execution just add~
, example@Cucumber.Options(tags = {"~@notest"})
. You can refer as many tags as you want.
- 将您愿意执行的特定 .feature 文件移动到单独的包中,然后通过
@CucumberOptions features
选项引用该包(路径)。请注意,如果您没有@CucumberOptions features
明确指定,cucumber 将尝试在与您的包结构匹配的子目录中定位功能。在这种情况下,例如,如果您的测试运行程序(例如:JUnit)在包中,cucumberTest
那么您的功能文件必须位于文件夹中的类路径上cucumberTest
。 - 用标签标记
feature
或scenario
在您的 .feature 文件中。例如@execute
。这将允许你让黄瓜知道你想只执行特定的标签feature
(如果标签被应用到的功能),或scenario
通过(如果标签被应用到场景)@Cucumber.Options(tags = {"@execute"})
。为了达到相反的效果,即从执行中排除特定的功能/场景,只需添加~
示例@Cucumber.Options(tags = {"~@notest"})
。您可以根据需要引用任意数量的标签。
More information about cucumber tags
In general I would recommend you to follow standard maven project structure, i.e. put you step definitions in src/test/java
and your feature files in src/test/resources
. You can create new maven project from your favorite IDE or from command line tool.
一般来说,我会建议您遵循标准的 maven 项目结构,即将您的步骤定义src/test/java
放在src/test/resources
. 您可以从您喜欢的 IDE 或命令行工具创建新的 Maven 项目。
回答by Ankur Srivastava
you need to add below code to CucumberOptions
您需要将以下代码添加到 CucumberOptions
format = { "pretty", "html:target/site/cucumber-pretty", "json:src/test/resources/json/cucumber.json" }
格式 = { "漂亮", "html:target/site/cucumber-pretty", "json:src/test/resources/json/cucumber.json" }