@Test annotation has many attributes, and alwaysRun is one of them. In this post, we will discuss the alwaysRun attribute in detail.
What is the use of the alwaysRun attribute? What does this attribute do? Well, According to testng documentation.
If set to true, this test method will always be run even if it depends on a method that failed.
Confused? Don’t worry. We have got you covered.
Let’s try to run a test case (testMethod, as shown below ) that depends on another test method ( dependsOnThis, also shown below ) without using the alwaysRun attribute and see what happens. Here we will fail the dependsOnThis test case.
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
}
}
Output –
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
===============================================
We got the above output after running the testMethod as we can see that the testMethod() hasn’t been executed because its dependent method (dependsOnThis) has failed.
Now, let’s try the same code, but we will use the alwaysRun attribute and see what happens.
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
}
}
Output –
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
===============================================
Here you can see that the testMethod test ran despite dependsOnThis failed same as earlier. So, this is the functionality of the alwaysRun attribute.
We have a question: Is it necessary to pass the dependsOnMethods or dependsOnGroups attribute whenever we use the alwaysRun attribute in the @Test annotation?
The answer is yes, alwaysRun will only work if we pass the dependsOnMethods or dependsOnGroups attribute with it. The alwaysRun will be ignored if it’s been written without the dependsOnMethods or dependsOnGroups attribute. So, it’s better to use it with dependsOnMethods or dependsOnGroups 😛
We hope that you have liked this article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.
Other @Test attributes
- @Test Annotation attributes – enabled attribute
- @Test Annotation attributes – dependsOnMethods attribute
- @Test annotation attributes – invocationCount attribute
- @Test annotation attributes – threadPoolSize attribute
- @Test annotation attributes – expectedExceptions attribute
- @Test annotation attributes – expectedExceptionsMessageRegExp attribute
- @Test annotation attributes – priority attribute