需要在范围报告中打印 log4j 日志。我怎样才能做到这一点?
问问题
3215 次
2 回答
1
尝试结束测试并在@AfterMethod 中刷新报告并在@AfterTest Method 中关闭报告。它对我有用。试试下面的代码:
@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();
}
于 2017-11-20T07:48:32.467 回答
1
首先,创建一个具有同步方法的类,该方法返回以下实例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>
于 2017-11-20T11:11:32.787 回答