基本邏輯:
- 使用 testNG 在每個(gè) test 執(zhí)行前根據(jù) test case 生成文件名稱唧席,并且保存在MDC中(ThreadLocal)并且在執(zhí)行結(jié)束后刪除該文件名
- 使用 Log back 的 SiftingAppender 根據(jù)不同的線程將日志寫入不用的日志文件中
基本操作
- 編寫 TestNG Listener 編寫
- 引入 maven 依賴
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.3.0</version>
<scope>test</scope>
</dependency>
- 編寫 Listener, 重寫 beforeInvocation 和 afterInvocation 方法
package me.chan.service.user.test;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;
public class TestListener implements IInvokedMethodListener {
private static final Logger LOGGER = LoggerFactory.getLogger(TestListener.class);
@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
String fileName = testResult.getTestClass().getRealClass().getSimpleName() + "#" +
testResult.getName();
MDC.put("testcase", fileName);
LOGGER.debug("===============test started=================");
}
@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
Throwable error = testResult.getThrowable();
if (error != null) {
LOGGER.error("===============test failed=================", error);
}
LOGGER.debug("===============test ended=================");
MDC.remove("testcase");
}
}
- 配置 maven plugins
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<suiteXmlFiles>
<file>target/test-classes/testng.xml</file>
</suiteXmlFiles>
<properties>
<property>
<name>suitethreadpoolsize</name>
<value>1</value>
</property>
<property>
<name>listener</name>
<value>me.chan.TestListener</value>
</property>
</properties>
</configuration>
</plugin>
</plugins>
</build>
- 配置 logback 文件
(src/main/test/resources/logback.xml)
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true">
<appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator>
<key>testcase</key>
<defaultValue>unknown</defaultValue>
</discriminator>
<sift>
<!-- 讀取 TestListener 設(shè)置的 testcase 屬性-->
<appender name="FILE-${testcase}" class="ch.qos.logback.core.FileAppender">
<file>target/test-classes/logs/${testcase}.log</file>
<append>true</append>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d [%thread] %level %logger{35} - %msg%n</pattern>
</layout>
</encoder>
</appender>
</sift>
</appender>
<root level="DEBUG">
<appender-ref ref="SIFT"/>
</root>
</configuration>
- 配置 testng.xml
(src/main/test/resources/testng.xml)
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="my-test-suite">
<test name="testcase-1" parallel="false" enabled="true">
<classes>
<class name="me.chan.test.Test1"/>
</classes>
</test>
</suite>
- 運(yùn)行
mvn clean test