TestNG 是自動化測試框架,比Junit更強大,可用於單元測試,功能測試,端到端測試等,下面我們就介紹一下如何基於IntelliJ IDEA 創建TestNG test
1. 打開IntelliJ IDEA -> Create New Project -> Next -> Next -> 編寫項目名稱 -> Finished
2. 打開https://mvnrepository.com/artifact/org.testng/testng/7.0.0 下載testng-7.0.0.jar 文件,放到項目文件夾中。
3. File -> Project Structure... -> Modules -> Dependencies -> Add JARs or directories 添加testng-7.0.0.jar -> Apply -> OK
4. 創建測試用例
package com.testng.demo; import org.testng.Assert; import org.testng.annotations.Test; public class Test1 { @Test public void case1(){ Assert.assertEquals(1,1); } @Test public void case2(){ Assert.assertEquals("hello","hello");; } }
5. Add Configuration... -> Add New Configuration -> TestNG
6. 自定義名稱 Testng -> 選擇測試類 如Test1 -> OK
7. Listeners -> Use default reporters -> Apply -> OK
8. 運行Testng
問題:報錯
Exception in thread "main"
java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException
解決方法:
缺少jcommander的jar包導致,按照步驟2下載jcommander,加入到依賴中即可。
下載地址如下:
https://mvnrepository.com/artifact/com.beust/jcommander/1.81