How to make Junit 4 ignore the base test class?

I have a base class for many tests in which there are some helper methods that they need.

It itself does not have any tests, but JUnit (in eclipse) calls the test runner on it and complains that there are no methods for testing.

How can I make it ignore this class?

I know that I could add a dummyTest method that would solve the problem, but it will also appear for all child classes.

Suggestions?

+41
junit junit4
Jan 20 '09 at 3:00 p.m.
source share
4 answers

Use annotations for @Ignore . He also works in the classroom. See This:

 @Ignore public class IgnoreMe { @Test public void test1() { ... } @Test public void test2() { ... } } 

In addition, you can annotate a class containing testing methods using @Ignore and none of the contained tests will be executed.

Source: JUnit JavaDoc

+66
Jan 20 '09 at 15:12
source share
β€” -

As a note, I always recommend indicating a reason for ignoring:

 @Ignore("This test will prove bug #123 is fixed, once someone fixes it") 

I hope that the junit xml report formatter used when running tests from ant will one day turn on the ignored counter (and reasons) along with a skip, crash, and error.

+20
Jan 29 '09 at 13:05
source share

Making the abstract class should be sufficient for JUnit 4. If it does not work, double-check which version of JUnit you are using.

It also links your intention that this is just a piece of test.

It also prohibits JUnit from treating tests in the class as β€œignored” (so the final count of ignored tests will be what you expect).

Rename the class as an alternative. Most runners use the class name to determine which classes are tests and which helpers. In Maven with default settings, you just need to make sure that the base class does not start or end with Test and does not end with TestCase ( http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion .html )

+11
Mar 26 '13 at 15:27
source share

There are 2 options:

  • You should create the base test class abstract , which Junit4 got enough. If you use the @Igonre attribute, it will show it as an ignored test (and add it to the total number of tests).

  • You can change the name of the test class in which it does not have the word Test (case sensitive) at the beginning or at the end of the test name / or the word TestCase at the end.

BTW: @Igonre for another use case when you want to ignore a specific test.

0
Dec 26 '17 at 14:14
source share



All Articles