这套框架的报告是自己封装的
由于之前已经通过Extentreport插件实现了Testng的IReport接口,所以在testng.xml中使用listener标签并指向实现IReport接口的那个类就可以替换原始的testngreport
testng配置如下:
单suite,单test
test name 指向你写的testCase,methods放入需要执行的方法
1 2 34 5 156 147 138 129 10 11 16 1817
测试用例中使用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根url25 String host;26 //Excel路径27 String testCaseExcel;28 //header29 HashMappostHeader = new HashMap ();30 @BeforeClass31 public void setUp(){32 testBase = new TestBase();33 restClient = new RestClient();34 postHeader.put("Content-Type","application/json");35 //载入配置文件,接口endpoint36 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 @BeforeClass94 public void endTest(){95 System.out.print("测试结束");96 }97 98 }
运行testng.xml后在test-output目录下找到Index.html,每次执行都会刷新测试报告
2.testng.xml多条testcase的情况下,test name 会变成报告的testname
1 2 34 5 156 147 138 129 10 11 16 2617 2518 2419 2320 21 22 27 2928
原文地址