我的TestNG测试如下:
public class Tests2 {
@Test(priority = 10)
public void Test1() {
System.out.println("+++++++++Running Test1++++++++++++");
throw new SkipException("Skipping the test");
}
@Test(priority = 20)
public void Test2() {
System.out.println("+++++++++Running Test2++++++++++++");
}
@Test(priority = 30, dependsOnMethods = {"Test1","Test2"})
public void Test3() {
System.out.println("+++++++++Running Test3++++++++++++");
}
}
如果传递了Test1或Test2中的任何一个,我希望我的Test3被执行。 TestNG的工作方式似乎总是检查Test1和Test2是否都通过了。有没有办法可以将逻辑AND更改为逻辑OR。我也尝试过像依赖运气那样指定dependsOnMethods
dependsOnMethods = {("Test1"),("Test2")})
我也试过了dependsOnGroups,但是再次检查是否所有相应的测试都是在逻辑AND中传递的。 运行上述测试后,我得到输出:
+++++++++Running Test1++++++++++++
+++++++++Running Test2++++++++++++
PASSED: Test2
SKIPPED: Test1
org.testng.SkipException: Skipping the test
at com.Tests.Tests2.Test1(Tests2.java:12)
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:497)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
at org.testng.TestNG.run(TestNG.java:1031)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:207)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:178)
SKIPPED: Test3
===============================================
Default test
Tests run: 3, Failures: 0, Skips: 2
===============================================
===============================================
Default suite
Total tests run: 3, Failures: 0, Skips: 2
===============================================
答案 0 :(得分:0)
dependsOnMethods
只是告诉单元测试运行的顺序。这并不意味着那些测试应该通过或失败。
您可以通过编程方式执行此操作
public class Tests2 {
private boolean testOnePassed=false;
private boolean testTwoPassed=false;
@Test(priority = 10)
public void Test1() {
System.out.println("+++++++++Running Test1++++++++++++");
testTwoPassed = false// based on your custom condition
throw new SkipException("Skipping the test");
}
@Test(priority = 20)
public void Test2() {
System.out.println("+++++++++Running Test2++++++++++++");
testTwoPassed = true// based on your custom condition
}
@Test(priority = 30)
public void Test3() {
if(testOnePassed || testTwoPassed){
System.out.println("+++++++++Running Test3++++++++++++");
}
}
}