Open In App

How to disable an entire unit test in TestNG?

Last Updated : 30 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

TestNG is an integrated testing framework for Java, which provides several features to support automated testing. As with most test management solutions, a clear need has been seen to be able to comment out selected unit tests so that the code containing them is not removed entirely. In this article, I’ll explain to you how you can deactivate an entire unit test in TestNG and the various approaches available.

Introduction

Applying a disable can be necessary, for instance, at the time when a feature you are working on is outside the scope of a temporary project or if a test fails due to the dependency that is not met, or you do not want to run particular tests in a particular environment. TestNG offers several ways to disable tests, the choice depends on the specific situation.

Methods to Disable Unit Tests in TestNG

The good thing about TestNG is that one can disable tests in different ways. Below are the most common methods: Below are the most common methods:

  • Using the '@Test' Annotation with the 'enabled' Parameter
  • Using XML Configuration
  • Using Groups Exclusion

1. Using the '@Test' Annotation with the 'enabled' Parameter

The easiest method to disable a test is by use of the 'enabled' parameter in the '@Test' level as shown below. If the 'enabled' attribute is put to 'false', TestNG will not execute the code of the annotated method.

Example:

Java
import org.testng.annotations.Test;

public class SampleTest {

    @Test(enabled = false)
    public void testMethod1() {
        // This test will be disabled
        System.out.println("This test method is disabled.");
    }

    @Test
    public void testMethod2() {
        // This test will be executed
        System.out.println("This test method is enabled.");
    }
}

In this example 'testMethod1' is ignored and will not be invoked when the test method for the test suite is run but 'testMethod2' will be invoked.

2. Using XML Configuration

Another way to exclude tests is by means of TestNG’s XML configuration. This approach may be useful when you need better control over the test execution and disable/enable tests in an environment or a given context.

Example:

XML
<suite name="Sample Suite">
    <test name="Sample Test">
        <classes>
            <class name="com.example.SampleTest">
                <methods>
                    <exclude name="testMethod1"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>


In this case, we can observe that the 'testMethod1' method is unavailable, which in fact means that this method would not be executed even if there were no comments in it.

3. Using Groups Exclusion

It also possesses facilities for groups which enable you to group and run tests specifically. There must be situations where all tests grouped under some tag should be disabled because they include a definite group of parameters that you do not want to test against.

Example:

Java
import org.testng.annotations.Test;

public class SampleTest {

    @Test(groups = {"skip"})
    public void testMethod1() {
        // This test is part of the 'skip' group
        System.out.println("This test method is in the 'skip' group.");
    }

    @Test
    public void testMethod2() {
        // This test will be executed
        System.out.println("This test method is enabled.");
    }
}

TestNG XML Configuration:

XML
<suite name="Sample Suite">
    <test name="Sample Test">
        <groups>
            <exclude name="skip"/>
        </groups>
        <classes>
            <class name="com.example.SampleTest"/>
        </classes>
    </test>
</suite>

That is why, all tests belong to “skip” group like 'testMethod1' will not performed in this case.

Conclusion

Skipping tests in TestNG can also be easily done, and the following are various ways that one can use to do so. Regardless of whether you like to configure all test execution within the code itself using '@Test' or to use XML configuration along with TestNG groups, they provide the potential of keeping the test suite clean and easily modifiable to meet changes in the course of development.

Enabling tests can in fact help you to concentrate on only the important parts of your test, which indeed can be very effective in large projects where not all tests have to be run through all the time.


Next Article

Similar Reads