如何使用带有 JAVA 的 Cucumber 框架的 TestNG?

How to use TestNG with Cucumber framework with JAVA?

我正在使用 Junit 运行 运行ner 文件,但我想使用 TestNG 运行 特征文件和范围报告。 请帮我找出 TestNG with cucumber 运行ner class 到 运行 项目。

@RunWith(ExtendedCucumber.class) -This is used for Junit
AbstractTestNGCucumberTests – is used for TestNG

使用 AbstractTestNGCucumberTests 扩展 Runner class 并添加 TestNG 依赖项

第 1 步 - 如果您使用的是 Maven,则在您的 pom.xml 中添加此 "cucumber-testng" 依赖项 - 我使用的是 4.8.0 版本

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-testng</artifactId>
    <version>4.8.0</version>
</dependency>

第 2 步 - 您需要创建一个 Runner Class 并将其扩展到 AbstractTestNGCucumberTests,因此它看起来如下所示 -

public class <your runner class name here>  extends AbstractTestNGCucumberTests

第 3 步- 在您的 运行ner class

中添加以下 "CucumberOptions"
@CucumberOptions
(
features = "<your feature file path>", //the path of the feature files
glue={"<your step definition file path>"}, //the path of the step definition files
plugin= {"pretty:target/cucumber-pretty.txt",
        "html:target/cucumber-html-report",
        "json:target/cucumber.json",
        "rerun:target/rerun.txt"
        }, //to generate different types of reporting
monochrome = true, //display the console output in a proper readable format
strict = true, //it will check if any step is not defined in step definition file
dryRun = false //to check the mapping is proper between feature file and step definition file
)

现在,您可以从 testng.xml 文件中 运行 执行。 确保在 testng.xml 文件中添加 "Runner class"。

<class name="runner class path here" />

要用 TestNG 替换 Junit,可以使用以下方法:

一个。在 TestNG xml 中,在 class 标签中添加 TestRunner 文件的路径和名称。它将帮助 TestNG 定位 cucumber 的 TestRunner 文件

b。在 TestRunner 文件中,确保您的 class extends AbstractTestNGCucumberTests

c。在 Maven 中添加 cucumber-testNG 依赖项以加载所需的 jar。

那你就可以开始了,除了我们已经拥有的控件(来自 TestRunner)之外,它还可以帮助你从 testNG 控制测试执行。此外,现在可以使用 cucumber-reporting,这似乎是最好的报告工具,尤其是对于测试自动化工程师而言。 (但我认为开发人员也可以在单元测试中使用它)