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

由于之前已经通过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. CodeForces 623E Transforming Sequence 动态规划 倍增 多项式 FFT 组合数学

    原文链接http://www.cnblogs.com/zhouzhendong/p/8848990.html 题目传送门 - CodeForces 623E 题意 给定$n,k$. 让你构造序列$a( ...

  2. 命令行神器 Click 简明笔记

    Click 是用 Python 写的一个第三方模块,用于快速创建命令行.我们知道,Python 内置了一个 Argparse 的标准库用于创建命令行,但使用起来有些繁琐,Click 相比于 Argpa ...

  3. 【Codeforces】【网络流】【线段树】【扫描线】Oleg and chess (CodeForces - 793G)

    题意: 给定一个n*n的矩阵,一个格子上可以放一个车.其中有q个子矩阵,且q个子矩阵互不相交或者是重叠(但边界可以衔接).这q个子矩阵所覆盖的地方都是不能够放车的.车可以越过子矩阵覆盖的地方进行攻击( ...

  4. Face The Right Way [POJ3276] [开关问题]

    题意: 有n头奶牛排成一排,有的朝前(F)有的朝后(B),现在你可以使k头奶牛一次性翻转朝向(n>=k>=1),问你最少的翻转次数和此时对应的k值. Input Line 1: A sin ...

  5. [jzoj]4216.【NOIP2015模拟9.12】平方和

    Link https://jzoj.net/senior/#main/show/4216 Description 给出一个N个整数构成的序列,有M次操作,每次操作有一下三种: ①Insert Y X, ...

  6. vue-countTo---简单好用的一个数字滚动插件

    vue-countTo是一个无依赖,轻量级的vue组件,可以自行覆盖easingFn. 你可以设置 startVal 和 endVal,它会自动判断计数或倒计时.支持vue-ssr.vue-count ...

  7. JS的javascript:void(0)用法

    javascript:void(0)用法如下: <a href="javascript:void(0)"></a> // 执行js函数,0表示不执行函数. ...

  8. [LeetCode] Binary Trees With Factors 带因子的二叉树

    Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree us ...

  9. [LeetCode] Number of Lines To Write String 写字符串需要的行数

    We are to write the letters of a given string S, from left to right into lines. Each line has maximu ...

  10. cmd 命令 net start mongodb 启动不了,提示 net 不是内部命令或者外部命令

    1.要管理员的身份进入 cmd 2.右击我的电脑-->属性-->高级系统设置 3.选择高级-->环境变量 4.找到系统变量-->Path-->编辑 5.把 C:\wind ...