maven - 范围报告仅显示 testng maven selenium java 框架的最后运行结果
问题描述
我正在使用 testng 6.9.10,extentreports 3.1.5。使用 maven surefire 插件并使用 forkcount 和重用 forks ,测试并行运行。(即当我设置forkcount -> 2 和reuseforks-> true 时,打开两个chrome 浏览器实例,两个测试类并行运行)
mvn test -Dgroups=group1
(有两个测试类属于 group1)。问题是范围报告仅显示上次运行的结果。
我只在 pom.xml 中包含了侦听器类(不在其他任何地方,不在 @beforeclass 或 @afterclass 作为 BaseTest 类的一部分)
<property>
<name>listener</name>
<value>util.listener.TestExtentListener</value>
</property>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<forkCount>2</forkCount>
<reuseForks>true</reuseForks>
请问有什么解决办法吗?
public class ExtentManager {
private static ExtentReports extent;
private static String reportFileName = "Test-Automaton-Report"+".html";
private static String fileSeperator = System.getProperty("file.separator");
private static String reportFilepath = System.getProperty("user.dir") +fileSeperator+ "TestReport";
private static String reportFileLocation = reportFilepath +fileSeperator+ reportFileName;
public static ExtentReports getInstance() {
if (extent == null)
createInstance();
return extent;
}
//Create an extent report instance
public static ExtentReports createInstance() {
String fileName = getReportPath(reportFilepath);
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(fileName);
htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
htmlReporter.config().setChartVisibilityOnOpen(true);
htmlReporter.config().setTheme(Theme.STANDARD);
htmlReporter.config().setDocumentTitle(reportFileName);
htmlReporter.config().setEncoding("utf-8");
htmlReporter.config().setReportName(reportFileName);
htmlReporter.config().setTimeStampFormat("EEEE, MMMM dd, yyyy, hh:mm a '('zzz')'");
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
//Set environment details
extent.setSystemInfo("OS", "Mac");
extent.setSystemInfo("AUT", "QA");
return extent;
}
//Create the report path
private static String getReportPath (String path) {
File testDirectory = new File(path);
if (!testDirectory.exists()) {
if (testDirectory.mkdir()) {
System.out.println("Directory: " + path + " is created!" );
return reportFileLocation;
} else {
System.out.println("Failed to create directory: " + path);
return System.getProperty("user.dir");
}
} else {
System.out.println("Directory already exists: " + path);
}
return reportFileLocation;
}
}
public class ExtentTestManager {
static Map<Integer, ExtentTest> extentTestMap = new HashMap<Integer, ExtentTest>();
static ExtentReports extent = ExtentManager.getInstance();
public static synchronized ExtentTest getTest() {
return (ExtentTest) extentTestMap.get((int) (long) (Thread.currentThread().getId()));
}
public static synchronized void endTest() {
extent.flush();
}
public static synchronized ExtentTest startTest(String testName) {
ExtentTest test = extent.createTest(testName);
extentTestMap.put((int) (long) (Thread.currentThread().getId()), test);
return test;
}
}
public class TestExtentListener implements ITestListener {
ExtentTest test;
private static ThreadLocal<ExtentTest> extentTestThreadLocal = new ThreadLocal<ExtentTest>();
public void onStart(ITestContext context) {
System.out.println("*** Test Suite " + context.getName() + " started ***");
}
public void onFinish(ITestContext context) {
System.out.println(("*** Test Suite " + context.getName() + " ending ***"));
ExtentTestManager.endTest();
ExtentManager.getInstance().flush();
}
public void onTestStart(ITestResult result) {
System.out.println(("*** Running test method " + result.getMethod().getMethodName() + "..."));
test = ExtentTestManager.startTest(result.getMethod().getMethodName());
extentTestThreadLocal.set(test);
}
public void onTestSuccess(ITestResult result) {
System.out.println("*** Executed " + result.getMethod().getMethodName() + " test successfully...");
extentTestThreadLocal.get().log(Status.PASS, "Test passed");
}
public void onTestFailure(ITestResult result) {
System.out.println("*** Test execution " + result.getMethod().getMethodName() + " failed...");
extentTestThreadLocal.get().log(Status.FAIL, "Test Failed");
}
public void onTestSkipped(ITestResult result) {
System.out.println("*** Test " + result.getMethod().getMethodName() + " skipped...");
extentTestThreadLocal.get().log(Status.SKIP, "Test Skipped");
}
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
System.out.println("*** Test failed but within percentage % " + result.getMethod().getMethodName());
}
}
解决方案
您可能没有在 java 中使用线程本地类来使范围报告线程安全。否则对象将被覆盖,范围报告仅显示活动测试结果。
你可以这样做:
ExtentReports extent = ExtentReportGenerator.ExtentReport();
ExtentTest test;;
private static ThreadLocal<ExtentTest> extent_test = new ThreadLocal<ExtentTest>();
更多详细信息,您可以参考此博客。
https://www.automationinja.com/post/thread-safe-extent-report-in-selenium
推荐阅读
- r - R:使用想要的子字符串列表清理字符串
- amazon-web-services - 在 CloudFormation 模板中使用 CodeBuild 创建的 AMI ID
- c# - C# 使用 Newtonsoft.json DefaultContractResolver IValueProvider 替换数据崩溃
- python - 在 Pandas 数据框中将列类型从字符串转换为日期时间格式
- time-series - 如何在 GEE 中的 Shapefile 中创建具有所有多边形随时间变化的图形?
- c++ - 如何制作序列的动态数组?
- python-3.x - 无法从亚马逊刮取产品价格
- c - memcpy() 用于裸机环境
- ios - Swift DateFormatter 属性不变
- frequency - R函数查找随机森林树元素的加权频率