简体   繁体   English

以逻辑OR在TestNG中运行相关测试

[英]Running dependent tests in TestNG in logical OR

I have my TestNG tests like below: 我有如下的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++++++++++++");
    }
}

I want my Test3 to be executed if either of Test1 OR Test2 is passed. 如果要通过Test1或Test2之一,我希望执行我的Test3。 The way TestNG is working, it seems like it is always checking if both Test1 AND Test2 are passed. TestNG的工作方式似乎总是检查是否同时通过了Test1和Test2。 Is there a way that we can change the logical AND to logical OR. 有没有一种方法可以将逻辑AND更改为OR。 I also tried specifying the dependsOnMethods like but on luck 我也尝试指定dependsOnMethods,但是运气不错

dependsOnMethods  = {("Test1"),("Test2")})

I also tried dependsOnGroups but that again is checking if all the corresponding tests are passed in logical AND. 我还尝试了dependsOnGroups,但是再次检查是否所有相应的测试都以逻辑AND传递。 Upon running the above tests I am getting the output as: 运行以上测试后,我得到的输出为:

+++++++++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
===============================================

dependsOnMethods just tells the order in which unit test will run. dependsOnMethods只是告诉您单元测试的运行顺序。 It does not mean that those tests should be passed or failed. 这并不意味着这些测试应该通过或失败。

You can do it programmatically 您可以以编程方式进行

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++++++++++++");
       }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM