簡介:
語言:java
框架:SpringBoot
集成開發工具:IntelliJ IDEA
測試框架:TestNG
項目管理工具:maven
本章重點介紹TestNG的使用。
一.testNG介紹
TestNG是Java中的一個測試框架, 類似於JUnit 和NUnit, 功能都差不多, 只是功能更加強大,使用也更方便
Java中已經有一個JUnit的測試框架了。 TestNG比JUnit功能強大的多。 測試人員一般用TestNG來寫自動化測試。 開發人員一般用JUnit寫單元測試。
官方網站: http://testng.org/doc/index.html
二、TestNG的優點
2.1 漂亮的HTML格式測試報告
2.2 支持並發測試
2.3 參數化測試更簡單
2.4 支持輸出日志
2.5 支持更多功能的注解
三、編寫TestNG測試用例的步驟
3.1 使用 Eclipse生成TestNG的測試程序框架
3.2 在生成的程序框架中編寫測試代碼邏輯
3.3 根據測試代碼邏輯,插入TestNG注解標簽
3.4 配置Testng.xml文件,設定測試類、測試方法、測試分組的執行信息
3.5 執行TestNG的測試程序
四、TestNG安裝使用
工程的pom.xml中添加如下內容即可:
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.10</version> </dependency>
示例代碼:
package com.huaxi.huaxitest; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test;
import com.huaxi.base.BaseTest public class TestNGCase extend BaseTest{ @BeforeClass public void testBefore() throws Exception{ System.out.println("this is before class"); } @Test public void test002() throws Exception{ System.out.println("this is TestNG test case"); } @AfterClass public void testAfter() throws Exception{ System.out.println("this is after class"); } }
需要繼承AbstractTestNGSpringContextTests
package com.huaxi.base; import com.huaxi.huaxitest.HuaxitestApplication; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; @SpringBootTest(classes = HuaxitestApplication.class) public class BaseTest extends AbstractTestNGSpringContextTests { }
運行結果截圖:
五、TestNG常用注解
注解 |
描述 |
@BeforeSuite |
注解的方法將只運行一次,運行所有測試前此套件中。 |
@AfterSuite |
注解的方法將只運行一次此套件中的所有測試都運行之后。 |
@BeforeClass |
注解的方法將只運行一次先行先試在當前類中的方法調用。 |
@AfterClass |
注解的方法將只運行一次后已經運行在當前類中的所有測試方法。 |
@BeforeTest |
注解的方法將被運行之前的任何測試方法屬於內部類的 <test>標簽的運行。 |
@AfterTest |
注解的方法將被運行后,所有的測試方法,屬於內部類的<test>標簽的運行。 |
@BeforeGroups |
組的列表,這種配置方法將之前運行。此方法是保證在運行屬於任何這些組第一個測試方法,該方法被調用。 |
@AfterGroups |
組的名單,這種配置方法后,將運行。此方法是保證運行后不久,最后的測試方法,該方法屬於任何這些組被調用。 |
@BeforeMethod |
注解的方法將每個測試方法之前運行。 |
@AfterMethod |
被注釋的方法將被運行后,每個測試方法。 |
@DataProvider |
標志着一個方法,提供數據的一個測試方法。注解的方法必須返回一個Object[] [],其中每個對象[]的測試方法的參數列表中可以分配。 該@Test 方法,希望從這個DataProvider的接收數據,需要使用一個dataProvider名稱等於這個注解的名字。 |
@Factory |
作為一個工廠,返回TestNG的測試類的對象將被用於標記的方法。該方法必須返回Object[]。 |
@Listeners |
定義一個測試類的監聽器。 |
@Parameters |
介紹如何將參數傳遞給@Test方法。 |
@Test |
標記一個類或方法作為測試的一部分。 |
六、TestNG忽略測試
不想運行某個用例時,可以在測試用例加上@Test(enable = false), 來禁用此測試用例
package com.huaxi.huaxitest; import org.testng.annotations.Test; public class TestNGCase { @Test public void test001() throws Exception{ System.out.println("this is TestNG test case"); } @Test(enabled = false) public void test002() throws Exception{ System.out.println("忽略這個用例"); } }
七、TestGN分組測試
package com.huaxi.huaxitest; import org.testng.annotations.Test; public class TestNGCase { @Test(groups = "group1") public void test001() throws Exception{ System.out.println("一組用例"); } @Test(groups = "group2") public void test002() throws Exception{ System.out.println("二組用例"); } }
八、Test.xml標簽
1、選擇一個類中的全部測試腳本
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MySute">
<test name="TestCaseClass">
<classes>
<class name="com.huaxi.huaxitest.TestNGCase" />
</classes>
</test>
</suite>
2、選擇一個類中的部分測試腳本
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="MySute"> <test name="TestCaseClass"> <classes> <class name="com.huaxi.huaxitest.TestNGCase" > <methods> <include name="test001"/> </methods> </class> </classes> </test> </suite>
3、選擇一個類中的某些組
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="MySute"> <test name="TestCaseClass"> <groups> <run> <include name="group1"/> </run> </groups> <classes> <class name="com.huaxi.huaxitest.TestNGCase"></class> </classes> </test> </suite>
九、Idea+maven+testng+reportng生成測試報告
1、在pom.xml文件中添加maven的依賴包
<dependencies>注意在這個節點下添加下面的依賴包</dependencies>
<dependency> <groupId>org.uncommons</groupId> <artifactId>reportng</artifactId> <version>1.1.4</version> <scope>test</scope> </dependency> <dependency> <groupId>com.google.inject</groupId> <artifactId>guice</artifactId> <version>4.0</version> <scope>test</scope> </dependency>
2、再在pom.xml文件添加插件
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.17</version> <configuration> <suiteXmlFiles> <suiteXmlFile>xmlfile/testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <properties> <property> <name>usedefaultlisteners</name> <value>false</value> </property> <property> <name>listener</name> <value>org.uncommons.reportng.HTMLReporter, org.uncommons.reportng.JUnitXMLReporter</value> </property> </properties> <workingDirectory>target/</workingDirectory> </configuration> </plugin>
<build> <plugins> 上面的內容是這個節點下添加,不要添加錯位置了 </plugins> </build>
3、在testng.xml中配置監聽器
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="MySute"> <test name="TestCaseClass"> <classes> <class name="com.huaxi.huaxitest.TestNGCase"></class> </classes> </test> <listeners> <listener class-name="org.uncommons.reportng.HTMLReporter"/> <listener class-name="org.uncommons.reportng.JUnitXMLReporter"/> </listeners> </suite>
4、IDEA配置
在Idea中打開Run-Edit Configurations...
在Listeners標簽下勾選“Use default reporters”
最后運行testng.xml,自動生成test-output目錄,在html目錄下找到index.html
打開index.html
參考文檔:
https://blog.csdn.net/df0128/article/details/83243822
https://www.cnblogs.com/seven7777/p/9517186.html