06.25 接口測試:Rest Assured+TestNg 實現持續集成的接口驅動自動化

接口測試:Rest Assured+TestNg 實現持續集成的接口驅動自動化

引言

之前一直使用Jmeter做接口測試,也圍繞Jmeter做了一些功能集成,比如:生成excle結果文件、數據庫斷言、自動提交缺陷、自動更新案例執行結果至Testlink等。雖說Jmeter簡單易上手,但大批量執行測試案例時,響應時間較長,這對向來追求測試效率的筆者而言,無疑是心頭之痛。

很早就聽說過Rest Assured,TestNg兩大框架,也看過一些相關的文章,但苦於各種原因,一直都是淺嘗輒止。這兩天心血來潮,嘗試使用Rest Assured+TestNg來實現數據驅動的接口測試,誰知不“嘗(試)”則已,一“嘗”驚人,實在是接口測試人員的福音。

框架介紹

  • Rest Assured

REST Assured是一個可以簡化HTTP Builder頂層,基於REST服務的測試過程的Java DSL(針對某一領域,具有受限表達性的一種計算機程序設計語言)。它支持發起POST,GET,PUT,DELETE,OPTIONS,PATCH和HEAD請求,並且可以用來驗證和校對這些請求的響應信息。

  • TestNg

TestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing (testing a class in isolation of the others) to integration testing (testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).

  • ReportNg

ReportNG is a simple HTML reporting plug-in for the TestNG unit-testing framework.

實現功能

  • 讀取excel測試案例數據。
  • 發送請求。
  • 斷言。
  • 生成測試報告。

實現步驟

1、代碼結構及案例模板

接口測試:Rest Assured+TestNg 實現持續集成的接口驅動自動化

代碼結構

接口測試:Rest Assured+TestNg 實現持續集成的接口驅動自動化

案例模板(部分字段預留後續使用)

2、新建maven項目並配置pom.xml

<project> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelversion>4.0.0/<modelversion>
<groupid>org.test.restassured/<groupid>
<artifactid>restassured/<artifactid>
<version>1.0-SNAPSHOT/<version>

<dependencies>
<dependency>
<groupid>io.rest-assured/<groupid>
<artifactid>rest-assured/<artifactid>
<version>3.1.0/<version>
<scope>test/<scope>
/<dependency>
<dependency>
<groupid>org.testng/<groupid>
<artifactid>testng/<artifactid>
<version>6.11/<version>
/<dependency>
<dependency>
<groupid>net.sourceforge.jexcelapi/<groupid>
<artifactid>jxl/<artifactid>
<version>2.6.12/<version>
/<dependency>

<dependency>
<groupid>org.uncommons/<groupid>
<artifactid>reportng/<artifactid>
<version>1.1.4/<version>
<scope>test/<scope>
<exclusions>
<exclusion>
<groupid>org.testng/<groupid>
<artifactid>testng/<artifactid>
/<exclusion>
/<exclusions>
/<dependency>

<dependency>
<groupid>com.google.inject/<groupid>
<artifactid>guice/<artifactid>
<version>4.0/<version>
/<dependency>
/<dependencies>
<build>
<plugins>

<plugin>
<groupid>org.apache.maven.plugins/<groupid>
<artifactid>maven-surefire-plugin/<artifactid>
<version>2.5/<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>
<forkmode>always/<forkmode>
/<configuration>
/<plugin>
/<plugins>
/<build>/<project>

3、配置ReportNg監聽

接口測試:Rest Assured+TestNg 實現持續集成的接口驅動自動化

reportNg監聽

4、讀取案例數據

由於TestNg的@DataProvider註釋返回的是二維數組,所以需讀取excel案例數據保存到一個二維數組。

public class ReadExcelCases { public static Object[][] readCases(String filePath) throws IOException, BiffException {
InputStream inputStream = new FileInputStream(filePath);
Workbook rwb = Workbook.getWorkbook(inputStream);
Sheet sheet = rwb.getSheet(0); int rsRows = sheet.getRows(); // 獲取總行數
int rsColums = sheet.getColumns();//獲取總列數
int countY = 0; for (int i = 1; i < rsRows; i++) { if(sheet.getCell(3, i).getContents().equals("Y")) //統計需要執行的案例數
countY++;
}
Object[][] cases = new Object[countY][rsColums]; int x =0; for (int i = 1; i < rsRows; i++) { if(sheet.getCell(3, i).getContents().equals("Y")){ //執行標識為“Y”才記錄數組
for (int j = 0; j < rsColums; j++) {
cases[x][j] = sheet.getCell(j, i).getContents();
}
x++;
}
} return cases;
}
}

TestNg的@Test傳參有多種方法,具體可百度,本例子使用@DataProvider來傳參。

public class CasesDataProvider { @DataProvider(name = "casesProvider") public static Object[][] caseProvider() throws IOException, BiffException {
String filePath = ".\\\\src\\\\test\\\\testCases\\\\發送短信.xls"; //測試案例相對路徑
Object[][] cases = ReadExcelCases.readCases(filePath); return cases;
}
}

5、執行案例

public class RunTest {


@BeforeClass
public void setUp() {
RestAssured.baseURI = "http://XX.XXX.XXX.XXX"; //請求IP
RestAssured.basePath = "v1/gateway.do";
RestAssured.port = 8187;
}
@Test(dataProvider = "casesProvider", dataProviderClass = CasesDataProvider.class)
public void runCases(String caseNo, String testPoit, String preResult, String YorN, String tableCheck, String appId, String merchantId, String api, String version, String phone, String bizTransaction, String acctType) { String bodyString = "{\\n" + "\\t\\"appId\\":\\"" + appId + "\\",\\n" + "\\t\\"api\\":\\"" + api + "\\",\\n" + "\\t\\"data\\":{\\n" + "\\t\\t\\"merchantId\\":\\"" + merchantId + "\\",\\n" + "\\t\\t\\"bizTransaction\\":\\"" + bizTransaction + "\\",\\n" + "\\t\\t\\"phone\\":\\"" + phone + "\\",\\n" + "\\t\\t\\"acctType\\":\\"" + acctType + "\\"\\n" + "\\t\\t},\\n" + "\\t\\"version\\":\\"" + version + "\\"\\n" + "}\\n";
Response response = given()
.contentType("application/json;charset=UTF-8")
.request()
.body(bodyString)
.post();
response.prettyPrint();//格式化響應報文
//斷言
String json = response.asString();
JsonPath jp = new JsonPath(json); if(response.statusCode() == 200){ //請求成功
Assert.assertEquals(jp.get("message").toString(),preResult);
}else{
Assert.assertEquals(jp.get("data.errMsg").toString(),preResult);
}
}
}

6、測試報告

當然,ReportNg測試報告支持自定義,百度還是好多資源的,後續筆者再做探究。

接口測試:Rest Assured+TestNg 實現持續集成的接口驅動自動化

測試報告

接口測試:Rest Assured+TestNg 實現持續集成的接口驅動自動化

測試報告

展望

以上只是Rest Assured+TestNg強大功能的冰山一角,後續再慢慢摸索。另外,既然邁出了這一步,那怎麼也得展望一下未來,打算後續搞個接口測試平臺玩玩。歡迎關注公眾號學習。


公眾號:軟件測試資源站(ID:testpu)

關注後私信回覆 入群,加入自學社群聯盟。


分享到:


相關文章: