Manoj
Manoj

Reputation: 5612

is there any conditional annotation in JUnit to mark few test cases to be skipped?

As far as I know to skip a test case the simplest thing to do is to remove the @Test annotation, but to do it over a large number of test cases is cumbersome. I was wondering if there is any annotation available in JUnit to turn off few test cases conditionally.

Upvotes: 22

Views: 17364

Answers (5)

Hagay
Hagay

Reputation: 1

You can use assumeTrue(your_condition) to skip the test if the condition is false. In the following example junit will skip the test if the file doesn't exist.

File file = new File("path/to/your/file");
assumeTrue(file.exists());

Upvotes: 0

MarcoS
MarcoS

Reputation: 13564

If you use JUnit 4.x, just use @Ignore. See here

Upvotes: 2

Jonathan Holloway
Jonathan Holloway

Reputation: 63672

As other people put here @Ignore ignores a test.

If you want something conditional that look at the junit assumptions.

http://junit.sourceforge.net/javadoc/org/junit/Assume.html

This works by looking at a condition and only proceeding to run the test if that condition is satisfied. If the condition is false the test is effectively "ignored".

If you put this in a helper class and have it called from a number of your tests you can effectively use it in the way you want. Hope that helps.

Upvotes: 8

Kaj
Kaj

Reputation: 10949

Hard to know if it is the @Ignore annotation that you are looking for, or if you actually want to turn off certain JUnit tests conditionally. Turning off testcases conditionally is done using Assume.

You can read about assumptions in the release notes for junit 4.5

There's also a rather good thread here on stack over flow: Conditionally ignoring tests in JUnit 4

Upvotes: 29

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298898

You can use the @Ignore annotation which you can add to a single test or test class to deactivate it.

If you need something conditional, you will have to create a custom test runner that you can register using

@RunWith(YourCustomTestRunner.class)

You could use that to define a custom annotation which uses expression language or references a system property to check whether a test should be run. But such a beast doesn't exist out of the box.

Upvotes: 5

Related Questions