在多线程执行期间使用TestNG生成Cucumber ExtentReport



我正在使用Cucumber和TestNG在移动设备上并行运行测试。下面给出了我的TestNG runner类。

@CucumberOptions(
features="src/test/resources/features",
glue={"org.cucumber.stepdefs"},
plugin = {
"com.cucumber.listener.ExtentCucumberFormatter:" }, monochrome = true)
public class TestRunner extends BaseTest {
private static TestNGCucumberRunner testRunner;
@BeforeClass
public void setUP() {   
System.out.println("Thread = " + Thread.currentThread().getId() + " - object hash = " + this.hashCode());
testRunner = new TestNGCucumberRunner(TestRunner.class);
ExtentProperties extentProperties = ExtentProperties.INSTANCE;
extentProperties.setReportPath("output/" + this.hashCode() + "-report.html");
}
@Test(description="Tests",dataProvider="features")
public void setUpClass(CucumberFeatureWrapper cFeature) {
testRunner.runCucumber(cFeature.getCucumberFeature());
}
@DataProvider(name="features")
public Object[][] getFeatures() {
return testRunner.provideFeatures();
}
@AfterClass
public static void teardown() {
testRunner.finish();
}
}

正如预期的那样,我的Cucumber测试在每个连接设备的单独线程上并行运行(在我的testng.xml文件中定义(。

我使用ExtentReports在每次运行后生成报告,尽管目前我只得到一个报告,我希望每个运行的线程都有一个单独的报告。

我不明白为什么会发生这种事。我添加了控制台打印,以检查每个正在运行的线程是否有TestRunner类的即时信息,事实上确实存在-运行后我的控制台日志包含以下内容(假设连接了2个移动设备(:

Thread = 33 - object hash = 923219673
Thread = 32 - object hash = 280884709

因此,这正如我所期望的那样工作,除了在生成报告时,只创建了一个包含所有测试数据的报告。我的假设是,因为我在setUp()方法中将报告路径设置为每个线程中的唯一文件名,所以应该为每个线程创建一个报告?

似乎在生成报告时,Cucumber ExtendedReport插件会等待所有测试完成,并默认生成批量报告。这是正确的吗?如果是这样的话,我该如何改变呢?

以下是我的pom.xml文件的主要部分,以获取其他配置信息。

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-jvm -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-jvm</artifactId>
<version>4.2.0</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-testng -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-testng</artifactId>
<version>1.2.5</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-picocontainer -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>1.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.aventstack/extentreports -->
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>3.1.5</version>
</dependency>
<dependency>
<groupId>com.vimalselvam</groupId>
<artifactId>cucumber-extentsreport</artifactId>
<version>3.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-junit -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>6.0.0-BETA3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<parallel>tests</parallel>
<threadCount>10</threadCount>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
@BeforeClass
public void setUP() {   
System.out.println("Thread = " + Thread.currentThread().getId() + " - object hash = " + this.hashCode());
testRunner = new TestNGCucumberRunner(TestRunner.class);
ExtentProperties extentProperties = ExtentProperties.INSTANCE;
extentProperties.setReportPath("output/" + this.hashCode() + "-report.html");
}

此设置方法被并行调用两次。如果您正在更改这些线程之间共享的ExtentProperties.INSTANCE上的setReportPath。因此线程正在覆盖彼此的结果。您可以通过打印ExtentProperties.INSTANCE的哈希来验证这一点。

最新更新