我刚接触Selenium,Cucumber Tool,正在学习使用Selenium,cucumber,maven和TestNG构建BDD框架。
我基本上有三个Java文件- 测试运行器(src / test / java) 步骤定义(src / test / java) TestBase(src / main / java)
我在TestBase类中定义了之前和之后的钩子 我的Test-Runner类有一个为Extent-report定义的插件,还有@AfterClass批注,该批注正在加载range-config.xml-
现在,当运行功能文件时,它不执行@AfterClass批注,因此跳过生成范围报告。 但是,如果我使用TestNG直接运行Test-runner文件,它将跳过执行testBase Class-
中定义的Hook。Test-Runner代码-
@CucumberOptions(
features={"src/test/resources/Features/login"},
glue={"stepDefinition","src/main/java/Utils/TestBase.java"},
monochrome=true,
plugin={"pretty","html:target/Reports","com.cucumber.listener.ExtentCucumberFormatter:target/cucumber-reports/report.html"}
)
public class Login_Runner extends AbstractTestNGCucumberTests{
@AfterClass
public static void writeExtentReport() {
System.out.println("I am in After Class");
Reporter.loadXMLConfig(new File("src/test/resources/extent-config.xml"));
}
任何人都可以帮助解决此问题吗?
答案 0 :(得分:0)
假设以下结构
src/test/java/stepdef/LoginSteps.java
src/test/java/runner/Login_Runner.java
src/test/resources/features/login.feature
pom.xml
LoginSteps.java
implements all steps for `login.feature`
Login_Runner.java
package runner;
import org.testng.annotations.AfterClass;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
@CucumberOptions(features = "src/test/resources/features", glue = "stepdef")
public class Login_Runner extends AbstractTestNGCucumberTests {
@AfterClass
public static void theAfterClassMethod() {
System.out.println("execute @AfterClass annotated method");
}
}
login.feature
Feature: test
Scenario: login
Given open browser
And start app
When enter details
Then login happens
pom.xml *
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.suboptimal</groupId>
<artifactId>cuke-testng2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>3.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>3.0.2</version>
<type>jar</type>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
<surefire.version>2.22.0</surefire.version>
</properties>
</project>
运行测试
mvn clean test -Dtest=Login_Runner
产生输出
...
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running runner.Login_Runner
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@5ae63ade
execute @Before annotated method
execute @After annotated method
execute @AfterClass annotated method
1 Scenarios (1 passed)
4 Steps (4 passed)
0m0.023s
将类Login_Runner
重命名为例如LoginRunnerTest
,您可以使用mvn clean test
进行测试。
也许跑步者类名称是您的最初问题,而不是错过了@AfterTest
方法的执行。