Java JUnit:如何避免测试工具类中的“没有可运行的方法”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/672466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JUnit: how to avoid "no runnable methods" in test utils classes
提问by LiorH
I have switched to JUnit4.4 from JUnit3.8. I run my tests using ant, all my tests run successfully but test utility classes fail with "No runnable methods" error. The pattern I am using is to include all classes with name *Test* under test folder.
我已经从 JUnit3.8 切换到 JUnit4.4。我使用 ant 运行我的测试,我的所有测试都成功运行,但测试实用程序类失败并显示“无可运行方法”错误。我使用的模式是在 test 文件夹下包含名称为 *Test* 的所有类。
I understand that the runner can't find any method annotated with @Test attribute. But they don't contain such annotation because these classes are not tests. Surprisingly when running these tests in eclipse, it doesn't complain about these classes.
我知道跑步者找不到任何用 @Test 属性注释的方法。但是它们不包含这样的注释,因为这些类不是测试。令人惊讶的是,在 eclipse 中运行这些测试时,它并没有抱怨这些类。
In JUnit3.8 it wasn't a problem at all since these utility classes didn't extend TestCase so the runner didn't try to execute them.
在 JUnit3.8 中,这根本不是问题,因为这些实用程序类没有扩展 TestCase,因此运行程序不会尝试执行它们。
I know I can exclude these specific classes in the junit target in ant script. But I don't want to change the build file upon every new utility class I add. I can also rename the classes (but giving good names to classes was always my weakest talent :-) )
我知道我可以在 ant 脚本的 junit 目标中排除这些特定类。但我不想在我添加的每个新实用程序类上更改构建文件。我也可以重命名类(但给类起个好名字总是我最弱的才能:-))
Is there any elegant solution for this problem?
这个问题有什么优雅的解决方案吗?
采纳答案by Jon Skeet
Assuming you're in control of the pattern used to find test classes, I'd suggest changing it to match *Test
rather than *Test*
. That way TestHelper
won't get matched, but FooTest
will.
假设您可以控制用于查找测试类的模式,我建议将其更改为 match*Test
而不是*Test*
. 这种方式TestHelper
不会匹配,但FooTest
会。
回答by akuhn
What about adding an empty test method to these classes?
向这些类添加一个空的测试方法怎么样?
public void avoidAnnoyingErrorMessageWhenRunningTestsInAnt() {
assertTrue(true); // do nothing;
}
回答by JoelPM
Annotate your util classes with @Ignore. This will cause JUnit not to try and run them as tests.
使用 @Ignore 注释您的 util 类。这将导致 JUnit 不会尝试将它们作为测试运行。
回答by gmoore
My specific case has the following scenario. Our tests
我的具体情况有以下情况。我们的测试
public class VenueResourceContainerTest extends BaseTixContainerTest
all extend
全部延长
BaseTixContainerTest
and JUnit was trying to run BaseTixContainerTest. Poor BaseTixContainerTest was just trying to setup the container, setup the client, order some pizza and relax... man.
JUnit 正在尝试运行 BaseTixContainerTest。可怜的 BaseTixContainerTest 只是试图设置容器,设置客户端,订购一些披萨并放松……伙计。
As mentioned previously, you can annotate the class with
如前所述,您可以使用
@Ignore
But that caused JUnit to report that test as skipped (as opposed to completely ignored).
但这导致 JUnit 报告该测试已跳过(而不是完全忽略)。
Tests run: 4, Failures: 0, Errors: 0, Skipped: 1
That kind of irritated me.
那种感觉让我很恼火。
So I made BaseTixContainerTest abstract, and now JUnit truly ignores it.
所以我把 BaseTixContainerTest 抽象化了,现在 JUnit 真的忽略了它。
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
回答by froh42
To prevent JUnit from instantiating your test base class just make it
为了防止 JUnit 实例化你的测试基类,只需让它
public abstract class MyTestBaseClass { ... whatever... }
(@Ignore reports it as ignored which I reserve for temporarilyignored tests.)
(@Ignore 将其报告为已忽略,我保留用于暂时忽略的测试。)
回答by Rahul Vig
I was also facing a similar issue ("no runnable methods..") on running the simplest of simple piece of code (Using @Test, @Before etc.) and found the solution nowhere. I was using Junit4 and Eclipse SDK version 4.1.2. Resolved my problem by using the latest Eclipse SDK 4.2.2. I hope this helps people who are struggling with a somewhat similar issue.
在运行最简单的代码段(使用@Test、@Before 等)时,我也遇到了类似的问题(“没有可运行的方法......”),但无处可寻。我使用的是 Junit4 和 Eclipse SDK 版本 4.1.2。通过使用最新的 Eclipse SDK 4.2.2 解决了我的问题。我希望这可以帮助那些正在为类似问题而苦苦挣扎的人。
回答by mc1arke
Ant now comes with the skipNonTests
attribute which was designed to do exactly what you seem to be looking for. No need to change your base classes to abstract or add annotations to them.
Ant 现在带有skipNonTests
属性,该属性旨在完成您似乎正在寻找的内容。无需更改您的基类来抽象或向它们添加注释。
回答by Sridhar Sarnobat
Be careful when using an IDE's code-completion to add the import for @Test
.
使用 IDE 的代码完成功能为@Test
.
It has to be import org.junit.Test
and not import org.testng.annotations.Test
, for example. If you do the latter, you'll get the "no runnable methods" error.
例如,它必须是import org.junit.Test
而不是import org.testng.annotations.Test
。如果你做后者,你会得到“没有可运行的方法”错误。
回答by Raghu K Nair
- If this is your base test class for example AbstractTest and all your tests extends this then define this class as abstract
- If it is Util class then better remove *Test from the class rename it is MyTestUtil or Utils etc.
- 如果这是您的基本测试类,例如 AbstractTest 并且您的所有测试都扩展了它,则将此类定义为抽象类
- 如果它是 Util 类,那么最好从类中删除 *Test 重命名它是 MyTestUtil 或 Utils 等。
回答by illuminatos
In your test class if wrote import org.junit.jupiter.api.Test; delete it and write import org.junit.Test; In this case it worked me as well.
在你的测试类中如果写了 import org.junit.jupiter.api.Test; 删除它并写入 import org.junit.Test; 在这种情况下,它也对我有用。