java JUnit 和 junit.framework.TestSuite - 没有可运行的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/190224/
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 and junit.framework.TestSuite - No runnable methods
提问by bernhardrusch
I've made some unit tests (in test class). The tutorial I've read said that I should make a TestSuite for the unittests.
我做了一些单元测试(在测试课中)。我读过的教程说我应该为单元测试制作一个 TestSuite。
Odd is that when I'm running the unit test directly (selecting the test class - Run as jUnit test) everything is working fine, altough when I'm trying the same thing with the test suite there's always an exception: java.lang.Exception: No runnable methods.
奇怪的是,当我直接运行单元测试时(选择测试类 - 作为 jUnit 测试运行)一切正常,尽管当我用测试套件尝试同样的事情时,总是有一个例外:java.lang。例外:没有可运行的方法。
Here is the code of the test suite:
这是测试套件的代码:
import junit.framework.Test;
import junit.framework.TestSuite;
public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test suite for com.xxx.yyyy.test");
//$JUnit-BEGIN$
suite.addTestSuite(TestCase.class);
//$JUnit-END$
return suite;
}
}
Any ideas why this isn't working ?
任何想法为什么这不起作用?
采纳答案by bernhardrusch
I'm not experienced in ant - so I'm not using it for testing it right now.
我在 ant 方面没有经验 - 所以我现在不使用它来测试它。
Searching the internet it seems like I'm mixing up the old jUnit 3.8 and jUnit 4.0 behavior. Trying now a way to use the "new behavior"
在互联网上搜索,似乎我正在混淆旧的 jUnit 3.8 和 jUnit 4.0 行为。现在尝试一种使用“新行为”的方法
edited:
now it works:
编辑:
现在它的工作原理:
AllTest changed to:
AllTest 更改为:
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(value=Suite.class)
@SuiteClasses(value={TestCase.class})
public class AllTests {
}
TestCase changed to:
测试用例更改为:
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class TestCase {
@Test
public void test1 {
assertTrue (tmp.getTermin().equals(soll));
}
}
回答by bernhardrusch
Took me a bit too to figure it out, but I think this solves your problem:
我也花了一点时间才弄清楚,但我认为这可以解决您的问题:
You're doing a suite.addTestSuite(TestCase.class), while you should've done a suite.addTest(TestCase.class).
你正在做一个suite.addTestSuite(TestCase.class),而你应该做一个suite.addTest(TestCase.class)。
You can also add a testsuite to a testsuite to create a whole hierarchy of testsuites. In that case you'll have to use suite.addTest(). But note that you then use .suite() and not .class: suite.addTest(MyTestSuite.suite())!
您还可以将测试套件添加到测试套件以创建整个测试套件层次结构。在这种情况下,您将不得不使用suite.addTest()。但请注意,您随后使用 .suite() 而不是 .class:suite.addTest(MyTestSuite.suite ())!
回答by AmiNadimi
Be careful when using an IDE's code-completion to add the import for @Test. It has to be import org.junit.Testand notimport org.testng.annotations.Test, for example. If you use the second one by mistake, you'll get the "no runnable methods" error.
(I was using Intellij Idea 2017 which imported org.junit.jupiter.api.Testinstead!)
使用 IDE 的代码完成添加导入时要小心@Test。例如,它必须是import org.junit.Test而不是import org.testng.annotations.Test。如果您错误地使用了第二个方法,您将收到“没有可运行的方法”错误。(我使用的是导入的 Intellij Idea 2017 org.junit.jupiter.api.Test!)
回答by gizmo
For sure, it won't work since you're not telling the test suite what are your test classes.
当然,它不会起作用,因为您没有告诉测试套件您的测试类是什么。
But I'm wondering why you're not using the "classical way" for building Test suites, which is ant using jUnit's ant tasks.
但我想知道你为什么不使用“经典方式”来构建测试套件,这是使用 jUnit 的 ant 任务的 ant。

