TestNG+extentReports+log4j2 完善自动化测试框架——美观的报告和保留日志文件

本文介绍了如何在Appium+TestNG自动化测试框架基础上,通过ExtentReports美化测试报告,并利用Log4j2记录日志。详细讲述了引入依赖、重写监听器、配置log4j2.xml文件以及解决遇到的问题,如配置文件路径、XML错误和版本兼容问题等,旨在提升测试报告的可读性和日志管理的便利性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前搭的框架是Appium+TestNG的,执行成功与否全看TesNG控制台输出,很不方便,就准备加入日志和输出报告,看了一些参考,就准备采用extentReport来美化报告,使用log4j2来输出日志

Appium和testng就不再赘述了,前面我也写过Appium的环境搭建,这里主要写一下报告和日志模块

A、使用extentReports来美化报告

1、首先引入三个依赖:

<dependency>
    <groupId>com.relevantcodes</groupId>
    <artifactId>extentreports</artifactId>
    <version>2.41.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.vimalselvam/testng-extentsreport -->
<dependency>
    <groupId>com.vimalselvam</groupId>
    <artifactId>testng-extentsreport</artifactId>
    <version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.aventstack/extentreports -->
<dependency>
    <groupId>com.aventstack</groupId>
    <artifactId>extentreports</artifactId>
    <version>3.0.6</version>
</dependency>

2、重写testng监听类

package com.pick.tools;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.ResourceCDN;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.model.TestAttribute;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;
import org.testng.*;
import org.testng.xml.XmlSuite;

import java.io.File;
import java.util.*;

public class ExtentTestNGIReporterListener implements IReporter {
   
    static Date date = new Date();
    static String form = String.format("%tF", date);
    static String hour = String.format("%tH", date);
    static String minute = String.format("%tM", date);
    static String second = String.format("%tS", date);
    //生成的路径以及文件名
    private static final String OUTPUT_FOLDER = "test-output/";
    private static final String FILE_NAME = "index" + form + hour + minute + second + ".html";
    private ExtentReports extent;

    public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
   
        init();
        boolean createSuiteNode = false;
        if (suites.size() > 1) {
   
            createSuiteNode = true;
        }
        for (ISuite suite : suites) {
   
            Map<String, ISuiteResult> result = suite.getResults();
            //如果suite里面没有任何用例,直接跳过,不在报告里生成
            if (result.size() == 0) {
   
                continue;
            }
            //统计suite下的成功、失败、跳过的总用例数
            int suiteFailSize = 0;
            int suitePassSize = 0;
            int suiteSkipSize = 0;
            ExtentTest suiteTest = null;
            //存在多个suite的情况下,在报告中将同一个suite的测试结果归为一类,创建一级节点。
            if (createSuiteNode) {
   
                suiteTest = extent.createTest(suite.getName()).assignCategory(suite.getName());
            }
            boolean createSuiteResultNode = false;
            if (result.size() > 1) {
   
                createSuiteResultNode = true;
            }
            for (ISuiteResult r : result.values()) {
   
                ExtentTest resultNode;
                ITestContext context = r.getTestContext();
                if (createSuiteResultNode) {
   
                    //没有创建suite的情况下,将在SuiteResult的创建为一级节点,否则创建为suite的一个子节点。
                    if (null == suiteTest) {
   
                        resultNode = extent.createTest(r.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值