Java 在 Junit 4 中运行所有测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2255046/
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
Run all tests in Junit 4
提问by alexloh
I want to be able to run all tests in a project programmatically. I know Eclipse has a "Run as JUnit test" configuration which somehow grabs all the tests in a project and run them. Is there any way for me to also grab the list of tests programmatically and run them? Or is there some good way to construct a test suite containing all the test cases without manually listing out every one (all 700+) of them?
我希望能够以编程方式运行项目中的所有测试。我知道 Eclipse 有一个“作为 JUnit 测试运行”的配置,它以某种方式获取项目中的所有测试并运行它们。有什么方法可以让我以编程方式获取测试列表并运行它们吗?或者有什么好方法可以构建一个包含所有测试用例的测试套件,而无需手动列出所有测试用例(全部 700 多个)?
I've tried the "New... -> Test Suite" option in Eclipse, but that seems to work only for JUnit 3, identifying tests by their extending from TestCase
我已经在 Eclipse 中尝试了“New... -> Test Suite”选项,但这似乎只适用于 JUnit 3,通过从 TestCase 扩展来识别测试
The test classes are JUnit 4, so their only distinguishing characteristic is the annotation, no naming convention, no subclassing from TestCase.
测试类是 JUnit 4,所以它们唯一的区别特征是注释,没有命名约定,没有来自 TestCase 的子类。
Thanks in advance!
提前致谢!
采纳答案by Fabian Steeg
Though it does not really solve your immediate problem, I find it a very useful general practice to create suites and suites of suites, e.g. for a package something like PackageFooSuite
etc. and assemble these suites in one or more suites again, like ModuleFooSuite
and have one top-level suite, like AllTestsSuite
. That way it's easy to run both all tests in one step as well as submodule tests for the package I'm currently working on (and have the tests run quicker than if I would always run all of them):
虽然它并没有真正解决您当前的问题,但我发现创建套件和套件套件是一种非常有用的一般做法,例如,对于诸如此类的包,PackageFooSuite
然后将这些套件再次组装在一个或多个套件中,例如ModuleFooSuite
并拥有一个顶部级套房,如AllTestsSuite
. 这样就可以轻松地在一个步骤中运行所有测试以及我当前正在处理的包的子模块测试(并且测试运行速度比我总是运行所有测试更快):
@RunWith(Suite.class)
@Suite.SuiteClasses({ PackageFooSuite.class, PackageBarSuite.class} )
public final class AllTestsSuite {} // or ModuleFooSuite, and that in AllTests
回答by davek
You can do this fairly easily from within maven using the surefire plugin: I usually clean/compile/install my projects from the command line before comparing them for eclipse usage (mvn eclipse:clean eclipse:eclipse
) and you can define a test suite in your pom which lists all the tests you want to run en masse every time you run mvn install
. You're not calling them programatically, exactly, but you can certainly call them en masse.
您可以使用surefire插件在maven中轻松完成此操作:我通常在比较eclipse使用情况之前从命令行清理/编译/安装我的项目(mvn eclipse:clean eclipse:eclipse
),您可以在pom中定义一个测试套件,其中列出所有测试每次跑步时都想集体跑步mvn install
。您不是完全以编程方式调用它们,但您当然可以整体调用它们。
回答by Axel Fontaine
Of the top of my head using Spring:
在我的头顶使用 Spring:
- Implement a TypeFilter that matches classes with methods annotated with @Test (don't forget to consider the superclasses)
- Invoke classpath scanning on your top-most test package
- Invoke the JUnitRunner with the scan results
- 实现一个 TypeFilter ,它匹配带有 @Test 注释的方法的类(不要忘记考虑超类)
- 在最顶层的测试包上调用类路径扫描
- 使用扫描结果调用 JUnitRunner
More info on classpath scanning and custom type filters here
关于类路径扫描和自定义类型过滤器的更多信息在这里
回答by cmcginty
I also recommend using the JUnit Suite annotations. Follow the link for more detail.
我还建议使用 JUnit Suite 注释。按照链接,更详细。
回答by Mookie Wilson
With Eclipse Indigo (possibly Helios as well) in the Run Configurations dialog box, you now have the ability to Run all tests in a selected project, package or source folder.
使用“运行配置”对话框中的 Eclipse Indigo(也可能是 Helios),您现在可以在选定的项目、包或源文件夹中运行所有测试。
Also a good reference from Eclipse is the article Java Unit testing with JUnit 4.x in Eclipse.
同样来自 Eclipse 的一个很好的参考是文章Java Unit testing with JUnit 4.x in Eclipse。
回答by eis
None of the other answers did it for me. I had 40k tests I needed to run, so manually listing every class was not an option.
其他答案都没有为我做到。我需要运行 40k 个测试,因此手动列出每个类不是一个选项。
I did it with ClasspathSuite. A test suite that runs all Junit4 and Junit3 test cases in the class path is as follows:
我用ClasspathSuite做到了。在类路径中运行所有 Junit4 和 Junit3 测试用例的测试套件如下:
import org.junit.extensions.cpsuite.ClasspathSuite;
import org.junit.extensions.cpsuite.ClasspathSuite.*;
import org.junit.runner.RunWith;
import org.junit.runner.JUnitCore;
import static org.junit.extensions.cpsuite.SuiteType.*;
@RunWith(ClasspathSuite.class)
@SuiteTypes({ JUNIT38_TEST_CLASSES, TEST_CLASSES })
public class RunAllSuite {
/* main method not needed, but I use it to run the tests */
public static void main(String args[]) {
JUnitCore.runClasses(RunAllSuite.class);
}
}
I needed to run it from command line, so this is what I did:
我需要从命令行运行它,所以这就是我所做的:
- Downloaded cp-1.2.6.jar
- Create the previously mentioned RunAllSuite
- Compile the class,
javac RunAllSuite.java -cp cpsuite-1.2.6.jar;junit-4.8.1.jar
- run it with target tests in the class path,
java -cp cpsuite-1.2.6.jar;junit-4.8.1.jar;path/to/runallsuite/folder;target/classes;target/test-classes RunAllSuite
- 下载cp-1.2.6.jar
- 创建前面提到的 RunAllSuite
- 编译类,
javac RunAllSuite.java -cp cpsuite-1.2.6.jar;junit-4.8.1.jar
- 在类路径中使用目标测试运行它,
java -cp cpsuite-1.2.6.jar;junit-4.8.1.jar;path/to/runallsuite/folder;target/classes;target/test-classes RunAllSuite
And that's it. With the RunAllSuite above, anywhere in your code you can just do JUnitCore.runClasses(RunAllSuite.class), which runs all tests in class path. There are other config options as well which are explained in the ClasspathSuite home page.
就是这样。使用上面的 RunAllSuite,您可以在代码的任何位置执行 JUnitCore.runClasses(RunAllSuite.class),它会在类路径中运行所有测试。还有其他配置选项,在 ClasspathSuite 主页中有解释。
Note also that the class given above does not print anything. If that is needed, you can do
另请注意,上面给出的类不打印任何内容。如果需要,你可以这样做
import org.junit.extensions.cpsuite.ClasspathSuite;
import org.junit.extensions.cpsuite.ClasspathSuite.*;
import org.junit.runner.RunWith;
import org.junit.runner.JUnitCore;
import org.junit.internal.TextListener;
import static org.junit.extensions.cpsuite.SuiteType.*;
@RunWith(ClasspathSuite.class)
@SuiteTypes({ JUNIT38_TEST_CLASSES, TEST_CLASSES })
public class RunAllSuite {
public static void main(String args[]) {
JUnitCore junit = new JUnitCore();
junit.addListener(new TextListener(System.out));
junit.run(RunAllSuite.class);
}
}
回答by Carlos Rendon
In Eclipse (I'm using 4.6.1) - Right click the project folder, select "Run As", choose "JUnit Test"
在 Eclipse 中(我使用的是 4.6.1) - 右键单击项目文件夹,选择“Run As”,选择“JUnit Test”
It will run all tests in that project. Same for a package.
它将运行该项目中的所有测试。一个包也一样。