一、方案
需求目标:测试左移,测试介入研发过程,验证单接口正常及异常逻辑
选用工具:Java、SpringBoot、Maven、TestNG、httpclient、Allure、Jenkins
方案:创建测试接口测试工程,参照研发设计文档和设计思路,编写正常及异常用例,直接调用服务端接口,覆盖接口逻辑和验证异常处理,提升接口健壮性。
二、项目结构及代码说明
2.1 项目结构
2.2 代码示例
接口脚本举例说明:AddCustomerTest
点击查看代码
package cases.crc.customer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import config.TestConfig;
import model.CrcCustomerCase;
import model.InterfaceName;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.ibatis.session.SqlSession;
import org.json.simple.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import utils.ConfigFile;
import utils.DatabaseUtil;
import java.io.IOException;
import java.util.Random;
public class AddCustomerTest {
@BeforeTest
public void beforeTest(){
TestConfig.defaultHttpClient = new DefaultHttpClient();
TestConfig.crcCustomerCreateUrl = ConfigFile.getUrl(InterfaceName.ADDCUSTOMER);
}
@Test(groups = "addCustomerSuccess",description = "新增客户成功")
public void addCustomerSuccess() throws IOException {
// 使用HttpClient调用接口
HttpPost post = new HttpPost(TestConfig.crcCustomerCreateUrl);
//设置请求头信息 设置header,并且绕过token校验
post.setHeader("content-type","application/json");
post.setHeader("Authorization","Bearer test1");
// 设置json参数
JSONObject param = new JSONObject();
String param1 = randomNumber();
param.put("customerName",param1);
param.put("contacts","hqq");
param.put("mobile","13000001111");
param.put("address","上海市闵行区");
param.put("zipcode","310030");
param.put("areacode",360425);
param.put("status",0);
param.put("bankAccount","123456");
param.put("legalPerson","hqq");
param.put("usci",param1);
param.put("description","自动化接口测试客户");
//将参数信息添加到方法中
StringEntity entity = new StringEntity(param.toString(),"utf-8");
post.setEntity(entity);
//执行post方法
HttpResponse response = TestConfig.defaultHttpClient.execute(post);
//声明一个对象来进行响应结果的存储,获取响应结果
String strResult = EntityUtils.toString(response.getEntity(),"utf-8");
// 根据用户名数据库查询客户id
SqlSession session = DatabaseUtil.getSqlSession();
CrcCustomerCase o = session.selectOne("getCustomerInfoByCustomerName", param1);
int customerId = o.getId();
// 将json格式的字符串转换成json对象,便于断言
ObjectMapper mapper = new ObjectMapper();
try{
JsonNode jsonNode = mapper.readTree(strResult);
Assert.assertEquals(jsonNode.get("code").asInt(),0);
Assert.assertEquals(jsonNode.get("data").asInt(),customerId);
}catch (Exception e){
System.out.println("json转换失败");
e.printStackTrace();
}
}
@Test(groups = "addCustomerFaile",description = "新增客户失败,客户名称存在")
public void addCustomerFailed01() throws IOException{
// 使用HttpClient调用接口
HttpPost post = new HttpPost(TestConfig.crcCustomerCreateUrl);
//设置请求头信息 设置header,并且绕过token校验
post.setHeader("content-type","application/json");
post.setHeader("Authorization","Bearer test1");
// 设置json参数
JSONObject param = new JSONObject();
param.put("customerName","格力集团");
param.put("status",0);
//将参数信息添加到方法中
StringEntity entity = new StringEntity(param.toString(),"utf-8");
post.setEntity(entity);
//执行post方法
HttpResponse response = TestConfig.defaultHttpClient.execute(post);
//声明一个对象来进行响应结果的存储,获取响应结果
String strResult = EntityUtils.toString(response.getEntity(),"utf-8");
// 将json格式的字符串转换成json对象,便于断言
ObjectMapper mapper = new ObjectMapper();
try{
JsonNode jsonNode = mapper.readTree(strResult);
Assert.assertEquals(jsonNode.get("code").asInt(),1001001012);
Assert.assertEquals(jsonNode.get("msg").asText(),"客户名称已存在");
}catch (Exception e){
System.out.println("json转换失败");
e.printStackTrace();
}
}
public String randomNumber(){
String res = "Auto-";
Random random = new Random();
for (int i = 0; i < 6; i++) {
int randomInt = random.nextInt(10);
res += randomInt;
}
System.out.println("生成的随机字符串:" + res);
return res;
}
}
三、项目集成和测试报告
3.1 准备工作
1)插件安装 allure
2)Jenkins:系统管理--》全局工具配置
3.2 jenkins新建自由风格的项目:esmc-test-autotest
1)源码:配置gitlab上的源码地址、用户名密码、分支等
2)maven命令:mvn clean test 清理target/目录下的文件,并执行src/test/下的测试用例
3)执行mvn test后,会生成allure-results文件夹存放生成的json报告
4)需要配置Allure,查看allure-results/的测试报告