rest-assured实现java接口自动化测试
1.导包
导入import io.restassured.response.Response;
2.使用
-
2.1 请求四要素:请求方式,请求路径,请求参数,请求头 given() 在什么场景下(请求头,请求参数,cookies等等) when() 执行什么操作(get,post) then() 判断结果(显示日志,返回接口的结果)
-
2.2 获取响应结果
then().extract().response()提取返回结果
3.示例
https://httpbin.org是一个用于测试HTTP请求和响应的在线平台。
3.1 get
@Test
public void test1(){ given(). queryParam( "phone", "122325235"). queryParam( "password", "123456"). when (). get("``https://httpbin.org/get``"). then(). log().body(); }
3.2 post 表单参数
@Test public void test2(){ given(). contentType("application/x-www-form-urlencoded"). body("hello=123&world=456"). when(). post("``https://httpbin.org/post``"). then(). log().body(); }
3.3 post json参数
@Test public void test3(){ given(). contentType("application/json"). body("{\"hello\":\"123\",\"world\":\"456\"}"). when(). post("``https://httpbin.org/post``"). then(). log().body(); }
3.4 post 文件上传
@Test(invocationCount = 2) public void test4(){ Response res = given(). contentType("multipart/form-data"). multiPart(new File("F:\\hp\\Pictures\\001.jpg")). when(). post("``https://httpbin.org/post``"). then(). log().body().extract().response();//提取返回结果 // System.out.println(res); System.out.println(res.time()); System.out.println(res.statusCode()); System.out.println(res.jsonPath().get("headers.Content-Type")); }

浙公网安备 33010602011771号