TestNG :JAVA一個主流的測試框架,引入了註解、依賴、分組等便於使用的新功能。
Rest Assured :基於REST服務的測試過程的Java DSL,支持GET、POST等請求,且可以通過斷言驗證響應的信息。
測試類demo
創建一個測試類:TestDemo()
可以通過下面的demo,體會到註解的執行順序:
package com.course.demo;
import org.testng.annotations.*;
public class TestDemo {
@Test
public void testcase1(){
System.out.println("執行了 >>> testcase1");
}
@Test
public void testcase2(){
System.out.println("執行了 >>> testcase2");
}
@BeforeTest
public void beforeTest(){
System.out.println("執行了 >>> beforeTest");
}
@AfterTest
public void afterTest(){
System.out.println("執行了 >>> afterTest");
}
@BeforeMethod
public void beforeMethod(){
System.out.println("執行了 >>> beforeMethod");
}
@AfterMethod
public void afterMethod(){
System.out.println("執行了 >>> afterMethod");
}
@BeforeClass
public void beforeClass(){
System.out.println("執行了 >>> beforeClass");
}
@AfterClass
public void afterClass(){
System.out.println("執行了 >>> afterClass");
}
@BeforeSuite
public void beforeSuite(){
System.out.println("執行了 >>> beforeSuite");
}
@AfterSuite
public void AfterSuite(){
System.out.println("執行了 >>> AfterSuite");
}
}
run之後結果如下:
[TestNG] Running:
執行了 >>> beforeSuite
執行了 >>> beforeTest
執行了 >>> beforeClass
執行了 >>> beforeMethod
執行了 >>> testcase1
執行了 >>> afterMethod
執行了 >>> beforeMethod
執行了 >>> testcase2
執行了 >>> afterMethod
執行了 >>> afterClass
執行了 >>> afterTest
執行了 >>> AfterSuite
===============================================
Default Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
生成report
當case量累積到成百上千時,測試報告的作用就凸現出來了。
Python的unittest測試框架,或者UI的Katalon,都可以生成對應的report,TestNG也可以通過配置生成一份美觀的測試報告。
pom.xml文件裡配置生成測試報告所需的依賴:
<dependency>
<groupid>com.relevantcodes/<groupid>
<artifactid>extentreports/<artifactid>
<version>2.41.1/<version>
/<dependency>
<dependency>
<groupid>com.vimalselvam/<groupid>
<artifactid>testng-extentsreport/<artifactid>
<version>1.3.1/<version>
/<dependency>
<dependency>
<groupid>com.aventstack/<groupid>
<artifactid>extentreports/<artifactid>
<version>3.0.6/<version>
/<dependency>
引入一個生成測試報告report的文件:ExtentTestNGIReporterListener.java
通過配置文件resources下的testng.xml配置計劃執行哪些case,以及使用的監聽:
<suite>
<test>
<classes>
<class>
<methods>
<include>
<include>
/<methods>
/<class>
/<classes>
/<test>
<listeners>
<listener>
/<listeners>
/<suite>
為了看到測試報告的效果,修改TestDemo.java裡的兩個測試case,分別加上斷言:
@Test
public void testcase1(){
System.out.println("執行了 >>> testcase1");
Assert.assertEquals(1,1);
}
@Test
public void testcase2(){
System.out.println("執行了 >>> testcase2");
Assert.assertEquals(1,2);
}
run一下testng.xml,測試報告生成路徑,以及文件名稱均可以通過修改ExtentTestNGIReporterListener.java文件自定義
用瀏覽器打開測試報告,展示效果如下:
引入Rest Assured
引入Rest Assured測試框架
pom.xml引入依賴:
<dependency>
<groupid>io.rest-assured/<groupid>
<artifactid>rest-assured/<artifactid>
<version>3.0.3/<version>
/<dependency>
<dependency>
<groupid>io.rest-assured/<groupid>
<artifactid>json-path/<artifactid>
<version>3.0.3/<version>
/<dependency>
<dependency>
<groupid>io.rest-assured/<groupid>
<artifactid>xml-path/<artifactid>
<version>3.0.3/<version>
/<dependency>
修改測試類,用Rest Assured寫一個測試case,並忽略之前的兩個case:
@Test(enabled = false)
public void testcase1(){
System.out.println("執行了 >>> testcase1");
}
@Test(enabled = false)
public void testcase2(){
System.out.println("執行了 >>> testcase2");
}
@Test(priority = 1)
public void testBaidu(){Response response =
given().
when().
get("https://www.baidu.com").
then().log().ifError().
statusCode(200).
extract().response();
System.out.println("百度首頁接口返回信息 >>> "+response.asString());
}
run之後結果如下:
[TestNG] Running:
/Users/apple/Library/Caches/IdeaIC2019.1/temp-testng-customsuite.xml
執行了 >>> beforeSuite
執行了 >>> beforeTest
執行了 >>> beforeClass
執行了 >>> beforeMethod
百度首頁接口返回信息 >>>
<title>百度一下,你就知道/<title>...
執行了 >>> afterMethod
執行了 >>> afterClass
執行了 >>> afterTest
執行了 >>> AfterSuite
===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
maven編譯
testng.xml 配置新增的測試case:testBaidu()
通過配置pom.xml實現maven編譯:
<build>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins/<groupid>
<artifactid>maven-surefire-plugin/<artifactid>
<version>2.7.1/<version>
<configuration>
<testfailureignore>true/<testfailureignore>
<suitexmlfiles>
<suitexmlfile>
./src/main/resources/testng.xml
/<suitexmlfile>
/<suitexmlfiles>
/<configuration>
/<plugin>
/<plugins>
/<build>
命令行執行mvn clean package:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
[TestNGContentHandler]
執行了 >>> beforeSuite
執行了 >>> beforeTest
執行了 >>> beforeClass
執行了 >>> beforeMethod
百度首頁接口返回信息 >>>
<title>百度一下,你就知道/<title>...
執行了 >>> afterMethod
執行了 >>> afterClass
執行了 >>> afterTest
執行了 >>> AfterSuite
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.889 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ Chapter1 ---
[INFO] Building jar: /Users/apple/Downloads/TestNG/Chapter1/target/Chapter1-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.015 s
[INFO] Finished at: 2019-10-09T11:55:34+08:00
[INFO] ------------------------------------------------------------------------
這樣可以集成到jenkins裡,通過設置定時任務執行接口測試腳本。
閱讀更多 GT七妖妖 的文章