项目github地址:

https://github.com/tianchiTester/API_AutoFramework

这套框架的报告是自己封装的

1.测试基类TestBase:

接口请求的testcase类需要继承此类去读取properties文件

package com.qa.base;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties; public class TestBase {
//这个类作为所有接口请求的父类,加载读取properties文件
public Properties prop;
//构造函数
public TestBase(){
try{
prop = new Properties();
FileInputStream fis = new FileInputStream(System.getProperty("user.dir")+"/src/main/java/com/qa/config/config.properties");
prop.load(fis);
}catch(FileNotFoundException f){
f.printStackTrace();
}catch (IOException i){
i.printStackTrace();
}
}
}

2.配置文件

配置文件里存放项目的endpoint,可以通过修改endpoint进行环境的切换

测试数据存放读取excel的地址

tokenpath存放jsonpath的所在路径用于需要token才能调用的接口

#项目的根url(endpoint)
Host= https://xxx.com #测试数据excel地址
postdata = xxx
getdata = xxx #通过jsonpath获取返回结果token的所在路径
tokenPath = xxx

3.Excel文件中测试数据存放接口地址endpoint后面的url

4.report样式,我使用的是extentreprot插件,实现方式是实现testNG的IReporter接口,再通过testng.xml中listener标签进行监听

