Exclude Test Methods

Introduction to excluding test methods

Excluding test methods prevents them from running during test execution. We have already seen how we can prevent groups of tests from running in the previous section. In this section we will look at how we can exclude one test at a time.

Explanation of @Test(enabled = false) annotation

We discussed earlier that the @Test annotation has several properties. One of those properties that we can configure is the ‘enabled’ property. The enabled property is just like a switch on the method. It turns the method on or off as we need. It accepts two valid boolean properties- true and false. By default the enabled property is always set to true.

Example:

@Test(enabled=true/false) public void LoginTest() { //body of the test-method }

A Note on the @Ignore Annotation

The @Ignore annotation is a class level annotation that yield the same output as the @Test(enabled=true/false) feature. The method above could have been stopped from execution using @Ignore annotation as: @Ignore public void LoginTest() { //body of the test-method } This works exactly the same as the code above.

Demonstration of excluding specific test methods using enabled attribute.

To exclude specific test methods using enabled attribute you need to modify the default value of the enabled attribute as:

@Test(enabled=false) public void LoginTest() { //body of the test-method }

This excludes the method LoginTest from execution.

Example code snippet illustrating exclusion of tests

Let’s consider the example we took earlier. We excluded nav-search group from running altogether from the testng.xml file. Now suppose you have many other test methods inside the nav-search group but you want only this method (testNavBarSearch) to not run. Maybe because its incomplete or error-prone or unstable. So instead of excluding the entire group from execution, you can turn this method off and run the group the same way as before.

Example:

Java
public class TestClassB {
    private WebDriver driver;
    private WebElement searchIcon;
    private WebElement searchBar;
    @BeforeClass
    public void setUp() {
        driver=new ChromeDriver();
        driver.get("https://www.w3wiki.org/");
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        searchIcon= driver.findElement(By.xpath("//*[@id=\"gcse-form\"]/button/i"));
        searchIcon.click();
    }

    @Test(groups = {"nav-search"}, enabled=false)
    public void testNavBarSearch() {
        searchBar=driver.findElement(By.xpath("//*[@id=\"gcse-search-input\"]"));
        searchBar.sendKeys("TestNG and Selenium");
        searchBar.sendKeys(Keys.ENTER);
    }

    @AfterClass
    public void tearDown() {
        driver=null;
    }
}

You can see that in the above example we didn’t even have to remove it from the group. We simply turned it off.

You will see that the output opens only one instance of browser and run gives the following output:

Considerations for when to exclude tests and alternatives

You can exclude tests in the following situations:

  • Incomplete test
  • Unstable test
  • Slow Running Test
  • Debugging

Alternatively, you can also avoid using this feature by:

  • Adopting Good code practices
  • Completing incomplete tests
  • Fixing unstable ones
  • See if you can somehow make the slow tests run faster by coming up with a suitable solution

What are Exclude and Include Test Methods in TestNG?

TestNG is a popular Java testing framework that stands for Test-Next Generation. It is an advanced form of JUnit, and offers comprehensive features, especially when it comes to integration tests. In this article, we will learn how to include and exclude test cases from executing at the Method, Class, and Suite levels.

Table of Content

  • Include Test Methods
  • Exclude Test Methods
  • Advanced Techniques for Exclude and Include
  • Conclusion
  • FAQ’s on Exclude Include Test Cases in TestNG

Include and exclude features in TestNG allow us to stop and allow tests from executing on the go. This means that if we have written a test that we do not want to execute in some situation, we can simply ignore that test case, test method, or test suite without having to delete it entirely. This way, if we ever need to run these tests again, all we need to do is simply enable them.

Similar Reads

Include Test Methods

@Test annotation...

Exclude Test Methods

Introduction to excluding test methods...

Advanced Techniques for Exclude and Include

Parameterizing test methods inclusion/exclusion...

Conclusion

In this article we covered a lot of things. We looked at how we can configure the testng.xml file to run only the tests that we want. We also look at how we can use the @Ignore annotation and the @Test(enabled=false) to produce the same results. We looked at how we can create groups and include or exclude those specific groups inside the testng.xml file. Hope you have learnt something useful from this article. If you have any queries, please feel free to discuss in the comments below....

FAQ’s on Exclude Include Test Cases in TestNG

Where can I use the @Ignore annotation?...