Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Junit 4 to ignore a Base Test Class?

Tags:

junit

junit4

I have a base class for many tests that has some helper methods they all need.

It does not by itself have any tests on it, but JUnit (in eclipse) is invoking the test runner on it and complaining that there are no methods to test.

How can I make it ignore this class?

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

Suggestions?

like image 456
Allain Lalonde Avatar asked Jan 20 '09 15:01

Allain Lalonde


People also ask

How do you ignore a test class in JUnit 4?

A second option would be to disable tests temporarily using the JUnit @Ignore annotation. We can add it at the class level to disable all tests in a class: @Ignore("Class not ready for tests") public class IgnoreClassUnitTest { @Test public void whenDoTest_thenAssert() { // ... } }

How do I ignore a test class in JUnit?

If you want to ignore a test method, use @Ignore along with @Test annotation. If you want to ignore all the tests of class, use @Ignore annotation at the class level.

How can you ignore a test script in testing?

Sometimes, it happens that our code is not ready and the test case written to test that method/code fails. In such cases, annotation @Test(enabled = false) helps to disable this test case.

How do you unit test a class that extends a base class?

If you really need to extend the base class to override something, extract the functionality to a third class and make the extended class a proxy. The extended class does nothing else than call methods on the third class. E.g. This way, you can test the real implementation, without instantiating the base class.


2 Answers

Use to @Ignore annotation. It also works on classes. See this one:

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

Also, you can annotate a class containing test methods with @Ignore and none of the containing tests will be executed.

Source: JUnit JavaDoc

like image 161
guerda Avatar answered Sep 18 '22 06:09

guerda


Just as a note, I'd always recommend giving a reason for the ignore:

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

I'm hoping the junit xml report formatter, used when running tests from ant, will one day include the ignored count (and the reasons) along with pass, fail, and error.

like image 28
matt burns Avatar answered Sep 22 '22 06:09

matt burns