有时在测试的过程中,有些测试方法要依赖于另外测试方法的执行成功,testng支持这种依赖关系的
TestNg的依赖关系
testNg允许指定依赖关系,如果依赖关系失败了,则跳过后面的所有方法.
在@Test注释中使用属性dependsOnMethods,或者在@Test注释中使用属性dependsOnGroups。
示例代码dependsOnMethods
一个简单的method2依赖于method1
public class TestNgDemo4 {
@Test
public void test_method(){
System.out.println("第一个测试方法");
}
@Test(dependsOnMethods = {"test_method"})
public void test_method1(){
System.out.println("第二个测试方法");
}
}
执行结果:
第一个测试方法
第二个测试方法
===============================================
Default Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
如果method1失败了,则不执行method2
public class TestNgDemo4 {
@Test
public void test_method(){
System.out.println("第一个测试方法");
throw new RuntimeException();
}
@Test(dependsOnMethods = {"test_method"})
public void test_method1(){
System.out.println("第二个测试方法");
}
}
执行结果:
第一个测试方法
java.lang.RuntimeException
at testfan.three.testngdemo.TestNgDemo4.test_method(TestNgDemo4.java:15)
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:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127)
=================================
Default Suite
Total tests run: 2, Failures: 1, Skips: 1
===============================
dependsOnGroups 的操作
编写2个测试脚本,分别叫testBase,testTwo,其中testTwo依赖于testbase的方法
testBase.java的代码示例:
import org.testng.annotations.Test;
@Test(groups = "deploy")
public class testBase {
@Test
public void deployServer() {
System.out.println("Deploying Server...");
}
@Test(dependsOnMethods = "deployServer")
public void deployBackUpServer() {
System.out.println("Deploying Backup Server...");
}
}
testtwo.java代码:
import org.testng.annotations.Test;
public class testTwo {
@Test(dependsOnGroups = "deploy")
public void test_001(){
System.out.println("依赖方法的测试");
}
}
点击运行测试,测试结果如下:
Deploying Server...
Deploying Backup Server...
依赖方法的测试