Galet
Galet

Reputation: 6299

How to skip or ignore execution of tests in TestNG

I am writing test in java using TestNG.

I want to skip or ignore a all class methods using conditional inside the class file.

In ruby, I have followed this How to skip certain tests with Test::Unit

How can I do in java?

Upvotes: 10

Views: 28907

Answers (4)

Mehul Aggarwal
Mehul Aggarwal

Reputation: 56

Taking reference from this article.

Using the @Ignore annotation, you can easily ignore a single test case or all cases inside a class and its subclasses.

  • Ignoring a single test case
    
    @Test @Ignore
    public void test1() {
        System.out.println("Excecuting test1");
        Assert.assertTrue(true);
    }
     
    @Test
    public void test2() {
        System.out.println("Excecuting test2");
        Assert.assertTrue(true);
    }
     
}

The test1() method would be ignored here, and test2() would run.

  • Ignoring all cases of a class and its subclasses
@Ignore
public class CodekruTest {

    @Test
    public void test1() {
        System.out.println("Excecuting test1");
        Assert.assertTrue(true);
    }

    @Test
    public void test2() {
        System.out.println("Excecuting test2");
        Assert.assertTrue(true);
    }

}

No test case would be executed here. And this not only extends to CodekruTest class but its subclasses as well.

Please refer to this article for more insights on how to ignore the cases in TestNG.

Upvotes: 1

Raffaele
Raffaele

Reputation: 20885

You can throw SkipException's in your tests if assumptions does not hold, and that mechanism is flexible enough to even mark the test as skipped or failed. This article shows how to integrate this approach in a declarative manner in your test suite. Basically you annotate methods with @Assumes and have a custom IInvokedMethodListener.

Or (I don't encourage this but it's an option), if you can determine what to skip statically, you may generate an XML spec on the fly and run it.

Upvotes: 9

juherr
juherr

Reputation: 5740

Another solution is to use an IAnnotationTransformers and then disable the test like you can do with @Test(enabled = false).

public void transform(ITest annotation, Class testClass,
      Constructor testConstructor, Method testMethod) {
    if (...determine if the test should be disabled) {
        annotation.setEnable(false);
    }
}

Upvotes: 7

hasnae
hasnae

Reputation: 2183

You can ignore test method using the annotation @Test(enabled = false)

Upvotes: 9

Related Questions