假设这些步骤是一种长期测试方法的一部分。尝试使用子节点的概念。当断言失败时,您可以将它们的状态设置为失败或错误等。问题是您需要在 try-catch 块中进行硬断言才能捕获AssertionError
然后设置状态。
ExtentTest test = extent.startTest("Hello","Yeah");
extent.loadConfig(ExtentReports.class, "extent-config.xml");
test.log(LogStatus.PASS, "Before Step details");
ExtentTest child1 = extent.startTest("Child 1");
try{
//Assertion to be placed here
child1.log(LogStatus.PASS, "Pass");
} catch(AssertionError e) {
child1.log(LogStatus.FAIL, "Fail");
}
//Add to soft assertion
ExtentTest child2 = extent.startTest("Child 2");
try{
//Assertion to be placed here
child2.log(LogStatus.PASS, "Pass");
} catch(AssertionError e) {
child2.log(LogStatus.FAIL, "Fail");
}
//Add to soft assertion
test.appendChild(child1).appendChild(child2);
test.log(LogStatus.PASS, "After Step details");
获取如下报告 -
更新将此方法添加到ExtentTestManager
类中并从 testng 测试中调用静态方法。ThreadLocal
尽管可以使用-http : //extentreports.com/docs/versions/3/java/#testng-examples以更简单的方式编写此类
public static synchronized void updateStepResult(String childNodeDesc, Object actual, Object expected) {
ExtentTest test = extentTestMap.get((int) (long) (Thread.currentThread().getId()));
ExtentTest cn = test.appendChild(extent.startTest(childNodeDesc));
try {
assertEquals(actual, expected);
cn.log(LogStatus.PASS, "Pass");
} catch (AssertionError e) {
cn.log(LogStatus.FAIL, "Fail");
}
}