栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

junit+ExtentReports使用及log4j2日志写入

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

junit+ExtentReports使用及log4j2日志写入

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 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 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的配置文件中使用

            
        
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/943927.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号