romeok
romeok

Reputation: 681

Does @Test(enabled = false) work for a class in TestNG?

From the TestNG doc I can see that (enabled = false) can be applied to a class or method. But it seems it only works when applied to a method.

Anybody seen the same, found a solution?

Upvotes: 8

Views: 19383

Answers (1)

Cedric Beust
Cedric Beust

Reputation: 15608

It seems to work for me:

@Test(enabled = false)
public class B {

    public void btest1() {
        System.out.println("B.btest1");
    }

}

Result:

===============================================
SingleSuite
Total tests run: 0, Failures: 0, Skips: 0
===============================================

Changing false to true:

B.btest1

===============================================
SingleSuite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

Possible reason

Here is what might be tripping you (hard to tell since you didn't provide any code):

@Test(enabled = false)
public class B {

    @Test
    public void btest1() {
        System.out.println("B.btest1");
    }

}

This case will run the test because by repeating the @Test annotation on the method, you are also overriding the enabled attribute to its default value, which is true.

The solution is to reiterate enabled=false at the method level:

@Test(enabled = false)
public class B {

    @Test(enabled = false)
    public void btest1() {
        System.out.println("B.btest1");
    }

}

I'm aware it's a bit counterintuitive but it's necessary in order to be consistent in the way method annotations can override class annotations.

Upvotes: 14

Related Questions