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

十年热恋 提交于 2019-12-09 14:00:38

问题


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?


回答1:


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




回答2:


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.




回答3:


Making the class abstract should be enough for JUnit 4. If it doesn't work, double check which version of JUnit you're using.

This also communicates your intent that this is just a fragment of a test.

It also prevents JUnit from counting the tests in the class as "ignored" (so the final count of ignored tests will be what you expect).

Alternatively, rename the class. Most of the runners use the class name to determine which classes are tests and which are helpers. In Maven with the default settings, you just have to make sure the base class doesn't begin or end with Test and doesn't end with TestCase (http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html)




回答4:


There are 2 options:

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

  2. You can change the name of the test class that it will not have the word Test (case sensitive) in the start or the end of the test name/or the word TestCase at the end.

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



来源:https://stackoverflow.com/questions/461644/how-to-get-junit-4-to-ignore-a-base-test-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!