@Test Annotation attributes – enabled attribute

@Test annotation has many attributes, and enabled is also one of them. In this post, we will discuss the enabled attribute in detail.

What does the enabled attribute do? When its value is false, that test case won’t be executed.

Without using the enabled attribute

Let us see how things work without using the enabled attribute first with the help of CodekruTest class.

import org.testng.Assert;
import org.testng.annotations.Test;

public class CodekruTest {

	@Test()
	public void test1() {

		System.out.println("test1 is passed");
		Assert.assertTrue(true);
	}

	@Test()
	public void test2() {

		System.out.println("test2 is passed");
		Assert.assertTrue(true);
	}

}

Output –

test1 is passed
test2 is passed
PASSED: test1
PASSED: test2

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

In the above scenario, both test cases are executed and now, let’s see what happens if we use enabled = false on the test1 method.

Now, with using enabled attribute
import org.testng.Assert;
import org.testng.annotations.Test;

public class CodekruTest {

	@Test(enabled = false) // now this test won't run
	public void test1() {

		System.out.println("test1 is passed");
		Assert.assertTrue(true);
	}

	@Test()
	public void test2() {

		System.out.println("test2 is passed");
		Assert.assertTrue(true);
	}

}

Output –

test2 is passed
PASSED: test2

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

We used “enabled = false” on the test1 method, and because of this, the test1() method wasn’t executed, but test2() did.

Can we use the enabled attribute to ignore all cases within a class?

The answer is yes. As illustrated below, we can easily do that by placing the @Test annotation on the class level.

import org.testng.Assert;
import org.testng.annotations.Test;

@Test(enabled = false)
public class Codekru {

	public void test1() {

		System.out.println("test1 is passed");
		Assert.assertTrue(true);
	}

	public void test2() {

		System.out.println("test2 is passed");
		Assert.assertTrue(true);
	}

}

Output –


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

If you want to know more about the class level annotation in TestNG, we suggest you read this post. And if you want to learn more about ignoring the test cases in TestNG, you can give this article a try.

We hope that you liked this article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Other @Test attributes
Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *