TestNG Tutorials 26: Understand Usage of alwaysRun Attribute With Test Method of TestNG

Hello Folks,

In this post we will learn about “alwaysRun” attribute and its usage with @Test annotated method in TestNG.

“alwaysRun” attribute can be used with Configuration methods(BeforeXXXX and @AfterXXXX) and Test methods(@Test).

TestNG documentation defines alwaysRun attribute as:

For configuration methods:

For before methods (beforeSuite, beforeTest, beforeTestClass and beforeTestMethod, but not beforeGroups): If set to true, this configuration method will be 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. Default value of alwaysRun attribute is false.

For Test methods:

If set to true, this test method will always be run even if it depends on a method that failed.

In this post, we will see usage of alwaysRun for Test methods. For configuration method see this post.

Let’s start with examples:

Suppose we have 2 test methods in a TestNG class as below:

On running above class as a TestNG suite, you will get below output:
Method 1
Method 2

Now, let’s explicitly fail first test method as below:

On running above class as a TestNG suite, you will get below output:
Method 1
Method 2

You can understand now that failure of one test method does not impact other test method execution. TestNG will ignore remaining statements in failed method and will continue with next test method.

Now, let’s establish relationship/dependency among tests as below:

On running above class as a TestNG suite, you will get below output:
Method 1

“Method3” depends on “Method2” and “Method” depends on “Method1”. Here, “Method1″fails causing skipping of “Method2”. Since “Method2″is skipped, “Method3” will also be skipped.

Suppose you have scenarios where if “Method2” should be executed even if “Method1” is fail or “method2” is find with whatever result of “Method1″. This is called soft dependency.

If you want soft dependency or say you want test methods to be executed even if dependent method is failed or skipped, you need to set”alwaysRun” attribute to true as below:

Output:

So, alwaysRun attribute can be used with test methods to achieve dependency as I explained above.

More about TestNG in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning