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.
它本身并没有对它进行任何测试,但JUnit(在eclipse中)正在调用它上面的测试运行器,并抱怨没有方法可以测试。
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.
我知道我可以添加一个可以解决问题的dummyTest方法,但它也会出现在所有子类中。
Suggestions?
4 个解决方案
#1
Use to @Ignore
annotation. It also works on classes. See this one:
用于@Ignore注释。它也适用于类。看到这个:
@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.
此外,您可以使用@Ignore注释包含测试方法的类,并且不会执行任何包含的测试。
Source: JUnit JavaDoc
来源: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.
我希望从ant运行测试时使用的junit xml报告格式化程序有一天会包含忽略的计数(以及原因)以及通过,失败和错误。
#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.
使类抽象应该足够JUnit 4.如果它不起作用,请仔细检查您正在使用的JUnit版本。
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).
它还可以防止JUnit将类中的测试计为“忽略”(因此忽略的测试的最终计数将是您所期望的)。
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)
或者,重命名该类。大多数跑步者使用类名来确定哪些类是测试,哪些是帮助。在具有默认设置的Maven中,您只需确保基类不以Test开头或结尾,并且不以TestCase结束(http://maven.apache.org/surefire/maven-surefire-plugin/实例/包涵体exclusion.html)
#4
There are 2 options:
有两种选择:
-
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).
你应该创建基础测试类抽象,这足以得到Junit4。如果使用@Igonre属性,它会将其显示为忽略的测试(并将其添加到测试的总数中)。
-
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.
您可以更改测试类的名称,该测试类在测试名称的开头或结尾处不会包含单词Test(区分大小写)或最后的单词TestCase。
BTW: @Igonre is for a different use case, when you want to ignore a specific test.
顺便说一句:@Igonre是针对不同的用例,当你想忽略一个特定的测试时。
#1
Use to @Ignore
annotation. It also works on classes. See this one:
用于@Ignore注释。它也适用于类。看到这个:
@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.
此外,您可以使用@Ignore注释包含测试方法的类,并且不会执行任何包含的测试。
Source: JUnit JavaDoc
来源: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.
我希望从ant运行测试时使用的junit xml报告格式化程序有一天会包含忽略的计数(以及原因)以及通过,失败和错误。
#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.
使类抽象应该足够JUnit 4.如果它不起作用,请仔细检查您正在使用的JUnit版本。
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).
它还可以防止JUnit将类中的测试计为“忽略”(因此忽略的测试的最终计数将是您所期望的)。
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)
或者,重命名该类。大多数跑步者使用类名来确定哪些类是测试,哪些是帮助。在具有默认设置的Maven中,您只需确保基类不以Test开头或结尾,并且不以TestCase结束(http://maven.apache.org/surefire/maven-surefire-plugin/实例/包涵体exclusion.html)
#4
There are 2 options:
有两种选择:
-
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).
你应该创建基础测试类抽象,这足以得到Junit4。如果使用@Igonre属性,它会将其显示为忽略的测试(并将其添加到测试的总数中)。
-
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.
您可以更改测试类的名称,该测试类在测试名称的开头或结尾处不会包含单词Test(区分大小写)或最后的单词TestCase。
BTW: @Igonre is for a different use case, when you want to ignore a specific test.
顺便说一句:@Igonre是针对不同的用例,当你想忽略一个特定的测试时。