ExtentReports工具类
public class ExtentReportsUtil {
public static Map extentMap = new HashMap<>();
public static ExtentTest classTest;
public static ExtentTest methodTest;
public static ExtentReports extent;
public static ExtentTest initTest;
public static void init(String name) {
if (!extentMap.containsKey(name)) {
extent = new ExtentReports();
ExtentSparkReporter spark =
new ExtentSparkReporter("../doc/test/report/" + name + ".html")
.config(
ExtentSparkReporterConfig.builder()
.offlineMode(true)
.documentTitle(name)
.timeStampFormat("yyyy-MM-dd hh:mm:ss")
.build())
.viewConfigurer()
.viewOrder()
.as(
new ViewName[] {
ViewName.AUTHOR,
ViewName.CATEGORY,
ViewName.DASHBOARD,
ViewName.DEVICE,
ViewName.EXCEPTION,
ViewName.LOG,
ViewName.TEST
})
.apply();
extent.attachReporter(spark);
extentMap.put(name, extent);
} else {
extent = extentMap.get(name);
}
}
}
junit单元测试公共类
public class CommonTest {
@RegisterExtension public DefaultTestWatcher testWatcher = new DefaultTestWatcher();
@BeforeAll
public void init(TestInfo testInfo) {
ExtentReportsUtil.init(
testInfo.getTags().stream().findFirst().orElseGet(() -> System.currentTimeMillis() + ""));
ExtentReportsUtil.classTest = ExtentReportsUtil.extent.createTest(testInfo.getDisplayName());
ExtentReportsUtil.initTest = ExtentReportsUtil.classTest.createNode("测试初始化");
log.info("[{}]类开始测试", testInfo.getDisplayName());
}
@BeforeEach
public void setUp(TestInfo testInfo) {
ExtentReportsUtil.methodTest =
ExtentReportsUtil.classTest.createNode(testInfo.getDisplayName());
ExtentReportsUtil.initTest = null;
log.info("[{}]方法开始测试", testInfo.getDisplayName());
}
@AfterEach
public void tearDown(TestInfo testInfo) {
log.info("[{}]方法结束测试", testInfo.getDisplayName());
}
@AfterAll
public void done(TestInfo testInfo) {
log.info("[{}]类结束测试", testInfo.getDisplayName());
ExtentReportsUtil.extent.flush();
ExtentReportsUtil.initTest = ExtentReportsUtil.classTest.createNode("测试结束");
}
}
自定义单元测试监听
public class DefaultTestWatcher implements TestWatcher {
@Override
public void testSuccessful(ExtensionContext context) {
ExtentReportsUtil.methodTest.pass("测试成功");
ExtentReportsUtil.extent.flush();
ExtentReportsUtil.methodTest = null;
}
@Override
public void testFailed(ExtensionContext context, Throwable e) {
ExtentReportsUtil.methodTest.fail(e);
ExtentReportsUtil.extent.flush();
ExtentReportsUtil.methodTest = null;
}
}
自定义log4j2的Appende
@Plugin(
name = "ExtentReportsAppender",
category = "Core",
elementType = "appender",
printObject = true)
public class ExtentReportsAppender extends AbstractAppender {
protected ExtentReportsAppender(
String name,
Filter filter,
Layout extends Serializable> layout,
boolean ignoreExceptions,
Property[] properties) {
super(name, filter, layout, ignoreExceptions, properties);
}
@Override
public void append(LogEvent event) {
final byte[] bytes = getLayout().toByteArray(event);
String log = new String(bytes);
if (Objects.nonNull(ExtentReportsUtil.methodTest)) {
ExtentReportsUtil.methodTest.info(log);
return;
}
if (Objects.nonNull(ExtentReportsUtil.initTest)) {
ExtentReportsUtil.initTest.info(log);
}
}
@PluginFactory
public static ExtentReportsAppender createAppender(
@PluginAttribute("name") String name,
@PluginElement("Filter") final Filter filter,
@PluginElement("Layout") Layout extends Serializable> layout,
@PluginAttribute("ignoreExceptions") boolean ignoreExceptions) {
if (name == null) {
LOGGER.error("No name provided for MyCustomAppenderImpl");
return null;
}
if (layout == null) {
layout = PatternLayout.createDefaultLayout();
}
return new ExtentReportsAppender(name, filter, layout, ignoreExceptions, Property.EMPTY_ARRAY);
}
}
在log4j2的配置文件中使用