JavaMan
JavaMan

Reputation: 1217

TestNG get all the disabled tests from a class

Because I have so many tests, it would be very helpful if i could somehow check if there are any tests that are "enabled = false", to eliminate the risk of "forgetting" to enable the tests again

So I would need something like this -> check for disabled Tests

I've created a super-Test class, is there a way to check for disabled Tests in the subclasses?

Upvotes: 2

Views: 646

Answers (2)

AutomatedOwl
AutomatedOwl

Reputation: 1089

I have developed a dedicated library which lets you collect all disabled tests in entire project, by inserting the path of test classes. It's done with annotation and codeless test:

@Listeners(DisabledTestsListener.class)
public class InventoryTests {

    @Test
    @DisabledTestsCollector(testsPath = "/src/test/java")
    void getDisabledTest() {
        // This test would collect all disabled tests in TestNG project.
    }

Output example:

Jul 14, 2018 11:57:28 AM com.github.automatedowl.tools.DisabledTestsListener afterInvocation
INFO: You have 2 disabled TestNG tests in your project.
Jul 14, 2018 11:57:28 AM com.github.automatedowl.tools.DisabledTestsListener afterInvocation
INFO: ---------------------------------------------
Jul 14, 2018 11:57:28 AM com.github.automatedowl.tools.DisabledTestsListener lambda$afterInvocation$0
INFO:  firstDisabledTest is a TestNG test which currently disabled.
Jul 14, 2018 11:57:28 AM com.github.automatedowl.tools.DisabledTestsListener lambda$afterInvocation$0
INFO: ---------------------------------------------
Jul 14, 2018 11:57:28 AM com.github.automatedowl.tools.DisabledTestsListener lambda$afterInvocation$0
INFO:  secondDisabledTest is a TestNG test which currently disabled.
Jul 14, 2018 11:57:28 AM com.github.automatedowl.tools.DisabledTestsListener lambda$afterInvocation$0
INFO: ---------------------------------------------

Upvotes: 0

juherr
juherr

Reputation: 5740

Test methods configuration is overriding the class configuration.

So, if you have both:

@Test(enable = false)
public class MyTest {

  @Test
  public void test() {}
}

Then the test will be enabled because the default value of enable is true.

If you want to revert the logic, a solution is to use the IAnnotationTransformer from https://github.com/cbeust/testng/pull/816/

public class TestClassDisabler implements IAnnotationTransformer {

  @Override
  public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor,
                        Method testMethod) {
    if (testMethod != null) {
      Test test = testMethod.getDeclaringClass().getAnnotation(Test.class);
      if (test != null && !test.enabled()) {
        annotation.setEnabled(false);
      }
    }
  }
}

Upvotes: 1

Related Questions