package com.qa.report;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.ResourceCDN;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.model.TestAttribute;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import org.testng.*;
import org.testng.xml.XmlSuite; import java.io.File;
import java.util.*; public class ExtentTestNGReporterListener implements IReporter{ //生成的路径以及文件名
private static final String OUTPUT_FOLDER = "test-output/";
private static final String FILE_NAME = "index.html";
private ExtentReports extent; @Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
init();
boolean createSuiteNode = false;
if(suites.size()>1){
createSuiteNode=true;
}
for (ISuite suite : suites) {
Map<String, ISuiteResult> result = suite.getResults();
//如果suite里面没有任何用例,直接跳过,不在报告里生成
if(result.size()==0){
continue;
}
//统计suite下的成功、失败、跳过的总用例数
int suiteFailSize=0;
int suitePassSize=0;
int suiteSkipSize=0;
ExtentTest suiteTest=null;
//存在多个suite的情况下,在报告中将同一个一个suite的测试结果归为一类,创建一级节点。
if(createSuiteNode){
suiteTest = extent.createTest(suite.getName()).assignCategory(suite.getName()); }
boolean createSuiteResultNode = false;
if(result.size()>1){
createSuiteResultNode=true;
}
for (ISuiteResult r : result.values()) {
ExtentTest resultNode;
ITestContext context = r.getTestContext();
if(createSuiteResultNode){
//没有创建suite的情况下,将在SuiteResult的创建为一级节点,否则创建为suite的一个子节点。
if( null == suiteTest){
resultNode = extent.createTest(r.getTestContext().getName());
}else{
resultNode = suiteTest.createNode(r.getTestContext().getName());
}
}else{
resultNode = suiteTest;
}
if(resultNode != null){
resultNode.getModel().setName(suite.getName()+" : "+r.getTestContext().getName());
if(resultNode.getModel().hasCategory()){
resultNode.assignCategory(r.getTestContext().getName());
}else{
resultNode.assignCategory(suite.getName(),r.getTestContext().getName());
}
resultNode.getModel().setStartTime(r.getTestContext().getStartDate());
resultNode.getModel().setEndTime(r.getTestContext().getEndDate());
//统计SuiteResult下的数据
int passSize = r.getTestContext().getPassedTests().size();
int failSize = r.getTestContext().getFailedTests().size();
int skipSize = r.getTestContext().getSkippedTests().size();
suitePassSize += passSize;
suiteFailSize += failSize;
suiteSkipSize += skipSize;
if(failSize>0){
resultNode.getModel().setStatus(Status.FAIL);
}
resultNode.getModel().setDescription(String.format("Pass: %s ; Fail: %s ; Skip: %s ;",passSize,failSize,skipSize));
}
buildTestNodes(resultNode,context.getFailedTests(), Status.FAIL);
buildTestNodes(resultNode,context.getSkippedTests(), Status.SKIP);
buildTestNodes(resultNode,context.getPassedTests(), Status.PASS);
}
if(suiteTest!= null){
suiteTest.getModel().setDescription(String.format("Pass: %s ; Fail: %s ; Skip: %s ;",suitePassSize,suiteFailSize,suiteSkipSize));
if(suiteFailSize>0){
suiteTest.getModel().setStatus(Status.FAIL);
}
} } extent.flush();
} private void init() {
//文件夹不存在的话进行创建
File reportDir= new File(OUTPUT_FOLDER);
if(!reportDir.exists()&& !reportDir .isDirectory()){
reportDir.mkdir();
}
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUTPUT_FOLDER + FILE_NAME);
htmlReporter.config().setDocumentTitle("api自动化测试报告");
htmlReporter.config().setReportName("api自动化测试报告"); htmlReporter.config().setChartVisibilityOnOpen(true);
htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
// htmlReporter.config().setTheme(Theme.STANDARD);
htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);
htmlReporter.config().setCSS(".node.level-1 ul{ display:none;} .node.level-1.active ul{display:block;}");
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
extent.setReportUsesManualConfiguration(true);
} private void buildTestNodes(ExtentTest extenttest,IResultMap tests, Status status) {
//存在父节点时,获取父节点的标签
String[] categories=new String[0];
if(extenttest != null ){
List<TestAttribute> categoryList = extenttest.getModel().getCategoryContext().getAll();
categories = new String[categoryList.size()];
for(int index=0;index<categoryList.size();index++){
categories[index] = categoryList.get(index).getName();
}
} ExtentTest test; if (tests.size() > 0) {
//调整用例排序,按时间排序
Set<ITestResult> treeSet = new TreeSet<ITestResult>(new Comparator<ITestResult>() {
@Override
public int compare(ITestResult o1, ITestResult o2) {
return o1.getStartMillis()<o2.getStartMillis()?-1:1;
}
});
treeSet.addAll(tests.getAllResults());
for (ITestResult result : treeSet) {
Object[] parameters = result.getParameters();
String name="";
//如果有参数,则使用参数的toString组合代替报告中的name
// for(Object param:parameters){
// name+=param.toString();
// }
//如果有参数只取第一个参数作test-name
for(int i=0;i<parameters.length;i++){
name = parameters[0].toString();
} if(name.length()>0){
if(name.length()>100){
name= name.substring(0,100)+"...";
}
}else{
name = result.getMethod().getMethodName();
}
if(extenttest==null){
test = extent.createTest(name);
}else{
//作为子节点进行创建时,设置同父节点的标签一致,便于报告检索。
test = extenttest.createNode(name).assignCategory(categories);
}
//test.getModel().setDescription(description.toString());
//test = extent.createTest(result.getMethod().getMethodName());
for (String group : result.getMethod().getGroups())
test.assignCategory(group); List<String> outputList = Reporter.getOutput(result);
for(String output:outputList){
//将用例的log输出报告中
test.debug(output);
}
if (result.getThrowable() != null) {
test.log(status, result.getThrowable());
}
else {
test.log(status, "Test " + status.toString().toLowerCase() + "ed");
} test.getModel().setStartTime(getTime(result.getStartMillis()));
test.getModel().setEndTime(getTime(result.getEndMillis()));
}
}
} private Date getTime(long millis) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millis);
return calendar.getTime();
}
}

5.接口请求方法

封装post方法, 普通get方法,需要header的get方法,delete方法

 package com.qa.restclient;

 import org.apache.http.client.ClientProtocolException;

 import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.log4j.Logger; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; public class RestClient {
//get接口带header
public CloseableHttpResponse getApi(String url , Map<String,String> map) throws IOException{
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet get = new HttpGet(url); for(Map.Entry<String,String> header: map.entrySet() ){
get.addHeader(header.getKey(),header.getValue());
}
CloseableHttpResponse httpResponse = httpClient.execute(get);
return httpResponse;
} //普通get接口
public CloseableHttpResponse getApi(String url) throws IOException{
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet get = new HttpGet(url);
CloseableHttpResponse httpResponse = httpClient.execute(get);
System.out.println(httpResponse);
return httpResponse;
} //post接口
public CloseableHttpResponse postApi(String url, String entityString, HashMap<String,String> headermap) throws ClientProtocolException, IOException {
//创建一个可关闭的HttpClient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new StringEntity(entityString));
//加载请求头到httppost对象
for(Map.Entry<String, String> entry : headermap.entrySet()) {
httppost.addHeader(entry.getKey(), entry.getValue());
}
//发送post请求
CloseableHttpResponse httpResponse = httpclient.execute(httppost);
return httpResponse;
} //delete方法
public CloseableHttpResponse deleteApi(String url) throws ClientProtocolException, IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpDelete httpdel = new HttpDelete(url);
//发送delete请求
CloseableHttpResponse httpResponse = httpclient.execute(httpdel);
return httpResponse;
}
}

