alwaysRun Attribute In TestNG: During discussing different annotation TestNG, we have mentioned so many attributes there. Out of them, alwaysRun is one of them. In this post, we are going to discuss in detail the TestNG attribute & how to use it with @test Annotation.
As per the TestNg official site statement
For before methods (beforeSuite, beforeTest, beforeTestClass, and beforeTestMethod, but not beforeGroups): If set to true, this configuration method will run regardless of what groups it belongs to.
For after methods (afterSuite, afterClass): If set to true, this configuration method will be run even if one or more methods invoked previously failed or was skipped.
The Above statement is applicable for these annotations @BeforeSuite, @AfterSuite, @BeforeTest, @AfterTest, @BeforeGroups, @AfterGroups, @BeforeClass, @AfterClass, @BeforeMethod & @AfterMethod
And for @Test Annotation: If set to true, this test method will always be run even if it depends on a method that failed.
Now let us try to Understand by Following Some Simple Programs:
package AlwaysRunExample; import org.testng.annotations.Test; public class AlwaysRunDemo1 { @Test public void method1() { System.out.println(“Method 1”); } @Test public void method2() { System.out.println(“Method 2”); } }
When we run the above class, in the output, we can find out that both the methods got executed.
Now Lets things little bit different and try to fail the first method explicitly and check whether after failing the first method, execution stop from that point or still it executing the second method:
package AlwaysRunExample; import org.testng.Assert; import org.testng.annotations.Test; public class AlwaysRunDemo1 { @Test public void method1() { System.out.println("Method 1"); // Failing test explicitly Assert.fail(); } @Test public void method2() { System.out.println("Method 2"); } }
Here we got to know from the output that when one method failed, it does not make any impact other method execution of testNG class. Still, if you looked the same thing in Java when we got any error or exception or any statement fails, then the execution is stopped from that point.
Now let us go one more step ahead and try to build dependency between the methods of a TestNG class and try to fail one method explicitly and check the output.
package AlwaysRunExample; import org.testng.Assert; import org.testng.annotations.Test; public class AlwaysRunDemo1 { @Test public void method1() { System.out.println("Method 1"); // Failing test explicitly Assert.fail(); } // This method will run only if method1 is passed. If method1 is failed or // skipped, method2 will not run. @Test(dependsOnMethods = "method1") public void method2() { System.out.println("Method 2"); } // Since method1 will fail, so method2 will be skipped. Since method2 is skipped, method3 will also be skipped as // method3 is dependent on method2 @Test(dependsOnMethods = "method2") public void method3() { System.out.println("Method 3"); } }
In the above program, you can see that we have made a dependency between the methods like the second method is dependent upon the execution results of the first method, and the third method is depended upon the execution results of the second method.
As you can see, the output when the first method gots failed the other two methods got skipped. The second method depends on the first method, but as the primary method got failed, so the second method got skipped, and the same thing happens with the third method.
Suppose there is a scenario where irrespective of whatever result of the dependent method, other test methods should be executed. Such things are called soft dependency.
In that case, the “alwaysRun” attribute comes to a picture, and by setting the attribute value to true, we can run other test methods when the dependent methods got failed or skipped.
Let’s Get to know how we can achieve by using “alwaysRun” attribute:
package AlwaysRunExample; import org.testng.Assert; import org.testng.annotations.Test; public class AlwaysRunDemo1 { @Test public void method1() { System.out.println("Method 1"); // Failing test explicitly Assert.fail(); } // alwaysRun attribute will override dependsOnMethods if dependent method is failed or skipped @Test(dependsOnMethods = "method1", alwaysRun=true) public void method2() { System.out.println("Method 2"); } // alwaysRun attribute will override dependsOnMethods if dependent method is failed or skipped @Test(dependsOnMethods = "method2", alwaysRun=true) public void method3() { System.out.println("Method 3"); } }
Now, if we run the above TestNG class, then you will find that as the first method got failed, but other methods successfully executed.
How to Use alwaysRun Attribute With Configuration Annotation?
Above, we have seen how we can use the alwaysRun method with the @test annotation, and now we are going to learn how to use it with the configuration annotation like @BeforeXXXX or @AfterXXXX annotations.
Let’s take a simple program with the configuration annotations and explicitly failed the @beforetest annotation and check what we got in the output:
package AlwaysRunExample; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterSuite; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class AlwaysRunDemo2 { @BeforeSuite public void beforeSuiteMethod() { System.out.println("beforeSuite"); // We fail this method explicitly Assert.fail(); } @BeforeTest public void beforeTestMethod() { System.out.println("beforeTest"); } @BeforeClass public void beforeClassMethod() { System.out.println("beforeClass"); } @BeforeMethod public void beforeMethodMethod() { System.out.println("beforeMethod"); } @Test public void testMethod() { System.out.println("test"); } @AfterSuite public void afterSuiteMethod() { System.out.println("afterSuite"); } @AfterTest public void afterTestMethod() { System.out.println("afterTest"); } @AfterClass public void afterClassMethod() { System.out.println("afterClass"); } @AfterMethod public void afterMethodMethod() { System.out.println("afterMethod"); } }
As you can see in the output, as the @beforesuite annotation got failed, it skips other configuration methods and test methods also.
Note: In the test methods case, if one test method failed, other methods get executed, but in case of configuration annotation, one configuration method failed, and then others also got skipped.
In the TestNG report, you will get test and configuration execution separately.
But as we all know that @before methods are mainly used mainly for the configuration purpose, so before the failure, some configuration already completed. Still, because of the failure, other configuration methods got skipped, but before complete the execution, we should release the resource.
So to achieve that, we must run at least one @after configuration method; in that case, we can use case we can use alwaysRun attribute with the @after configuration so that the after configuration method will execute and helps us to release the blocked the resources.
Sample Program:
package AlwaysRunExample; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterSuite; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class AlwaysRunDemo2 { @BeforeSuite public void beforeSuiteMethod() { System.out.println("beforeSuite"); Assert.fail(); } @BeforeTest public void beforeTestMethod() { System.out.println("beforeTest"); } @BeforeClass public void beforeClassMethod() { System.out.println("beforeClass"); } @BeforeMethod public void beforeMethodMethod() { System.out.println("beforeMethod"); } @Test public void testMethod() { System.out.println("test"); } @AfterSuite(alwaysRun=true) public void afterSuiteMethod() { System.out.println("afterSuite"); } @AfterTest public void afterTestMethod() { System.out.println("afterTest"); } @AfterClass public void afterClassMethod() { System.out.println("afterClass"); } @AfterMethod public void afterMethodMethod() { System.out.println("afterMethod"); } }
BY following the same way, you can use the alwaysRun attribute to true for other configuration methods.
Source: article
Leave a Reply