使用Pandaria編寫API自動化測試進階用法
ofollow,noindex">Pandaria 是一款基於Cucumber JVM的API自動化測試工具,上一篇文章介紹了它的基本功能,包括基本的HTTP操作和資料庫操作。本文將介紹使用Pandaria編寫自動化測試的一些高階用法。
快速開始
安裝
Pandaria本身以Java庫的形式釋出到Maven Central中,使用Pandaria本身只需要在構建工具中新增依賴即可,本身沒有額外的安裝操作。工欲善其事必先利其器,IDE能夠使我們編寫自動化測試事半功倍,此處推薦IntelliJ IDEA, 當然你可以選擇其他你喜歡的用於開發Java的IDE。
建立工程
安裝好IntelliJ IDEA後,我們可以建立一個標準的Maven工程。

新增依賴
在build.gradle或者pom.xml中新增Pandaria依賴。
build.gradle
apply plugin: 'java' dependencies { testCompile( "io.cucumber:cucumber-junit:4.0.0", 'com.github.jakimli.pandaria:pandaria-core:0.2.3', 'com.github.jakimli.pandaria:pandaria-db:0.2.3', 'com.github.jakimli.pandaria:pandaria-mongo:0.2.3' ) } 複製程式碼
pom.xml
<dependencies> <dependency> <groupId>com.github.jakimli.pandaria</groupId> <artifactId>pandaria-core</artifactId> <version>0.2.3</version> <scope>test</scope> </dependency> <dependency> <groupId>com.github.jakimli.pandaria</groupId> <artifactId>pandaria-db</artifactId> <version>0.2.3</version> <scope>test</scope> </dependency> <dependency> <groupId>com.github.jakimli.pandaria</groupId> <artifactId>pandaria-mongo</artifactId> <version>0.2.3</version> <scope>test</scope> </dependency> </dependencies> 複製程式碼
這裡除了pandaria-core以外,還包含了pandaria-db和pandaria-mongo兩個模組,如果你的專案不需要驗證資料庫,或者不需要驗證mongo db,你可以不新增這兩個模組。
本文使用JUnit, 依賴中也添加了cucumber-junit模組。
建立Cucumber Junit入口
在工程下面建立一個RunCucumberTest.java,這個檔案使用Cucumber的Junit Runner,用於執行feature檔案,使用Pandaria其實就是使用Cucumber,所有Cucumber本身的功能依然適用。
RunCucumberTest.java
package com.github.jakimli.pandaria_sample; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; import org.junit.runner.RunWith; @RunWith(Cucumber.class) @CucumberOptions(plugin = "pretty", features = "classpath:features/", glue = {"com.github.jakimli.pandaria", "com.github.jakimli.pandaria_sample"}) public class RunCucumberTest { } 複製程式碼
語法高亮和補全
現在可以開始編寫第一個自動化測試了。
在 src/test/resource/features
下面建立以 .feature
結尾的檔案,如test.feature。IntelliJ IDEA利用gherkin和Cucumber for Java兩個外掛提供gherkin的高亮和補全。可以在IntelliJ的外掛安裝中搜索這兩個外掛並安裝,安裝好後feature會高亮以及自動補全:

