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

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

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

testng配置如下:

单suite,单test

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

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
 3 <suite name="test" verbose="1" preserve-order="true" parallel="false">
 4     <test name="testCase1">
 5         <classes>
 6             <class name="com.qa.tests.testCase1">
 7                 <methods>
 8                     <include name="login"></include>
 9                     <include name="getApi"></include>
10                     <include name="deleteApi"></include>
11                 </methods>
12             </class>
13         </classes>
14     </test>
15     <listeners>
16         <listener class-name="com.qa.report.ExtentTestNGReporterListener"></listener>
17     </listeners>
18 </suite>

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

 1 package com.qa.tests;
 2
 3 import com.alibaba.fastjson.JSON;
 4 import com.qa.base.TestBase;
 5 import com.qa.Parameters.postParameters;
 6 import com.qa.restclient.RestClient;
 7 import com.qa.util.TestUtil;
 8 import org.apache.http.client.methods.CloseableHttpResponse;
 9 import org.testng.Assert;
10 import org.testng.Reporter;
11 import org.testng.annotations.BeforeClass;
12 import org.testng.annotations.DataProvider;
13 import org.testng.annotations.Test;
14
15 import java.io.IOException;
16 import java.util.HashMap;
17
18 import static com.qa.util.TestUtil.dtt;
19
20 public class testCase1 extends TestBase {
21     TestBase testBase;
22     RestClient restClient;
23     CloseableHttpResponse closeableHttpResponse;
24     //host根url
25     String host;
26     //Excel路径
27     String testCaseExcel;
28     //header
29     HashMap<String ,String> postHeader = new HashMap<String, String>();
30     @BeforeClass
31     public void setUp(){
32         testBase = new TestBase();
33         restClient = new RestClient();
34         postHeader.put("Content-Type","application/json");
35         //载入配置文件,接口endpoint
36         host = prop.getProperty("Host");
37         //载入配置文件,post接口参数
38         testCaseExcel=prop.getProperty("testCase1data");
39
40     }
41
42     @DataProvider(name = "postData")
43     public Object[][] post() throws IOException {
44         return dtt(testCaseExcel,0);
45
46     }
47
48     @DataProvider(name = "get")
49     public Object[][] get() throws IOException{
50         //get类型接口
51         return dtt(testCaseExcel,1);
52     }
53
54     @DataProvider(name = "delete")
55     public Object[][] delete() throws IOException{
56         //delete类型接口
57         return dtt(testCaseExcel,2);
58     }
59     @Test(dataProvider = "postData")
60     public void login(String loginUrl,String username, String passWord) throws Exception {
61         //使用构造函数将传入的用户名密码初始化成登录请求参数
62         postParameters loginParameters = new postParameters(username,passWord);
63         //将登录请求对象序列化成json对象
64         String userJsonString = JSON.toJSONString(loginParameters);
65         //发送登录请求
66         closeableHttpResponse = restClient.postApi(host+loginUrl,userJsonString,postHeader);
67         //从返回结果中获取状态码
68         int statusCode = TestUtil.getStatusCode(closeableHttpResponse);
69         Assert.assertEquals(statusCode,200);
70         Reporter.log("状态码:"+statusCode,true);
71         Reporter.log("接口地址: "+loginUrl);
72     }
73
74     @Test(dataProvider = "get")
75     public void getApi(String url) throws Exception{
76         closeableHttpResponse = restClient.getApi(host+ url);
77         int statusCode = TestUtil.getStatusCode(closeableHttpResponse);
78         Assert.assertEquals(statusCode,200);
79         Reporter.log("状态码:"+statusCode,true);
80         Reporter.log("接口地址: "+url);
81     }
82
83     @Test(dataProvider = "delete")
84     public void deleteApi(String url) throws Exception{
85         System.out.println(url);
86         closeableHttpResponse = restClient.deleteApi(url);
87         int statusCode = TestUtil.getStatusCode(closeableHttpResponse);
88         Assert.assertEquals(statusCode,204);
89         Reporter.log("状态码:"+statusCode,true);
90         Reporter.log("接口地址: "+url);
91     }
92
93     @BeforeClass
94     public void endTest(){
95         System.out.print("测试结束");
96     }
97
98 }

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

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

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
 3 <suite name="test" verbose="1" preserve-order="true" parallel="false">
 4     <test name="项目1">
 5         <classes>
 6             <class name="com.qa.tests.testCase1">
 7                 <methods>
 8                     <include name="login"></include>
 9                     <include name="getApi"></include>
