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

由于之前已经通过Extentreport插件实现了Testng的IReport接口,所以在testng.xml中使用listener标签并指向实现IReport接口的那个类就可以替换原始的testngreport

testng配置如下:

单suite,单test

test name 指向你写的testCase,methods放入需要执行的方法

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="test" verbose="1" preserve-order="true" parallel="false">
<test name="testCase1">
<classes>
<class name="com.qa.tests.testCase1">
<methods>
<include name="login"></include>
<include name="getApi"></include>
<include name="deleteApi"></include>
</methods>
</class>
</classes>
</test>
<listeners>
<listener class-name="com.qa.report.ExtentTestNGReporterListener"></listener>
</listeners>
</suite>

测试用例中使用Reporter.log方法可以在生成的report中对应的接口里增加你想要呈现的属性,比如状态码,接口地址

 package com.qa.tests;

 import com.alibaba.fastjson.JSON;
import com.qa.base.TestBase;
import com.qa.Parameters.postParameters;
import com.qa.restclient.RestClient;
import com.qa.util.TestUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; import java.io.IOException;
import java.util.HashMap; import static com.qa.util.TestUtil.dtt; public class testCase1 extends TestBase {
TestBase testBase;
RestClient restClient;
CloseableHttpResponse closeableHttpResponse;
//host根url
String host;
//Excel路径
String testCaseExcel;
//header
HashMap<String ,String> postHeader = new HashMap<String, String>();
@BeforeClass
public void setUp(){
testBase = new TestBase();
restClient = new RestClient();
postHeader.put("Content-Type","application/json");
//载入配置文件,接口endpoint
host = prop.getProperty("Host");
//载入配置文件,post接口参数
testCaseExcel=prop.getProperty("testCase1data"); } @DataProvider(name = "postData")
public Object[][] post() throws IOException {
return dtt(testCaseExcel,0); } @DataProvider(name = "get")
public Object[][] get() throws IOException{
//get类型接口
return dtt(testCaseExcel,1);
} @DataProvider(name = "delete")
public Object[][] delete() throws IOException{
//delete类型接口
return dtt(testCaseExcel,2);
}
@Test(dataProvider = "postData")
public void login(String loginUrl,String username, String passWord) throws Exception {
//使用构造函数将传入的用户名密码初始化成登录请求参数
postParameters loginParameters = new postParameters(username,passWord);
//将登录请求对象序列化成json对象
String userJsonString = JSON.toJSONString(loginParameters);
//发送登录请求
closeableHttpResponse = restClient.postApi(host+loginUrl,userJsonString,postHeader);
//从返回结果中获取状态码
int statusCode = TestUtil.getStatusCode(closeableHttpResponse);
Assert.assertEquals(statusCode,200);
Reporter.log("状态码:"+statusCode,true);
Reporter.log("接口地址: "+loginUrl);
} @Test(dataProvider = "get")
public void getApi(String url) throws Exception{
closeableHttpResponse = restClient.getApi(host+ url);
int statusCode = TestUtil.getStatusCode(closeableHttpResponse);
Assert.assertEquals(statusCode,200);
Reporter.log("状态码:"+statusCode,true);
Reporter.log("接口地址: "+url);
} @Test(dataProvider = "delete")
public void deleteApi(String url) throws Exception{
System.out.println(url);
closeableHttpResponse = restClient.deleteApi(url);
int statusCode = TestUtil.getStatusCode(closeableHttpResponse);
Assert.assertEquals(statusCode,204);
Reporter.log("状态码:"+statusCode,true);
Reporter.log("接口地址: "+url);
} @BeforeClass
public void endTest(){
System.out.print("测试结束");
} }

运行testng.xml后在test-output目录下找到Index.html,每次执行都会刷新测试报告

2.testng.xml多条testcase的情况下,test name 会变成报告的testname

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="test" verbose="1" preserve-order="true" parallel="false">
<test name="项目1">
<classes>
<class name="com.qa.tests.testCase1">
<methods>
<include name="login"></include>
<include name="getApi"></include>
<include name="deleteApi"></include>
</methods>
</class>
</classes>
</test>
<test name="项目2">
<classes>
<class name="com.qa.tests.testCase1">
<methods>
<include name="login"></include>
<include name="getApi"></include>
<include name="deleteApi"></include>
</methods>
</class>
</classes>
</test>
<listeners>
<listener class-name="com.qa.report.ExtentTestNGReporterListener"></listener>
</listeners>
</suite>

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

接口自动化框架(java)--5.通过testng.xml生成extentreport测试报告的更多相关文章

  1. 接口自动化框架(java)--1.项目概述

    项目github地址: https://github.com/tianchiTester/API_AutoFramework 这套框架的报告是自己封装的 1.测试基类TestBase: 接口请求的te ...

  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. 转载:python + requests实现的接口自动化框架详细教程

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

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

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

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

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

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

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

  9. Python+Pytest+Allure+Git+Jenkins接口自动化框架

    Python+Pytest+Allure+Git+Jenkins接口自动化框架 一.接口基础 接口测试是对系统和组件之间的接口进行测试,主要是效验数据的交换,传递和控制管理过程,以及相互逻辑依赖关系. ...

随机推荐

  1. TensorFlow的Bazel构建文件结构

    目录 说明 分析 全局设定文件:$TF_ROOT/WORKSPACE 外部依赖项入口:tensorflow/workspace.bzl 看看有多少package? 本来是想理解一下TF源码编译过程的, ...

  2. 在ABP的Web层中实现复杂请求跨域访问

    在最近的项目中,后端使用ABP,前端采用React,前后端完全分离.其中大部分接口都通过WebApi层调用,项目中未使用Session.但最后在添加一个网站的验证码验证留言功能时,使用了Session ...

  3. AWS S3 递归上传文件和递归下载文件, 以及S3之间拷贝文件夹

    1. 递归上传文件: aws s3 cp 本地文件夹 s3://bucket-name -- recursive --region us-east-1 2. 递归下载S3上的文件夹: cd  本地下载 ...

  4. URL 链接中 井号#、问号?、连接符& 分别有什么作用?

    在一个 URL 中可以包含很多的内容,其中不仅仅是包含 26 个英文字母,10 个罗马数字,中文汉字,还可以拥有井号“#”.问号“?”.连接符“&”等三种最常见的符号,那么这些符号在网站中都有 ...

  5. Hibernate 映射一对一关联关系

    基于外键的方式: 附上代码: public class Manager { private Integer mgrId; private String mgrName; private Departm ...

  6. NOIP-玩具谜题

    题目描述 小南有一套可爱的玩具小人,它们各有不同的职业. 有一天,这些玩具小人把小南的眼镜藏了起来.小南发现玩具小人们围成了一个圈,它们有的面朝圈内,有的面朝圈外,如下图: 这时 `singer` 告 ...

  7. [LeetCode] Split Array With Same Average 分割数组成相同平均值的小数组

    In a given integer array A, we must move every element of A to either list B or list C. (B and C ini ...

  8. oldboy es和logstash

    logstash: input:https://www.elastic.co/guide/en/logstash/current/input-plugins.html input { file { p ...

  9. CUDA相关问题

    之前装了GPU驱动后,再装了CUDA 9.0,再装了cuDNN,并且对样例mnistCUDNN进行执行,显示“Test passed!"通过.但是倒忘了有没有测试CUDA是否安装成功.驱动也 ...

  10. 锋利的jquery 事件 动画

    事件 $(function){} bind(type, [data],function) 事件类型, 传递参数, 处理函数 hover(enter, leave) 光标停留时,函数enter,离开时函 ...