接口测试流程
接口测试框架
练习
- testerhome: https://testerhome.com/api/v3/topics.json
- 雪球的api
- 企业微信
- 思寒的演示代码: https://github.com/seveniruby/RestAssuredDemo1222
- post演练环境: http://jenkins.testing-studio.com:8080/login?from=%2F
首个测试用例
import org.junit.Test;
import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;
public class TesterHomeTest {
@Test
public void topics(){
useRelaxedHTTPSValidation();
get("https://testerhome.com/api/v3/topics.json")
.then()
.body("topics[0].title", containsString("上海沙龙"));
}
}
请求发送
- get
- post
- param、 queryParam、formParam
- head cookie
- log、proxy
html断言
.body("html.head.title", equalTo("mp3_百度搜索"))
.body("**.find{it.@class=='nums_text'}", equalTo("百度为您找到相关结果约56,500,000个"))
.body(hasXPath("//*[@class='nums_text' and contains(text(), '百度为您找到相关结果约56,500,000个')]"))
JsonPath
@Test
public void jsonPathDemo(){
String title = "[北京沙龙] TesterHome 北京沙龙第 11 期,开始报名~";
given().when().get("https://testerhome.com/api/v3/topics.json")
.then()
.statusCode(200)
.body("topics.title[0]", equalTo(title))
.body("topics.size()", equalTo(23))
.body("topics.find {it.title.contains('北京沙龙')}.title", equalTo(title))
.body("topics.findAll {it.title.contains('北京沙龙')}.title[0]", equalTo(title))
;
}
XmlPath断言
@Test
public void xmlPathDemo(){
given()
.when().get("http://jenkins.testing-studio.com:8080/job/AllureDemo/api/xml")
.then()
.statusCode(200)
.body("freeStyleProject.displayName", equalTo("AllureDemo"))
.body("..lastBuild.number", equalTo("16"))
.body("..lastBuild.number.toFloat()", equalTo(16f))
.body("**.find {it.name()=='lastSuccessfulBuild'}.number", equalTo("1"))
.body("**.find {it.name()=='lastSuccessfulBuild'}.number.toInteger()", equalTo(1))
.body("**.findAll {it.number=='1'}[-1].url", equalTo("http://jenkins.testing-studio.com:8080/job/AllureDemo/1/"))
;
}
数字断言
@Test
public void hamcrestDemo(){
given()
.when().get("http://jenkins.testing-studio.com:8080/job/AllureDemo/api/xml")
.then()
.statusCode(200)
.body("..lastBuild.number.toFloat()", greaterThanOrEqualTo(16f))
.body("..lastBuild.number.toInteger()", greaterThanOrEqualTo(16))
.body("..lastBuild.number.toDouble()", closeTo(16,2))
.body("**.find {it.name()=='lastSuccessfulBuild'}.number", equalTo("1"))
.body("**.find {it.name()=='lastSuccessfulBuild'}.number.toInteger()", equalTo(1))
;
}
更多断言
JsonSchema
- 分析目标json
- 根据目标json生成json schema https://www.jsonschema.net/
- 对已有的json schema进行修改,加最大值,最小值、正则匹配
- 使用schema断言
@Test
public void schema(){
given().when().get("https://testerhome.com/api/v3/topics/6040.json")
.then()
.body(matchesJsonSchema(new File("/tmp/2.json")));
}
作业1
编写测试用例,实现发起get请求,搜索“霍格沃兹 测试学院”,断言状态码是否正确
作业2
编写post测试用例,登陆雪球,输入错误的用户名密码即可
作业3
找出来testerhome帖子回复数大于等于6的所有帖子数是不是等于特定数量
作业4
http://jenkins.testing-studio.com:8080/job/AllureDemo/api/xml 判断构建号>16并且节点名字等于build的job的url地址
作业5
http://jenkins.testing-studio.com:8080/job/AllureDemo/api/json 编写json schema校验用例
TODO
- xml请求例子
- pb例子
- 微信小程序