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/