Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 318
romeok Avatar asked Jul 19 '09 03:07

romeok


1 Answers

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.

like image 162
Cedric Beust Avatar answered Nov 03 '22 16:11

Cedric Beust