Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
December 14, 2016

Enabling and Disabling TestNG Test





In some scenarios you may need NOT to Execute/Run some of the tests or set of tests from getting executed in a particular class. For example, consider a scenario where a serious bug exists in application due to certain tests belonging to certain scenarios that cannot be executed. As the issue has already been identified we may need to disable the said test scenarios from being executed.

Disabling a test can be achieved in 2 ways. One is from testng.xml configuration file and the second is from the Test annotation property/attribute (i.e. enabled). These will disable the said test method from being executed as part of the test suite. If this attribute is set to false for the Test annotation at class level, all the public methods inside the class will be disabled.

1.Using Test annotation enabled property/attribute:

Will see sample program for how to disable and enable particular tests in a class using enabled property of Test annotation. If the enabled=true then the test will execute and if enabled=false then the test will NOT execute.


import org.testng.annotations.Test;


public class DisableAndEnableTests
{
    /**
     * This test will be executed as the enabled property is true.
     */
    @Test(enabled=true)
    public void firstTest()
    {
        System.out.println("In First Test");
    }
    
    /**
     * This test will NOT be executed as the enabled property is false.
     */
    @Test(enabled=false)
    public void secondTest()
    {
        System.out.println("In Second Test");
    }
    
    /**
     * This test will NOT be executed as the enabled property is false.
     */
    @Test(enabled=false)
    public void thirdTest()
    {
        System.out.println("In Third Test");
    }
}

Now Execute the above code using below xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test">
    <classes>
      <class name="DisableAndEnableTests"/>
    </classes>
  </test>
</suite>

Output for the above program is:

EnableAndDisableTestsOutput



2.Using Include and Exclude tags in the testng.xml file:

Will see sample program for how to disable and enable particular tests in a class using include and exclude tags from testng.xml file.

import org.testng.annotations.Test;

public class DisableAndEnableTests
{
    @Test
    public void firstTest()
    {
        System.out.println("In First Test");
    }
    
    @Test
    public void secondTest()
    {
        System.out.println("In Second Test");
    }
    
    @Test
    public void thirdTest()
    {
        System.out.println("In Third Test");
    }
}

Now Execute the above code using below xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test">
    <classes>
        <class name="DisableAndEnableTests">
              <methods>
                <include name="firstTest"/>
                <exclude name="secondTest"/>
                <exclude name="thirdTest"/>
            </methods>
        </class>
    </classes>
  </test>
</suite>

Output for the above program is:

EnableAndDisableTestsOutput

In the above programs we have three tests in the class. But after execution we are seeing only one test is getting executed(i.e. firstTest) and remaining tests (i.e. secondTest and thirdTest) are skipped from the execution.

This way we can disable or enable some of the tests from the execution according to our requirement.



Share this post: on Twitter on Facebook

Before And After Annotations TestNG Test Annotation

Related Posts

Excel to DataProvider

TestNG

Read data from Excel to DataProvider in Selenium

Running TestNG programmatically

TestNG

Running TestNG Tests Programmatically

EXECUTING ONLY FAILED TESTS

TestNG

Executing Only Failed Tests in TestNG

Capture Screenshot for Failed Tests

TestNG

Capture Screenshot for Failed Tests in TestNG

Preserve Order in TestNG

TestNG

Preserve Order in TestNG

PRIORITIZING TESTS

TestNG

Prioritizing Tests in TestNg

EXECUTE MULTIPLE XML FILES

TestNG

Execute Multiple XML files in TestNG

CUSTOM REPORTER IN TESTNG (1)

TestNG

Custom Reporter in TestNG

CUSTOM LOGGER IN TESTNG

TestNG

Custom Logger in TestNG

ASSERTIONS

TestNG

TestNG Assertions

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2025