6.测试用例

7.工具类

getToken:传入接口请求返回结果对象和jsonpath路径,获取token

dtt : 读取excel中的数据,传入excel路径和excel的sheet id

getstauteCode:获取返回结果的状态码

 package com.qa.util;

 import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ReadContext; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap; public class TestUtil { //获取返回的token ,使用JsonPath获取json路径
public static HashMap<String,String> getToken(CloseableHttpResponse closeableHttpResponse,String jsonPath) throws Exception{
HashMap<String,String> responseToken = new HashMap<String, String>();
String responseString = EntityUtils.toString( closeableHttpResponse.getEntity(),"UTF-8");
ReadContext ctx = JsonPath.parse(responseString);
String Token = ctx.read(jsonPath); //"$.EFPV3AuthenticationResult.Token"
if(null == Token||"".equals(Token)){
new Exception("token不存在");
} responseToken.put("x-ba-token",Token);
return responseToken;
} //遍历excel,sheet参数
public static Object[][] dtt(String filePath,int sheetId) throws IOException{ File file = new File(filePath);
FileInputStream fis = new FileInputStream(file); XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sh = wb.getSheetAt(sheetId);
int numberrow = sh.getPhysicalNumberOfRows();
int numbercell = sh.getRow(0).getLastCellNum(); Object[][] dttData = new Object[numberrow][numbercell];
for(int i=0;i<numberrow;i++){
if(null==sh.getRow(i)||"".equals(sh.getRow(i))){
continue;
}
for(int j=0;j<numbercell;j++) {
if(null==sh.getRow(i).getCell(j)||"".equals(sh.getRow(i).getCell(j))){
continue;
}
XSSFCell cell = sh.getRow(i).getCell(j);
cell.setCellType(CellType.STRING);
dttData[i][j] = cell.getStringCellValue();
}
} return dttData;
} //获取状态码
public static int getStatusCode(CloseableHttpResponse closeableHttpResponse){
int StatusCode = closeableHttpResponse.getStatusLine().getStatusCode();
return StatusCode;
} }

原文地址https://blog.csdn.net/qq_34693151/article/details/81867044

