目录
语法
如果test1()失败了怎么办?test2()测试会发生什么,因为它依赖于test1?
如果我们在test1()方法上不使用@Test注释,但在test 2()测试用例上仍然使用@Test(dependsOnMethod =“test1”),会怎么样?
如果我们在dependsOnMethods属性中传递一个方法列表会怎么样?他们的处决有命令吗?
如何在属于不同类的测试用例上使用dependsOnMethods?
本文将详细讨论@Test注释的dependsOnMethods属性。
这个属性有什么作用?dependsOnMethods属性的用途是什么?dependsOnMethods用于依赖于另一个测试用例执行的测试用例。因此,后一个测试用例将首先执行,因为它的执行对于运行依赖于它的用例是必要的。
语法
dependsOnMethod属性将接受一个字符串数组,如{“test1”,“test 2”},如果一个测试只依赖于另一个测试,你也可以传递“test1”。所以,它会像下面这样-
@Test(dependsOnMethod = {"test1","test2"}) // if depends on one or more tests
@Test(dependsOnMethod = "test1") // if your test depends on only one other test
现在,让我们看一些关于如何在测试中使用dependsOnMethods的代码-
import org.testng.Assert;
import org.testng.annotations.Test;
public class CodekruTest {
@Test // this test will run first
public void test1() {
System.out.println("test1 is passed");
Assert.assertTrue(true);
}
@Test(dependsOnMethods = "test1")
public void test2() {
System.out.println("test2 is passed");
Assert.assertTrue(true);
}
}
产出-
test1 is passed
test2 is passed
PASSED: test1
PASSED: test2
===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================
在这里,您可以看到首先执行test1(),然后执行test2()。现在,让我们玩一些事情,因为许多紧迫的问题仍然没有答案。
- 如果test1()失败了怎么办?test2()测试会发生什么,因为它依赖于test1?
- 如果我们在test1()方法上不使用@Test注释,但在test 2()测试用例上仍然使用@Test(dependsOnMethod =“test1”),会怎么样?
- 如果我们在dependsOnMethods属性中传递一个方法列表会怎么样?他们的处决有命令吗?
- 如何在属于不同类的测试用例上使用dependsOnMethods?
如果test1()失败了怎么办?test2()测试会发生什么,因为它依赖于test1?
在这种情况下,test 2()将被跳过,不会运行。我们来看看这个的实际效果-
import org.testng.Assert;
import org.testng.annotations.Test;
public class CodekruTest {
@Test
public void test1() {
System.out.println("test1 is passed");
Assert.assertTrue(false); // failing the condition here
}
@Test(dependsOnMethods = "test1") // now this test won't run as test1 failed
public void test2() {
System.out.println("test2 is passed");
Assert.assertTrue(true);
}
}
产出-
test1 is passed
FAILED: test1
java.lang.AssertionError: expected [true] but found [false]
at org.testng.Assert.fail(Assert.java:96)
at org.testng.Assert.failNotEquals(Assert.java:776)
at org.testng.Assert.assertTrue(Assert.java:44)
at org.testng.Assert.assertTrue(Assert.java:54)
at Test.CodekruTest.test1(CodekruTest.java:12)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
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.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
SKIPPED: test2
java.lang.Throwable: Method CodekruTest.test2()[pri:0, instance:Test.CodekruTest@27508c5d] depends on not successfully finished methods
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:888)
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.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
===============================================
Default test
Tests run: 2, Failures: 1, Skips: 1
===============================================
如果我们在test1()方法上不使用@Test注释,但在test 2()测试用例上仍然使用@Test(dependsOnMethod =“test1”),会怎么样?
它将抛出一个错误,并且任何一个测试用例都不会运行,如下所示-
import org.testng.Assert;
import org.testng.annotations.Test;
public class CodekruTest {
public void test1() {
System.out.println("test1 is passed");
Assert.assertTrue(true);
}
@Test(dependsOnMethods = "test1")
public void test2() {
System.out.println("test2 is passed");
Assert.assertTrue(true);
}
}
产出-
org.testng.TestNGException:
Test.CodekruTest.test2() is depending on method public void Test.CodekruTest.test1(), which is not annotated with @Test or not included.
at org.testng.internal.MethodHelper.findDependedUponMethods(MethodHelper.java:123)
at org.testng.internal.MethodHelper.topologicalSort(MethodHelper.java:266)
at org.testng.internal.MethodHelper.sortMethods(MethodHelper.java:376)
at org.testng.internal.MethodHelper.collectAndOrderMethods(MethodHelper.java:65)
at org.testng.TestRunner.initMethods(TestRunner.java:438)
at org.testng.TestRunner.init(TestRunner.java:271)
at org.testng.TestRunner.init(TestRunner.java:241)
at org.testng.TestRunner.<init>(TestRunner.java:192)
at org.testng.remote.support.RemoteTestNG6_12$1.newTestRunner(RemoteTestNG6_12.java:33)
at org.testng.remote.support.RemoteTestNG6_12$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_12.java:66)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:713)
at org.testng.SuiteRunner.init(SuiteRunner.java:260)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:198)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1295)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1273)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
如果我们在dependsOnMethods属性中传递一个方法列表会怎么样?他们的处决有命令吗?
不,没有测试的执行顺序,所以,如果我们通过一个测试列表,如@Test(dependsOnMethods = {“test1″,“test 2”}). TestNG可以先执行test1或test 2,但可以保证它们将在使用dependsOnMethods属性的测试方法之前运行。
如何在属于不同类的测试用例上使用dependsOnMethods?
我们可以通过在dependsOnMethods属性中给出方法名的完整路径来实现。让我们创建两个不同的类,CodekruTestFirst和CodekruTestSecond,每个类都有自己的测试用例。CodekruTestFirst的测试用例依赖于CodekruTestSecond的测试用例。
CodekruTestFirst.java
import org.testng.Assert;
import org.testng.annotations.Test;
public class CodekruTestFirst {
@Test()
public void testFirst() {
System.out.println("Excecuting test method in CodekruTestFirst class");
Assert.assertTrue(true);
}
}
CodekruTestSecond.java
import org.testng.Assert;
import org.testng.annotations.Test;
public class CodekruTestSecond {
@Test(dependsOnMethods = "Demo.CodekruTestFirst.testFirst")
public void testSecond() {
System.out.println("Excecuting test method in CodekruTestSecond class");
Assert.assertTrue(true);
}
}
我们现在将运行CodekruTestSecond类,并且CodekruTestFirst中的测试用例也将被执行,因为testSecond()方法依赖于它。
产出-
Excecuting test method in CodekruTestFirst class
Excecuting test method in CodekruTestSecond class
===============================================
codekru
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
注意事项: 依赖方法和依赖方法必须具有不同的名称。否则,您将遇到错误。
public class CodekruTestFirst {
@Test
public void test() {
System.out.println("Excecuting test method in CodekruTestFirst class");
Assert.assertTrue(true);
}
}
public class CodekruTestSecond {
@Test(dependsOnMethods = "Demo.CodekruTestFirst.test")
public void test() {
System.out.println("Excecuting test method in CodekruTestSecond class");
Assert.assertTrue(true);
}
}
现在,当我们运行CodekruTestSecond类时,我们会得到下面的错误。
org.testng.TestNGException:
The following methods have cyclic dependencies:
CodekruTestSecond.test()[pri:0, instance:Demo.CodekruTestSecond@236e3f4e]
at org.testng.internal.Graph.topologicalSort(Graph.java:128)
at org.testng.internal.MethodHelper.topologicalSort(MethodHelper.java:299)
at org.testng.internal.MethodHelper.sortMethods(MethodHelper.java:382)
at org.testng.internal.MethodHelper.collectAndOrderMethods(MethodHelper.java:71)
at org.testng.TestRunner.initMethods(TestRunner.java:517)
at org.testng.TestRunner.init(TestRunner.java:336)
at org.testng.TestRunner.init(TestRunner.java:289)
at org.testng.TestRunner.<init>(TestRunner.java:220)
at org.testng.remote.support.RemoteTestNG6_12$1.newTestRunner(RemoteTestNG6_12.java:33)
at org.testng.remote.support.RemoteTestNG6_12$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_12.java:66)
at org.testng.ITestRunnerFactory.newTestRunner(ITestRunnerFactory.java:55)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:676)
at org.testng.SuiteRunner.init(SuiteRunner.java:178)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:112)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1306)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1282)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1131)
at org.testng.TestNG.runSuites(TestNG.java:1069)
at org.testng.TestNG.run(TestNG.java:1037)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)