案例代码
import io.qameta.allure.*;
import io.qameta.allure.junit4.DisplayName;
import io.qameta.allure.model.Status;
import io.qameta.allure.model.StepResult;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
/**
* TODO
*
* @author TaoGe
* @version 1.0
* @date 2021/9/27 11:09
*/
public class TestAllure {
@Test
@DisplayName("测试用例名称")
@Description("测试用例描述")
@Link("https://www.baidu.com")
@Issue("bug编号110")
@Severity(SeverityLevel.MINOR)
public void demo01(){
login();
login();
StepResult stepResult = new StepResult();
stepResult.setStatus(Status.FAILED);
stepResult.setName("测试用例类名");
Allure.getLifecycle().startStep("细化",stepResult);
Allure.getLifecycle().stopStep();
assertThat(1, equalTo(2));
}
@Step("login步骤")
public void login(){
try {
Allure.addAttachment("图片","image/png",new FileInputStream("E:\\icon\\a.png"),".png");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("登录");
}
@Test
public void demo2(){
assertThat(2, equalTo(2));
}
@Test
public void demo3(){
assertThat(3, equalTo(4));
}
}
mvn clean test中文乱码
E:\源码\ceshi002>mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building ceshi002 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ ceshi002 ---
[INFO] Deleting E:\源码\ceshi002\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ceshi002 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ ceshi002 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ ceshi002 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\源码\ceshi002\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ ceshi002 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\源码\ceshi002\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.20:test (default-test) @ ceshi002 ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[INFO] Running TestAllure
鐧诲綍
鐧诲綍
[ERROR] Tests run: 3, Failures: 2, Errors: 0, Skipped: 0, Time elapsed: 1.974 s <<< FAILURE! - in TestAllure
[ERROR] demo01(TestAllure) Time elapsed: 1.627 s <<< FAILURE!
java.lang.AssertionError:
Expected: <2>
but: was <1>
at TestAllure.demo01(TestAllure.java:37)
[ERROR] demo3(TestAllure) Time elapsed: 0.002 s <<< FAILURE!
java.lang.AssertionError:
Expected: <4>
but: was <3>
at TestAllure.demo3(TestAllure.java:60)
[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] TestAllure.demo01:37
Expected: <2>
but: was <1>
[ERROR] TestAllure.demo3:60
Expected: <4>
but: was <3>
[INFO]
[ERROR] Tests run: 3, Failures: 2, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 18.470 s
[INFO] Finished at: 2021-09-27T13:46:35+08:00
[INFO] Final Memory: 15M/129M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.20:test (default-test) on project ceshi002: There are test failures.
[ERROR]
[ERROR] Please refer to E:\源码\ceshi002\target\surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date]-jvmRun[N].dump, [date].dumpstream and [date]-jvmRun[N].dumpstream.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
IDEA生成allure测试报告中文乱码
E:\源码\ceshi002>allure serve allure-results
Generating report to temp directory...
Report successfully generated to C:\Users\shenyf\AppData\Local\Temp\4653105515360773502\allure-report
Starting web server...
2021-09-27 13:47:10.490:INFO::main: Logging initialized @7678ms to org.eclipse.jetty.util.log.StdErrLog
Server started at <http://169.254.188.171:57332/>. Press <Ctrl+C> to exit
解决办法:
pom.xml文件:
<properties>
<aspectj.version>1.8.10</aspectj.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
问题解决后显示正常:
E:\源码\ceshi002>mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building ceshi002 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ ceshi002 ---
[INFO] Deleting E:\源码\ceshi002\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ceshi002 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ ceshi002 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ ceshi002 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\源码\ceshi002\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ ceshi002 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\源码\ceshi002\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.20:test (default-test) @ ceshi002 ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[INFO] Running TestAllure
登录
登录
[ERROR] Tests run: 3, Failures: 2, Errors: 0, Skipped: 0, Time elapsed: 2.16 s <<< FAILURE! - in TestAllure
[ERROR] demo01(TestAllure) Time elapsed: 1.767 s <<< FAILURE!
java.lang.AssertionError:
Expected: <2>
but: was <1>
at TestAllure.demo01(TestAllure.java:37)
[ERROR] demo3(TestAllure) Time elapsed: 0.006 s <<< FAILURE!
java.lang.AssertionError:
Expected: <4>
but: was <3>
at TestAllure.demo3(TestAllure.java:60)
[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] TestAllure.demo01:37
Expected: <2>
but: was <1>
[ERROR] TestAllure.demo3:60
Expected: <4>
but: was <3>
[INFO]
[ERROR] Tests run: 3, Failures: 2, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.854 s
[INFO] Finished at: 2021-09-27T13:49:50+08:00
[INFO] Final Memory: 16M/165M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.20:test (default-test) on project ceshi002: Ther
e are test failures.
[ERROR]
[ERROR] Please refer to E:\源码\ceshi002\target\surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date]-jvmRun[N].dump, [date].dumpstream and [date]-jvmRun[N].dumpstream.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
E:\源码\ceshi002>allure serve allure-results
Generating report to temp directory...
Report successfully generated to C:\Users\shenyf\AppData\Local\Temp\4653105515360773502\allure-report
Starting web server...
2021-09-27 13:47:10.490:INFO::main: Logging initialized @7678ms to org.eclipse.jetty.util.log.StdErrLog
Server started at <http://169.254.188.171:57332/>. Press <Ctrl+C> to exit
最终pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cn.test</groupId>
<artifactId>ceshi002</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<aspectj.version>1.8.10</aspectj.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-junit4</artifactId>
<version>2.15.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<testFailureIgnore>false</testFailureIgnore>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
</argLine>
<properties>
<property>
<name>listener</name>
<value>io.qameta.allure.junit4.AllureJunit4</value>
</property>
</properties>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>