Pandaria是一款基於Cucumber JVM的API自動化測試工具,上一篇文章介紹了它的基本功能,包括基本的HTTP操做和數據庫操做。本文將介紹使用Pandaria編寫自動化測試的一些高級用法。java
Pandaria自己以Java庫的形式發佈到Maven Central中,使用Pandaria自己只須要在構建工具中添加依賴便可,自己沒有額外的安裝操做。工欲善其事必先利其器,IDE可以使咱們編寫自動化測試事半功倍,此處推薦IntelliJ IDEA, 固然你能夠選擇其餘你喜歡的用於開發Java的IDE。git
安裝好IntelliJ IDEA後,咱們能夠建立一個標準的Maven工程。github
在build.gradle或者pom.xml中添加Pandaria依賴。數據庫
build.gradleexpress
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.xmljson
<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,你能夠不添加這兩個模塊。api
本文使用JUnit, 依賴中也添加了cucumber-junit模塊。bash
在工程下面建立一個RunCucumberTest.java,這個文件使用Cucumber的Junit Runner,用於運行feature文件,使用Pandaria其實就是使用Cucumber,全部Cucumber自己的功能依然適用。app
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會高亮以及自動補全:
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 """
複製代碼
除了關係型數據庫之外,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是否遵循給定的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來進行,使用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
複製代碼
使用Pandaria,結合Junit,運行測試就像運行單元測試同樣,你只須要在CI上運行mvn test
便可。
Pandaria自己採用TDD方式開發,其本身的測試自己就是示例,可到源碼src/test/resources/features
下面查看更多示例。
本文只對一些進階功能作基本介紹,具體使用請參考使用文檔