HTTP
全域性Http Header
Pandaria支援在配置檔案( application.properties
)中配置全域性的Http Header,所有的Http Request都會帶上這些Header。
一個典型的場景是,自動化測試執行在測試環境,當需要對API進行認真的時候,通常需要一個測試賬號,將對應的認證資訊放到HTTP的Authorization Header中。我們可以使用 http.headers.<name>
來配置。如:
application.properties
http.headers.Authorization=Bear Token 複製程式碼
上傳檔案
Pandaria支援檔案上傳,使用 attachment
關鍵字可以指定檔案路徑
Scenario: upload file * uri: /files * attachment: attachments/abc.txt * send: POST * status: 200 * response body: """ uploaded """ 複製程式碼
Mongo DB
除了關係型資料庫以外,Pandaria還支援對Mongo DB的操作和校驗。
插入
通常我們需要往mongo的一個集合中插入測試資料,可以這麼寫:
* collection: 'users' insert: """ {"user": "jakim"} """ 複製程式碼
清除
清除測試資料:
* collection: 'users' clear 複製程式碼
查詢和驗證
同樣我們可以從Mongo DB中查詢資料並做校驗
* collection: 'users' find all * verify: '$[0].user'="alice" 複製程式碼
也可以指定查詢條件
* collection: 'users' find: """ {"age": {$gt: 17}} """ * verify: '$[0].user'="jakim" * collection: 'users' find: """ {"age": {$lt: 17}} """ * verify: '$[0].user'="alice" * collection: 'users' find: filter/greater_than_17.json * verify: '$[0].user'="jakim" 複製程式碼
變數
普通變數
使用Pandaria,你可以使用基本的變數功能, ${<name>}
用於使用普通變數如:
你可以定義 envrionment
變數,然後在之後的URL中使用,這樣如果你需要切換環境,就只需要改envrionment變數的值就好了。
Background: * var: 'environment'='test' Scenario: hello world * uri: https://${environment}/users/octocat/orgs * send: GET * status: 200 複製程式碼
你也可以在配置檔案中指定變數的初始值
application.properties
variables.environment=test 複製程式碼
Scenario: initial value from configuration file * verify: ${environment}="test" * var: 'environment'="production" * verify: ${environment}="production" 複製程式碼
如上述,在feature檔案中定義會覆蓋配置檔案中的值。
生成隨機測試資料
隨機的測試資料在自動化測試中很實用,Pandaria中你可以使用 #{<expression>}
的形式來生成測試資料,如:
Scenario: faker in request body as doc string * uri: /faker/users * request body: """ {"name": "#{Name.fullName}", "city": "#{Address.city}"} """ * send: POST * response body: """ success """ 複製程式碼
這裡的 #{Name.fullName}
和 #{Address.city}
會被替換成隨機的人名和城市名。通過配置 faker.locale
可以切換語言。
上一次返回報文作為下一次請求報文
Pandaria支援將第一次HTTP請求的返回內容直接作為下一個請求的Request內容, 通過 @{<json path>}
的形式使用。
Scenario: request directly from last response * uri: /users/me * send: get * verify: '$.username'='jakim' * verify: '$.age'=18 * verify: '$.iq'=double: 80.0 * uri: /users * request body: """ { "username": @{$.username}} """ * send: POST * status: 200 * verify: '$.id'='auto-generated' * verify: '$.username'='jakim' * verify: '$.age'=18 * uri: /users * request body: """ @{$} """ * send: POST * status: 200 * verify: '$.id'='auto-generated' * verify: '$.username'='jakim' * verify: '$.age'=18 複製程式碼
校驗
驗證JSON Schema
你可以驗證一段JSON是否遵循給定的Json shcema:
* uri: /products/1 * send: get * verify: '$' conform to: """ { "$schema": "http://json-schema.org/draft-07/schema#", "$id": "http://example.com/product.schema.json", "title": "Product", "description": "A product in the catalog", "type": "object" } """ * verify: '$' conform to: schema/product.schema.json * verify: '$.tags' conform to: """ { "$schema": "http://json-schema.org/draft-07/schema#", "$id": "http://example.com/product.tags.schema.json", "title": "product tags", "description": "Product tags", "type": "array", "items": { "type": "string" } } """ 複製程式碼
使用Javascript自定義驗證
一些基本的驗證可以通過Javascript來進行,使用 code
關鍵字,如:
* var: 'age'=16 * var: 'iq'=90.0 * uri: http://localhost:10080/not_important * send: get * verify: '$.age'=code: ${age} + 2 * verify: '$.iq'=code: ${iq} - 10 * verify: '$.age'!=code: ${age} + 3 * verify: '$.iq'!=code: ${iq} - 11 * verify: '$.age'=code: """ ${age} + 2 """ * verify: '$.iq'=code: """ ${iq} - 10 """ 複製程式碼
或者
* verify code: ${name} == ${iq} / 3 * verify code: """ ${name} != ${iq} % 3 """ * verify code file: verification.js 複製程式碼
整合CI
使用Pandaria,結合Junit,執行測試就像執行單元測試一樣,你只需要在CI上執行 mvn test
即可。