实例GitHub:https://github.com/reese0329/rest_assured
dubbo协议
rpc
service
params
return
协议的共同点
- protocal
http
soap
dubbo
- target
host port serviceName
- request
结构化
schema
- response
结构化
schema
使用代理分析常见的协议
启动代理工具开放代理端口
设置app或web浏览器的代理地址
如果是https需要导入证书
执行测试行为
从代理工具中分析接口数据
11:00
burpsuite
演练
Android 4.4
32‘
使用自定义代理解析特殊协议
流量转发
协议解包
协议封包
原理是tcp proxy或者http proxy
参考工具 em-proxy nc
接口测试的质量目标
- 质量保证维度
功能正常。保持新老版本的兼容
性能正常。单词请求的响应跟总体qps相关
变更检测。字段的缺失,字段的类型变更
- 质量监控体系
构建接口层的快速问题的质量保证体系
构建接口监控体系
34:50
接口测试用例编写
接口测试流程
接口的分析
接口分析
接口测试框架
测试用例维护
持续集成
质量监控
接口测试框架选择
jmeter
soapUI
Robotframework
swagger
RestAssured
37‘
RestAssured
简约的接口测试dsl
支持xml 寄送的结构化解析
支持xpath jsonpath gpath 等多种解析方式
对spring的支持比较全面
根据自己的原因提子自己封装
需要满足接口测试的几大功能点
文档(最好读一遍,对接口测试有好处)
https://github.com/rest-assured/rest-assured
43'
创建maven,在pom.xml添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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>demoTest</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.0.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>3.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>spring-mock-mvc</artifactId>
<version>3.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>5.0.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>1.8.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>groovy-all</artifactId>
<version>1.8.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
reimport 依赖
创建测试目录
get请求
package restassured;
import io.restassured.config.HttpClientConfig;
import io.restassured.response.Response;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.params.CoreConnectionPNames;
import org.junit.*;
import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;
public class Demo {
@Test
public void baidu() {
given()
.log().all()
.queryParam("wd", "mp3")
.when()
.get("http://www.baidu.com/s")
.then()
.log().all()
.statusCode(200)
.body("html.head.title", equalTo("mp3_百度搜索"));
}
}
given 参数信息
when触发条件 get/post请求
then断言
.body 对返回内容做断言
.statusCode 对返回状态做断言 (返回内容为html,则使用html语法)
返回结果
错误的断言
package restassured;
import io.restassured.config.HttpClientConfig;
import io.restassured.response.Response;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.params.CoreConnectionPNames;
import org.junit.*;
import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;
public class Demo {
@Test
public void baidu() {
given()
.log().all()
.queryParam("wd", "mp3")
.when()
.get("http://www.baidu.com/s")
.then()
.log().all()
.statusCode(200)
.body("html.head.title", equalTo("mp4_百度搜索"));
}
//test
}
返回
50‘
用例编写(演练)
- 创建maven项目
- 添加依赖 restassured + junit/testng
- 编写用例
- 添加断言
- 调试
Post请求实例
借助代理抓包工具验证接口请求是否可以成功发送
burp suite 右键 copy as curl commands
粘贴执行
验证接口可以重复执行
可执行无报错,但是有问题,貌似没有真正发送成功
package restassured;
import io.restassured.config.HttpClientConfig;
import io.restassured.response.Response;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.params.CoreConnectionPNames;
import org.junit.*;
import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;
public class Demo {
@Test
//post请求
public void testerhome() {
given().log().all()
.header("Content-Type", "application/x-www-form-urlencoded")
.header("Cookie", "ll=\"108288\"; bid=ufm8svqInPI; __utmc=30149280; douban-fav-remind=1; gr_user_id=4171796e-842c-4c4a-90a0-26e6738a48d0; _vwo_uuid_v2=DCD0C06E626E1D6DFE13B60EAD6341A39|d72f1edb0d54ead74d312fdccc346905; __yadk_uid=D5htNM4K5N9sqGUCr3fbvz5kAD82ZqNh; push_noty_num=0; push_doumail_num=0; __utmv=30149280.4603; __utmz=30149280.1554109383.4.2.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; dbcl2=\"46030598:5+UMwWFA/7I\"; ck=rwMr; ap_v=0,6.0; ct=y; _pk_ref.100001.8cb4=%5B%22%22%2C%22%22%2C1554692809%2C%22https%3A%2F%2Faccounts.douban.com%2Fpassport%2Flogin_popup%3Flogin_source%3Danony%22%5D; _pk_id.100001.8cb4=159c3a6479396543.1553674557.11.1554692809.1554687227.; _pk_ses.100001.8cb4=*; __utma=30149280.526818714.1553674558.1554690313.1554692810.14; __utmt=1; __utmb=30149280.2.10.1554692810")
.formParam("ck", "aIY0")
.formParam("comment", "test19")
.when()
.log().all()
.post("https://www.douban.com/");
// .then()
// .log().all()
// .body("success", equalTo(true));
}
}
1‘04’
数据驱动
https://github.com/junit-team/junit4/wiki/Parameterized-tests
有问题,大概这个意思
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class FibonacciTest {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }
});
}
@Parameter // first data value (0) is default
public /* NOT private */ int fInput;
@Parameter(1)
public /* NOT private */ int fExpected;
@Test
public void test() {
assertEquals(fExpected, Fibonacci.compute(fInput));
}
}
public class Fibonacci {
...
}
package restassured;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;
@RunWith(Parameterized.class)
public class Demo {
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{0, 0}, {1, 1}, {2, 1}, {3, 2}, {4, 3}, {5, 5}, {6, 8}
});
}
// @Parameterized.Parameter
//导入一维数组
public /* NOT private */ int fInput;
// @Parameterized.Parameter
//二维数组
public /* NOT private */ int fExpected;
@Test
//get 请求
public void baidu() {
given()
.log().all()
.queryParam("wd", fInput)
.when()
.get("http://www.baidu.com/s")
.then()
.log().all()
.statusCode(200)
.body("html.head.title", equalTo(fExpected));
}
}
重命名参数
@Parameterized.Parameters(name ="{index}:baidu search wd={0} expected={1}")
package restassured;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized.Parameter;
import java.util.Arrays;
import java.util.Collection;
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;
@RunWith(Parameterized.class)
public class Demo {
@Parameterized.Parameters(name ="{index}:baidu search wd={0} expected={1}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{0, 0}, {1, 1}, {2, 1}, {3, 2}, {4, 3}, {5, 5}, {6, 8}
});
}
// @Parameterized.Parameter
//导入一维数组
public /* NOT private */ int fInput;
// @Parameterized.Parameter
//二维数组
public /* NOT private */ int fExpected;
@Test
//get 请求
public void baidu() {
given()
.log().all()
.queryParam("wd", fInput)
.when()
.get("http://www.baidu.com/s")
.then()
.log().all()
.statusCode(200)
.body("html.head.title", equalTo(fExpected));
}
}
.log().all()
打印所有的网络请求日志
可以自己再封装一个小框架
读数据驱动,读xml,读数据库,远程文件