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

由于之前已经通过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. leetcode目录

    Leetcode 1. 数组 2. 动态规划 3. 字符串 4. 链表.双指针.排序 5. 树 6. 回溯算法.贪心算法.分治算法.

  2. Java Spring Boot VS .NetCore (十一)自定义标签 Java Tag Freemarker VS .NetCore Tag TagHelper

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  3. PHP中的面向对象思想

    <?php header("Content-Type: text/html; charset=gb2312"); class person{ /** * 成员属性 * 在类中 ...

  4. python数据类型——数据转换

    数据类型有很多种,比如数值和字符,比如6和a,字符是需要加双引号的,下面的例子运行的结果是不一样的,数值会相加而字符会相连 print(6+6)print("6"+"6& ...

  5. UOJ#395. 【NOI2018】你的名字 字符串,SAM,线段树合并

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ395.html 题解 记得同步赛的时候这题我爆0了,最暴力的暴力都没调出来. 首先我们看看 68 分怎么做 ...

  6. Scala模式匹配| 隐式转换

    1. 模式匹配 Scala中的模式匹配类似于Java中的switch语法,但是更加强大.模式匹配语法中,采用match关键字声明,每个分支采用case关键字进行声明,当需要匹配时,会从第一个case分 ...

  7. GIT初始学习记录

    目录 GIT学习记录 配置github与gitlib两个账号 基本操作 git init:初始化仓库 git status:查看仓库状态 git add :向缓存区中添加文件 git commit 保 ...

  8. angular中service封装$http做权限时拦截403等状态及获取验证码倒计时、跨域问题解决

    封装$http.做权限时拦截403等状态及获取验证码倒计时: 拦截接口返回状态 var app = angular.module('app'); app.factory('UserIntercepto ...

  9. In the beginning, Coders create the repos and blogs

    ---恢复内容开始--- 这是一个新的博客 print(‘hello new world’) 这个博客叫做 brain in a jar, 不知道以后会不会改名. 只是因为偶然想到了普特南的缸中之脑理 ...

  10. Buffer --缓冲器

    一. 启动Buffer缓冲器 node 输入 buffer 创建一个新的buffer var buf = new buffer(''hello word) 查看buf的长度 buf.length 运行 ...