@Test Annotation属性- alwaysRun属性

@Test注释有许多属性,alwaysRun就是其中之一。在这篇文章中,我们将详细讨论alwaysRun属性。

alwaysRun属性的用途是什么?这个属性有什么作用?嗯,根据测试文件。

如果设置为true,则此测试方法将始终运行,即使它依赖于失败的方法。

困惑?别担心我们会帮你的。

让我们尝试运行一个依赖于另一个测试方法(dependsOnThis,也在下面显示)的测试用例(testMethod,如下所示),而不使用alwaysRun属性,看看会发生什么。这里我们将使dependsOnThis测试用例失败。

import org.testng.Assert;
import org.testng.annotations.Test;
 
public class CodekruTest {
 
    @Test
    public void dependsOnThis() {
        Assert.assertTrue(false); // failing the test
    }
 
    @Test(dependsOnMethods = { "dependsOnThis" })
    public void testMethod() {
        System.out.println("testMethod is passed"); // this line will never be printed
    }
 
}

产出-


FAILED: dependsOnThis
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.dependsOnThis(CodekruTest.java:10)
	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: testMethod
java.lang.Throwable: Method CodekruTest.testMethod()[pri:0, instance:Test.CodekruTest@404bbcbd] 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
===============================================


===============================================
Default suite
Total tests run: 2, Failures: 1, Skips: 1
===============================================

我们在运行testMethod后得到了上面的输出,因为我们可以看到testMethod()没有被执行,因为它的依赖方法(dependsOnThis)失败了。

现在,让我们尝试相同的代码,但我们将使用alwaysRun属性,看看会发生什么。

import org.testng.Assert;
import org.testng.annotations.Test;
 
public class CodekruTest {
 
    @Test
    public void dependsOnThis() {
        Assert.assertTrue(false); // failing the test
    }
 
    @Test(alwaysRun = true, dependsOnMethods = { "dependsOnThis" })
    public void testMethod() {
        System.out.println("testMethod is passed"); // now, this will be printed
    }
 
}

产出-

testMethod is passed
PASSED: testMethod
FAILED: dependsOnThis
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.dependsOnThis(CodekruTest.java:10)
	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)


===============================================
    Default test
    Tests run: 2, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Failures: 1, Skips: 0
===============================================

在这里,您可以看到,尽管dependsOnThis失败,testMethod测试仍然运行,与前面一样。这就是alwaysRun属性的功能。

我们有一个问题:每当我们在@Test注释中使用alwaysRun属性时,是否有必要传递dependsOnMethods或dependsOnGroups属性?

答案是肯定的,alwaysRun只有在我们传递了dependsOnMethodsdependsOnGroups属性的情况下才能工作。如果alwaysRun是在没有dependsOnMethods或dependsOnGroups属性的情况下编写的,它将被忽略因此,最好将其与dependsOnMethods或dependsOnGroups一起使用。

你可能感兴趣的:(java,unit,testing)