REST API自动化测试(十五)
前面的系列文章主要是详细的介绍了Rest-Assured针对不同请求方法发送网络请求的过程。下来
针对具体的业务案例来说明下Rest-Assured在实际企业中它的API自动化测试实战部分。首先在API
自动化测试中都会遇到登录授权部分需要处理,也就是动态参数,这部分的处理思路其实很简单,就
是获取到动态参数后,把它写到文件中,然后使用的时候读取出来,这部分实现的源码如下:
package utils;
/*
*动态参数编写到文件中
* */
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class Public
{
/*
* 把内容写到具体文件中
* */
public void writeContent(String filePath,String fileName,String content) throws Exception
{
File file=new File(filePath+fileName);
FileWriter fileWriter=new FileWriter(file);
fileWriter.write(content);
fileWriter.close();
}
/*
* 从文件中读取文件内容
* */
public String readFile(String filePath,String fileName) throws Exception
{
String content=null;
File file=new File(filePath+fileName);
FileReader fileReader=new FileReader(file);
char byt[]=new char[1024];
content=new String(byt,0,fileReader.read(byt));
return content;
}
}
下来实现针对API自动化测试的测试用例编写的过程,具体实现的代码如下:
package test.automation.api;
import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.json.JSONArray;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import utils.Public;
import java.util.HashMap;
public class TestAssuredApiTest
{
Public aPublic=new Public();
@BeforeMethod
public void setUp() throws Exception
{
RestAssured.baseURI="http://47.95.142.233:8000";
this.login();
}
public Response login() throws Exception
{
RequestSpecification httpRequest=RestAssured.given();
httpRequest.header("Content-Type","application/json;charset=UTF-8");
JSONObject params=new JSONObject();
params.put("username","13484545195");
params.put("password","asd888");
httpRequest.body(params.toString());
Response response=httpRequest.request(Method.POST,"/login/auth/");
aPublic.writeContent("data/","token",response.jsonPath().get("token"));
return response;
}
public String getToken() throws Exception
{
return aPublic.readFile("data/","token");
}
public void headers(RequestSpecification httpRequest) throws Exception
{
String s4="JWT %s";
httpRequest.header("Authorization",String.format(s4,this.getToken()));
httpRequest.header("Content-Type","application/json;charset=UTF-8");
}
@Test(description = "登录成功的验证")
public void test_login_success() throws Exception
{
Response response=this.login();
HashMap<String,Object> hashMap=response.jsonPath().getJsonObject("user");
JSONObject jsonObject=new JSONObject(hashMap);
Assert.assertEquals(response.statusCode(),200);
Assert.assertEquals(jsonObject.get("username"),"无涯");
Assert.assertEquals(jsonObject.get("is_active"),true);
}
@Test(description = "登录成功后的首页信息")
public void test_login_index() throws Exception
{
RequestSpecification httpRequest=RestAssured.given();
this.headers(httpRequest);
Response response=httpRequest.request(Method.GET,"/interface/index");
HashMap<String,Integer> hashMap=response.jsonPath().getJsonObject("count");
JSONObject jsonObject=new JSONObject(hashMap);
Assert.assertEquals(response.statusCode(),200);
Assert.assertEquals(jsonObject.get("product"),0);
}
public Response addProduct() throws Exception
{
RequestSpecification httpRequest=RestAssured.given();
this.headers(httpRequest);
httpRequest.header("Content-Type","application/json;charset=UTF-8");
JSONObject params=new JSONObject();
params.put("name","无涯课堂");
params.put("product_type","WEB");
params.put("version","admin");
params.put("master","admin");
params.put("description","admin");
httpRequest.body(params.toString());
Response response=httpRequest.request(Method.POST,"/interface/product/");
aPublic.writeContent("data/","productID", Integer.toString(response.jsonPath().get("id")));
return response;
}
public Response setProductName(String name) throws Exception
{
RequestSpecification httpRequest=RestAssured.given();
this.headers(httpRequest);
JSONObject params=new JSONObject();
params.put("name",name);
params.put("product_type","WEB");
params.put("version","admin");
params.put("master","admin");
params.put("description","admin");
String productID=aPublic.readFile("data/","productID");
params.put("id",productID);
httpRequest.body(params.toString());
String s4="/interface/product/%s/";
Response response=httpRequest.request(Method.PUT,String.format(s4,productID));
return response;
}
public Response delProduct() throws Exception
{
String productID=aPublic.readFile("data/","productID");
RequestSpecification httpRequest=RestAssured.given();
this.headers(httpRequest);
String s5="/interface/product/%s/";
String baseUrl=String.format(s5,productID);
Response response=httpRequest.request(Method.DELETE,String.format(s5,productID));
return response;
}
@Test(description = "添加产品信息")
public void test_add_product() throws Exception
{
Response response=this.addProduct();
this.delProduct();
Assert.assertEquals(response.statusCode(),201);
Assert.assertEquals(response.jsonPath().get("name"),"无涯课堂");
}
@Test(description = "修改产品名称信息")
public void test_set_product_name() throws Exception
{
this.addProduct();
Response response=this.setProductName("网易云课堂");
String jsonStr=response.body().asString();
this.delProduct();
Assert.assertEquals(response.statusCode(),200);
JSONObject jsonObject=new JSONObject(jsonStr);
Assert.assertEquals(jsonObject.get("id"),Integer.parseInt(aPublic.readFile("data/","productID")));
Assert.assertEquals(jsonObject.get("name"),"网易云课堂");
}
@Test(description = "验证删除产品信息")
public void test_del_product() throws Exception
{
this.addProduct();
Response response=this.delProduct();
Assert.assertEquals(response.statusCode(),204);
}
@Test(description = "精确搜索产品信息")
public void test_so_product() throws Exception
{
this.addProduct();
RequestSpecification httpRequest=RestAssured.given();
this.headers(httpRequest);
Response response=httpRequest.request(Method.GET,"/interface/products?name=无涯课堂");
String jsonStr=response.getBody().asString();
JSONArray jsonArray=new JSONArray(jsonStr);
JSONObject jsonObject=jsonArray.getJSONObject(0);
this.delProduct();
Assert.assertEquals(response.statusCode(),200);
Assert.assertEquals(jsonObject.get("name"),"无涯课堂");
Assert.assertEquals(jsonObject.get("product_type"),"WEB");
Assert.assertEquals(jsonObject.get("master"),"admin");
}
@Test(description = "默认查询方式")
public void test_default_so() throws Exception
{
this.addProduct();
RequestSpecification httpRequest=RestAssured.given();
this.headers(httpRequest);
Response response=httpRequest.request(Method.GET,"/interface/products?name=");
this.delProduct();
String jsonStr=response.getBody().asString();
JSONArray jsonArray=new JSONArray(jsonStr);
JSONObject jsonObject=jsonArray.getJSONObject(0);
Assert.assertEquals(response.statusCode(),200);
Assert.assertEquals(jsonObject.get("name"),"无涯课堂");
}
@Test(description = "验证模糊查询")
public void test_obscure_so() throws Exception
{
this.addProduct();
RequestSpecification httpRequest=RestAssured.given();
this.headers(httpRequest);
Response response=httpRequest.request(Method.GET,"/interface/products?name=无涯");
this.delProduct();
String jsonStr=response.getBody().asString();
JSONArray jsonArray=new JSONArray(jsonStr);
JSONObject jsonObject=jsonArray.getJSONObject(0);
Assert.assertEquals(response.statusCode(),200);
Assert.assertEquals(jsonObject.get("name"),"无涯课堂");
}
}
上面的测试代码主要结合具体的业务场景来编写的API自动化测试用例,在这个过程中也是保持了每个自动化测试
用例的独立性,这样设计的好处就是虽然业务之间是有关联的,但是编写的自动化测试用例是不能依赖的,要保持
自动化测试用例的独立性。
感谢您的阅读,后续文章持续编写Java技术栈的自动化测试体系文章。
欢迎关注微信公众号“Python自动化测试”

浙公网安备 33010602011771号