HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / TestNG / TestNG – Disable or ignore tests

TestNG – Disable or ignore tests

When executing TestNG tests, there may be some scenarios where you may have to disable a particular test or a set of tests from getting executed.

For example, consider a scenario where a serious bug exists in a feature 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.

1. How to disable test

Disabling a test in TestNG can be achieved by setting the enabled attribute of the @Test annotation to false.

@Test( enabled=false )

This will disable the said test method from being executed as part of the test suite. If this attribute is set for the @Test annotation at the class level, all the tests inside the class will be disabled.

2. Example of disabling tests

In below test, we have three test methods i.e. testMethodOne(), testMethodTwo() and testMethodThree(). Out of these testMethodTwo() needs to be disabled.

public class DisableTestDemo 
{
	@Test(enabled = true)
	public void testMethodOne() {
		System.out.println("Test method one.");
	}

	@Test(enabled = false)
	public void testMethodTwo() {
		System.out.println("Test method two.");
	}

	@Test
	public void testMethodThree() {
		System.out.println("Test method three.");
	}
}

Output of above test run is given below:

[TestNG] Running:  C:\Users\somepath\testng-customsuite.xml

Test method one.
Test method three.

PASSED: testMethodOne
PASSED: testMethodThree

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================

As you can see in the previous results, only two methods were executed by TestNG. The method with attribute enabled value as false was ignored from test execution.

By default the attribute value of enabled is true, hence you can see the test method with name testMethodThree() was executed by TestNG even when the attribute value was not specified.

Happy Learning !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

TestNG Tutorial

  • TestNG – Introduction
  • TestNG – Hello World
  • TestNG – Maven
  • TestNG – Annotations
  • TestNG – Expected Exception
  • TestNG – Disable/Ignore Tests
  • TestNG – Parallel Tests
  • TestNG – Dependent Tests
  • TestNG – Timeout Tests
  • TestNG – @Parameters
  • TestNG – @DataProvider
  • TestNG – @Factory
  • TestNG – @DataProvider
  • TestNG – Before and After
  • TestNG – Test Groups

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)