TestNg + Allure 生成测试报告
完成后结果如图
介绍
- TestNg 是基于Junit 的测试框架
- Allure 监听测试信息,生成报告
使用
- 因项目需要,我也是第一次编写测试用例,并生成报告,在网上找的这套方案.
TestNg
- 项目导入依赖
<!--测试相关--> <dependency> <groupId>io.qameta.allure</groupId> <artifactId>allure-testng</artifactId> <version>2.0-BETA18</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.10</version> </dependency>
- maven 编译插件
<!--testNG + allure 生成测试报告--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <configuration> <skipTests>true</skipTests> <argLine> -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/1.9.2/aspectjweaver-1.9.2.jar" </argLine> </configuration> <dependencies> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.2</version> </dependency> </dependencies> </plugin>
- 编写测试用例,注意@Test 注解使用testNg 相关包
@WebAppConfiguration() @RunWith(SpringRunner.class) @SpringBootTest @Slf4j public class TestNg extends AbstractTestNGSpringContextTests { @Test(description = "testNg 通过") public void testSuccess() { Assert.assertEquals(1, 1); System.out.println("测试通过"); } }
Allure 报告工具使用
-
安装 : 需要node 环境,安装命令,allure 需要jdk 1.8及以上
npm install -g allure-commandline --save-dev
-
配置文件 allure.properties ,可以定义报告文件输出路径
# 定义输出在项目 target 目录 allure.results.directory=target/allure-results
-
allure 命令.输入 allure -h 查看
Cai:bxadex-deal miya$ allure -help Could not parse arguments: Expected a command, got -help Usage: allure [options] [command] [command options] Options: --help Print commandline help. -q, --quiet Switch on the quiet mode. Default: false -v, --verbose Switch on the verbose mode. Default: false --version Print commandline version. Default: false Commands: generate Generate the report Usage: generate [options] The directories with allure results Options: -c, --clean Clean Allure report directory before generating a new one. Default: false --config Allure commandline config path. If specified overrides values from --profile and --configDirectory. --configDirectory Allure commandline configurations directory. By default uses ALLURE_HOME directory. --profile Allure commandline configuration profile. -o, --report-dir, --output The directory to generate Allure report into. Default: allure-report serve Serve the report Usage: serve [options] The directories with allure results Options: --config Allure commandline config path. If specified overrides values from --profile and --configDirectory. --configDirectory Allure commandline configurations directory. By default uses ALLURE_HOME directory. -h, --host This host will be used to start web server for the report. -p, --port This port will be used to start web server for the report. Default: 0 --profile Allure commandline configuration profile. open Open generated report Usage: open [options] The report directory Options: -h, --host This host will be used to start web server for the report. -p, --port This port will be used to start web server for the report. Default: 0 plugin Generate the report Usage: plugin [options] Options: --config Allure commandline config path. If specified overrides values from --profile and --configDirectory. --configDirectory Allure commandline configurations directory. By default uses ALLURE_HOME directory. --profile Allure commandline configuration profile.
-
找到serve 服务相关操作, 可以使用 -p 指定端口启动,不然每次启动都是随机端口
allure serve -p target/allure-results/