11

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?

4 Answers 4

10

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

Sign up to request clarification or add additional context in comments.

Comments

9

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.

Comments

8

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); } } 

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.