TestNG 强大的测试框架(7)-@Test之间的依赖关系-dependsOnMethods
最近在51testing论坛里浏览TestNG框架,看到了TestNG的依赖测试,感觉很不错,在自动化项目里面很实用,就在这里跟大家聊聊它!
我们在做接口自动化回归测试时,一般都会把接口链接和预想结果保存在文件里,比如excel或者记事本等,导致我们搭建框架时一个TestNG测试类会有多个数据驱动对应多个@Test(用例),然而用例之间也许还会存在着依赖关系(虽然我们一再强调用例的独立性),而@Test注解的函数是没有返回值的,要注意的是@Test注解的函数运行顺序是按Ascall码的顺序来运行。
一、下面是一个简单的Demo
1,testng的测试类代码,CP_Normal_1类里的两个Test函数之间没有任何依赖关系,如果直接运行这个文件的话,它是先运行函数AssertData(),然后运行函数StatusCodeAst(),这样的话AssertData()就会报空指针异常,因为声明的全局变量response里面是没有内容的,为NULL。
package com.TestNg.CloudPoint;
import org.testng.annotations.Test;
import com.common.Common;
import net.sf.json.JSONObject;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.Reporter;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class CP_Normal_1{
@Autowired
private Common comm = new Common();
private String path = null;
private Object[][] getUrl = null;
private CloseableHttpResponse response = null;
// @Parameters("resp_code")
@Test(dataProvider="testUrl")
public void StatusCodeAst(String name,String url) throws ClientProtocolException, IOException {
response = comm.Resp(url);
Reporter.log(name+",请求链接:"+url);
int status = comm.StatusCode(response);
assertEquals(status, 200);
Reporter.log(name+"返回状态为: "+status);
}
@Test(dataProvider="respdata")
public void AssertData(JSONObject readexcel){
try {
Reporter.log((String) getUrl[0][0]);
JSONObject data = comm.JsonDate(response);
assertEquals(data, readexcel);
Reporter.log(getUrl[0][0]+"返回的json数据与响应的一致:"+data);
} catch (IOException e) {
Reporter.log((String) getUrl[0][0]+":请求失败!");
}
}
@DataProvider
public Object[][] testUrl() throws IOException{
getUrl = comm.getreadExcel(path, "interfacaecase.xlsx", 1, 1, "CP(云控接口)");
return getUrl;
}
@DataProvider
public Object[][] respdata() throws IOException{
JSONObject excelread = comm.getreadExceldata(path, "interfacaecase.xlsx", 1, 20, "CP(云控接口)");
return new Object[][]{
{excelread}
};
}
@BeforeTest
public void setup() throws IOException {
path = comm.getPath();
}
@AfterTest
public void destroy(){
Reporter.log((String) getUrl[0][0]+"测试结束");
}
}
看看它的运行结果
excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
PASSED: StatusCodeAst("正常生成CP用例", ")
FAILED: AssertData({"du_gp":0,"r":20,"t":1,"fb_gp":0,"du_c":0,"i_t_bd":40,"ds":5,"dc":"","fb_c":0})
java.lang.NullPointerException
at com.TestNg.CloudPoint.CP_Normal_1.AssertData(CP_Normal_1.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
===============================================
Default test
Tests run: 2, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Failures: 1, Skips: 0
===============================================
大家看到这里应该都会明白上面的测试类为什么会报错,那么怎么解决呢,方法有很多。但是TestNG给我们提供了dependsOnMethods,有了它就很好办了,不同用例之间的依赖就会建立起来了!下面来看看代码
package com.TestNg.CloudPoint;
import org.testng.annotations.Test;
import com.common.Common;
import net.sf.json.JSONObject;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.Reporter;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class CP_Normal_1{
@Autowired
private Common comm = new Common();
private String path = null;
private Object[][] getUrl = null;
private CloseableHttpResponse response = null;
// @Parameters("resp_code")
@Test(dataProvider="testUrl")
public void StatusCodeAst(String name,String url) throws ClientProtocolException, IOException {
response = comm.Resp(url);
Reporter.log(name+",请求链接:"+url);
int status = comm.StatusCode(response);
assertEquals(status, 200);
Reporter.log(name+"返回状态为: "+status);
}
@Test(dataProvider="respdata",dependsOnMethods={"StatusCodeAst"},enabled=true)
public void AssertData(JSONObject readexcel){
try {
Reporter.log((String) getUrl[0][0]);
JSONObject data = comm.JsonDate(response);
assertEquals(data, readexcel);
Reporter.log(getUrl[0][0]+"返回的json数据与响应的一致:"+data);
} catch (IOException e) {
Reporter.log((String) getUrl[0][0]+":请求失败!");
}
}
@DataProvider
public Object[][] testUrl() throws IOException{
getUrl = comm.getreadExcel(path, "interfacaecase.xlsx", 1, 1, "CP(云控接口)");
return getUrl;
}
@DataProvider
public Object[][] respdata() throws IOException{
JSONObject excelread = comm.getreadExceldata(path, "interfacaecase.xlsx", 1, 20, "CP(云控接口)");
return new Object[][]{
{excelread}
};
}
@BeforeTest
public void setup() throws IOException {
path = comm.getPath();
}
@AfterTest
public void destroy(){
Reporter.log((String) getUrl[0][0]+"测试结束");
}
}
看看运行结果:
excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
PASSED: StatusCodeAst("正常生成CP用例", )
PASSED: AssertData({"du_gp":0,"r":20,"t":1,"fb_gp":0,"du_c":0,"i_t_bd":40,"ds":5,"dc":"","fb_c":0})
===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
浙公网安备 33010602011771号