10                     <include name="deleteApi"></include>
11                 </methods>
12             </class>
13         </classes>
14     </test>
15     <test name="项目2">
16         <classes>
17             <class name="com.qa.tests.testCase1">
18                 <methods>
19                     <include name="login"></include>
20                     <include name="getApi"></include>
21                     <include name="deleteApi"></include>
22                 </methods>
23             </class>
24         </classes>
25     </test>
26     <listeners>
27         <listener class-name="com.qa.report.ExtentTestNGReporterListener"></listener>
28     </listeners>
29 </suite>

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

原文地址:https://www.cnblogs.com/111testing/p/10624847.html

时间: 2024-07-28 15:16:04

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

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

这套框架的报告是自己封装的 一般token会在登录接口返回结果中呈现,从代码层面获取token的方式有很多种,我是使用jsonpath这个json路径语言去匹配token所在路径的key值 1 package com.qa.tests; 2 3 4 import com.alibaba.fastjson.JSON; 5 import com.qa.base.TestBase; 6 import com.qa.Parameters.postParameters; 7 import com.qa.r

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

这套框架的报告是自己封装的 每种请求类型放入不同的sheet中,就可以避免新建太多的excel去做数据驱动. XSSFSheet类提供了一个读取sheet的方法,getSheetAt(int),通过下标去访问想要的sheet 1.Excel,添加两个sheet页改名成get , delete,代表这两种类型的接口 2. 在用例的dataProvider中将这两个sheet作两个方法分别读取.再传入对应的test中 1 package com.qa.tests; 2 3 import com.al

接口自动化框架好文

接口自动化框架好文 2017-04-13 API 自动化测试框架分享 接口测试的一些感悟 HTTP API自动化测试从手工到平台的演变

python3+request接口自动化框架

首次书写博客,记录下写的自动化接口框架,框架比较简单,哈哈哈,算是记录下历程把!~~~ 一.本次框架由python3.6 书写 1.准备代码环境,下载python3.6    下载地址:https://www.python.org/downloads 2.下载pycharm 软件. 二.开始创建python接口自动化框架: 1.这是我创建的框架中的各个文件夹,分别有config  配置文件夹.fengzhuang   将接口用get post  两种传输方式进行封装并自动来区分执行. 2.log

python写接口自动化框架

代码如下: 1 #!/usr/bin/env python 2 # -*- coding: utf_8 -*- 3 # 获取测试用例文件excel 4 5 import xlrd 6 import json 7 8 9 class CreateExcel: 10 def __init__(self): 11 pass 12 13 @classmethod 14 def open_excel(cls): 15 path = "testcase.xls" 16 workbook = xlr

python接口自动化框架

基于 python 的接口测试框架 接口测试 · jphtmt · 于 5 月前发布 · 最后由 jphtmt 于 4 月前回复 · 3553 次阅读 项目背景 公司内部的软件采用B/S架构,管理实验室数据,实现数据的存储和分析统计.大部分是数据的增删改查,由于还在开发阶段,所以UI界面的变化非常快,之前尝试过用python+selenium进行UI自动化测试,后来发现今天刚写好的脚本第二天前端就改了页面,又得重新去定位元素什么的,消耗大量的精力与时间维护自动化脚本.针对此种情况,对接口测试较为

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

工具准备 JDK: jdk1.8.0_111 Ant: apache-ant-1.9.9 Jmeter: apache-jmeter-3.1 Jenkins: jenkins-2.7.4 JDK安装 安装JDK: 双击JDK安装包,选择安装路径,可以采取默认安装路径,也可以自定义安装路径,为了方便直接安装在默认路径下了.安装路径:C:\Program Files\Java\jdk1.8.0_111; 配置JDK环境变量: 系统变量→新建 JAVA_HOME 变量 变量值填写jdk的安装目录(本人

python接口自动化框架_初级

1.前提: python基础(能看懂代码就行,学到面向对象) 找一个应用场景(比如在聚合数据中找一个测试接口:https://www.juhe.cn/) Pycharm开发环境(IDE) 2.架子搭建: 总说框架框架不够接地气,那就简单的理解为目录,如下图是我的目录: 这是一般通用性工程目录结构, config目录管理所有的配置文件,里面可能有多个配置文件,类型可能是xml.ini.jsp等....... help目录中一般我会放一些帮助理解的东西 public目录顾名思义,里面放一些公共类,比

python入门(二十一):基于文件的接口自动化框架

1.接口测试过程中,最耗费时间的是什么? 组织测试用例.如果入参数量多的话,整理入参比较耗时.对接口文档.参数耗时.分析log定位问题.需要加解密参数.返回字段的验证.很多接口返回参数作为其他接口入参(关联). 2.安装pip 进入到D:\Python36\Scripts目录下,执行easy_install pip命令安装pip 安装成功后,输入pip,显示使用命令则为安装成功. 3.安装requests包 pip install requests安装最新版本的requests包 4.请求注册的