每当我尝试并行运行测试类时,两个测试类的日志就会出现在一个中。
这是我的testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods">
<test name="Test">
<classes>
<class name="com.moodle.automation.tests.Professor"/>
<class name="com.moodle.automation.tests.Students"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
它启动了两个浏览器,都运行单独的测试,只生成了1个报告(如预期的那样),但报告显示合并的日志,即test.log(Status.INFO, browser + " launched ...");
,对于这两个测试:
Status Timestamp Details
info_outline 10:15:54 PM Chrome launched ...
info_outline 10:15:54 PM Chrome launched ...
info_outline 10:16:01 PM Navigated to appURL...
info_outline 10:16:02 PM Navigated to appURL...
info_outline 10:16:05 PM Professor logged in ...
info_outline 10:16:08 PM Student logged in ...
我可能做错了什么?我按照本教程https://www.youtube.com/watch?v=QQAmcnGbVQM
进行了操作修改 测试类1与类2相同 第1类:
@Test()
public void test1() {
extentTest = extentReports.createTest("Test1");
extentTest.log(Status.INFO, "Test1 started ...");
Assert.assertTrue(1>1);
}
@Test()
public void test2() {
extentTest = extentReports.createTest("Test2");
extentTest.log(Status.INFO, "Test2 started ...");
Assert.assertTrue(1>1);
}
@Test()
public void test3() {
extentTest = extentReports.createTest("Test3");
extentTest.log(Status.INFO, "Test3 started ...");
Assert.assertTrue(1<0);
}
BaseClass的:
@AfterSuite
public void afterSuite() {
// extentReports.flush();
String finalReport = System.getProperty("user.dir") + "/Reporting/reports/report_" + timeStamp + ".html";
File htmlFile = new File(finalReport);
try {
Desktop.getDesktop().browse(htmlFile.toURI());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@AfterMethod
public void getResult(ITestResult result) {
if(result.getStatus() == ITestResult.FAILURE){
extentTest.fail(MarkupHelper.createLabel(result.getName()+" Test case failed", ExtentColor.RED));
extentTest.fail(result.getThrowable());
}
else if(result.getStatus() == ITestResult.SUCCESS){
extentTest.pass(MarkupHelper.createLabel(result.getName()+" Test case passed", ExtentColor.GREEN));
}
else {
extentTest.skip(MarkupHelper.createLabel(result.getName()+" Test case skipped", ExtentColor.YELLOW));
extentTest.skip(result.getThrowable());
}
}
@AfterTest
public void flushTestReports(){
extentReports.flush();
}
为了重现它,并行运行两个相同的浏览器。
答案 0 :(得分:1)
尝试结束测试并在@AfterMethod中刷新报告并在@AfterTest方法中关闭报告。它对我有用。试试下面代码:
@AfterMethod(alwaysRun=true)
public void TearDown_AM(ITestResult result) throws IOException
{
System.out.println("@After Method");
try
{
if(result.getStatus()==ITestResult.FAILURE)
{
String res = captureScreenshot(Driver, result.getName());
String image= logger.addScreenCapture(res);
System.out.println(image);
String TestCaseName = this.getClass().getSimpleName() + " Test Case Failure and Title/Boolean Value Failed";
logger.log(LogStatus.FAIL, TestCaseName + logger.addScreenCapture(res));
// logger.log(LogStatus.FAIL, image, this.getClass().getSimpleName() + " Test Case Failure and Title/Boolean Value Failed");
}
else if(result.getStatus()==ITestResult.SUCCESS)
{
logger.log(LogStatus.PASS, this.getClass().getSimpleName() + " Test Case Success and Title Verified");
}
else if(result.getStatus()==ITestResult.SKIP)
{
logger.log(LogStatus.SKIP, this.getClass().getSimpleName() + " Test Case Skipped");
}
report.endTest(logger);
report.flush();
}
catch(Throwable t)
{
logger.log(LogStatus.ERROR,t.fillInStackTrace());
}
}
@AfterTest(alwaysRun=true)
public void AfterTest()
{
System.out.println("@After Test");
Driver.close();
report.close();
}
答案 1 :(得分:1)
首先,使用synchronized方法创建一个类,该方法返回 ExtentReports
的实例:
public class ExtentManager {
private static ExtentReports report;
public static synchronized ExtentReports getInstance() {
if (report == null) {
report = new ExtentReports("MyReport.html");
}
return report;
}
}
其次,创建另一个只声明测试相关同步方法的类(当然,这些方法必须处理线程明智)。代码段:
public class ExtentTestManager {
static Map<Integer, ExtentTest> extentTestMap = new HashMap<Integer, ExtentTest>();
private static ExtentReports extent = ExtentManager.getInstance();
public static synchronized ExtentTest getTest() {
return extentTestMap.get((int) (long) (Thread.currentThread().getId()));
}
public static synchronized void endTest() {
extent.endTest(extentTestMap.get((int) (long) (Thread.currentThread().getId())));
}
public static synchronized ExtentTest startTest(String testName) {
return startTest(testName, "");
}
public static synchronized ExtentTest startTest(String testName, String desc) {
ExtentTest test = extent.startTest(testName, desc);
extentTestMap.put((int) (long) (Thread.currentThread().getId()), test);
return test;
}
}
最后,相应地修改 BaseClass :
public class BaseClass extends TestListenerAdapter {
public ExtentTest testReporter;
@BeforeMethod
public void beforeMethod(Method m) {
ExtentTestManager.startTest(m.getName(), "This is a simple test.");
}
@AfterMethod
public void afterMethod(ITestResult result) {
if (result.isSuccess()) {
ExtentTestManager.getTest().log(LogStatus.PASS, "Test passed");
ExtentTestManager.getTest().log(LogStatus.PASS, "Run from thread " + Thread.currentThread().getId());
}
else if (result.getStatus() == ITestResult.FAILURE) {
ExtentTestManager.getTest().log(LogStatus.FAIL, "Test failed");
ExtentTestManager.getTest().log(LogStatus.PASS, "Run from thread " + Thread.currentThread().getId());
}
else if (result.getStatus() == ITestResult.SKIP) {
ExtentTestManager.getTest().log(LogStatus.SKIP, "Test skipped");
ExtentTestManager.getTest().log(LogStatus.PASS, "Run from thread " + Thread.currentThread().getId());
}
ExtentTestManager.endTest();
ExtentManager.getInstance().flush();
}
@AfterSuite
public void afterSuite() {
ExtentManager.getInstance().flush();
}
}
编辑:
使用您的测试创建一个测试类(ExampleTest.java
):
public class ExampleTest extends BaseClass{
@Test
public void test_01(){
Assert.assertTrue(false);
}
@Test
public void test_02(){
Assert.assertTrue(false);
}
@Test
public void test_03(){
Assert.assertTrue(true);
}
@Test
public void test_04(){
Assert.assertTrue(false);
}
}
必填testng.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="DemoSuite1" parallel="methods" thread-count="2">
<test name = "Test">
<classes>
<class name = "com.extent.demo.ExampleTest" />
</classes>
</test>
</suite>