接口自动化框架(java)--1.项目概述的更多相关文章

  1. 接口自动化框架(java)--5.通过testng.xml生成extentreport测试报告

    这套框架的报告是自己封装的 由于之前已经通过Extentreport插件实现了Testng的IReport接口,所以在testng.xml中使用listener标签并指向实现IReport接口的那个类 ...

  2. 接口自动化框架(java)--4.接口Token传递

    这套框架的报告是自己封装的 一般token会在登录接口返回结果中呈现,从代码层面获取token的方式有很多种,我是使用jsonpath这个json路径语言去匹配token所在路径的key值 packa ...

  3. 接口自动化框架(java)--3.get,delete请求,Excel管理多种请求类型

    这套框架的报告是自己封装的 每种请求类型放入不同的sheet中,就可以避免新建太多的excel去做数据驱动. XSSFSheet类提供了一个读取sheet的方法,getSheetAt(int),通过下 ...

  4. 接口自动化框架(java)--2.接口用例POST请求,参数配置

    这套框架的报告是自己封装的 Post类型的接口通常有请求参数,请求参数也是json类型,所以需要写一个类将请求参数序列化成json对象 以常见的登录接口为例 新建一个package,和postPara ...

  5. Jmeter+ant+Jenkins接口自动化框架搭建

    摘自:https://testerhome.com/topics/13389 一.背景  上一篇讲了Jmeter 接口自动化-脚本数据分离实例,我们知道怎么利用Jmeter去编写接口自动化脚本,但是接 ...

  6. Jmeter+Ant+Jenkins接口自动化框架

    最近应公司要求,搭建一套接口自动化环境.看到通知邮件,没有多想就确定了Jmeter路线.可能有些人会 说,为啥不用python,相对而言高大上一些.因为公司内部现在项目有用到Jmeter,正好可以结合 ...

  7. python+request接口自动化框架

    python+request接口自动化框架搭建 1.数据准备2.用python获取Excel文件中测试用例数据3.通过requests测试接口4.根据接口返回的code值和Excel对比 但本章只讲整 ...

  8. 转载:python + requests实现的接口自动化框架详细教程

    转自https://my.oschina.net/u/3041656/blog/820023 摘要: python + requests实现的接口自动化框架详细教程 前段时间由于公司测试方向的转型,由 ...

  9. python + requests实现的接口自动化框架详细教程

    前段时间由于公司测试方向的转型,由原来的web页面功能测试转变成接口测试,之前大多都是手工进行,利用postman和jmeter进行的接口测试,后来,组内有人讲原先web自动化的测试框架移驾成接口的自 ...

随机推荐

  1. SQL窗口函数RANK(),Dense_Rank(),row_number(),NTILE()

    数据源 CREATE TABLE student( no int, ca ), name ), subject ), scorce int ); /* 数据 */ , ); , ); , ); , ) ...

  2. 这篇文章主要介绍了Citrix XenServer 6.1 安装图解教程

    本次为使用VirtualBox虚拟机过安装测试机过程,我们在使用Vm(无论是Vbox还是VMware等)我们的CPU都必须可支持Intel-V或AMD-V,并且在VM软件设置和BIOS设置开启虚拟化支 ...

  3. PSO:利用PSO算法优化二元函数,寻找最优个体适应度—Jason niu

    figure [x,y] = meshgrid(-5:0.1:5,-5:0.1:5); z = x.^2 + y.^2 - 10*cos(2*pi*x) - 10*cos(2*pi*y) + 20; ...

  4. JS对象的拷贝

    1:对数据进行备份的时候,如果这个数据是基本的数据类型,那么很好办,通过赋值实现复制即可. 赋值与浅拷贝的区别 var obj1 = { 'name' : 'zhangsan', 'age' : '1 ...

  5. 大数据项目之_15_电信客服分析平台_01&02_项目背景+项目架构+项目实现+数据生产+数据采集/消费(存储)

    一.项目背景二.项目架构三.项目实现3.1.数据生产3.1.1.数据结构3.1.2.编写代码3.1.3.打包测试3.2.数据采集/消费(存储)3.2.1.数据采集:采集实时产生的数据到 kafka 集 ...

  6. 移动端click事件出现300ms延迟

    问题分析: 双击缩放是指手在屏幕上快速点击两次,iOS自带的Safari浏览器会将网页缩放至原始比例.当用户在屏幕上单击某元素时,浏览器会先捕获此处单击,但浏览器不知道用户是要单击链接还是要双击该部分 ...

  7. WPF:通过名字找控件

    var plotBorder = (Border)FindName(string.Format("PlotBorder{0}", i))

  8. 【Codeforces】【网络流】【树链剖分】【线段树】ALT (CodeForces - 786E)

    题意 现在有m个人,每一个人都特别喜欢狗.另外还有一棵n个节点的树. 现在每个人都想要从树上的某个节点走到另外一个节点,且满足要么这个人自带一条狗m,要么他经过的所有边h上都有一条狗. 2<=n ...

  9. CodeForces - 1025C 字符串处理,画一个圆。。。

    题目链接: https://vjudge.net/problem/1810469/origin 题目大意: 给你一个字符串,中间切一刀,左右两边均反转,然后右边的串拼接到左边上. 思路: 比如  aa ...

  10. Python开发技术详解PDF

    Python开发技术详解(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1F5J9mFfHKgwhkC5KuPd0Pw 提取码:xxy3 复制这段内容后打开百度网盘手 ...