Java 您可以从命令行运行一个包中的所有 JUnit 测试而不明确列出它们吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24510742/
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
Can you run all JUnit tests in a package from the command line without explicitly listing them?
提问by Christian
Provided the test classes and JUnit are both on the classpath, one can run JUnit tests from the command line as follows:
如果测试类和 JUnit 都在类路径上,则可以从命令行运行 JUnit 测试,如下所示:
java org.junit.runner.JUnitCore TestClass1 TestClass2
Now, is there a way to run all tests in a package (and sub-packages) as well?
现在,有没有办法在一个包(和子包)中运行所有测试?
I'm looking for something like
我正在寻找类似的东西
java org.junit.runner.JUnitCore com.example.tests.testsIWantToRun.*
Is there an easy way of doing that (that doesn't involve maven or ant)?
有没有一种简单的方法来做到这一点(不涉及 maven 或 ant)?
回答by Kenster
Junit lets you define suitesof tests. Each suite defines a collection of tests, and running the suite causes all of the tests to be run. What I do is to define a suite for each package, listing the test classes for that package along with the suites for any sub-packages:
Junit 允许您定义测试套件。每个套件定义了一组测试,运行套件会导致所有测试都运行。我所做的是为每个包定义一个套件,列出该包的测试类以及任何子包的套件:
package com.foo.bar;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import com.foo.bar.baz.Suite_baz;
@RunWith(Suite.class)
@Suite.SuiteClasses({
ThisTest.class,
ThatTest.class,
TheOtherTest.class,
Suite_baz.class,
})
public class Suite_bar {
}
This isn't completely effortless. You have to construct the suites and manually update them with new test classes. I suppose it wouldn't be hard to write a little java program to generate these automatically, if someone wanted to.
这并非完全不费吹灰之力。您必须构建套件并使用新的测试类手动更新它们。我想如果有人愿意的话,编写一个小 Java 程序来自动生成这些并不难。
回答by Christian
I asked this question to be able to kick sets of a project's Cucumber tests on Jenkins off selectively, without really knowing what their RunTests classes would be called, what their CucumberOptions would contain, or where they would be located. I found a few helpful threads on StackOverflow in the meantime, which answer my question:
我问这个问题是为了能够有选择地在 Jenkins 上启动项目的 Cucumber 测试集,而无需真正知道它们的 RunTests 类将被调用什么,它们的 CucumberOptions 将包含什么,或者它们将位于何处。与此同时,我在 StackOverflow 上找到了一些有用的线程,它们回答了我的问题:
- How do I Dynamically create a Test Suite in JUnit 4?
- How to run multiple test classes with junit from command line?
Using those, I can kick my Cucumber tests off individually as follows:
使用这些,我可以按如下方式单独启动我的 Cucumber 测试:
First, I used the maven assembly plugin to get the tests packaged in a jar: https://stackoverflow.com/a/574650/2018047
首先,我使用maven assembly插件将测试打包成jar:https: //stackoverflow.com/a/574650/2018047
Then I copied the tests' dependencies to the target folder on Jenkins, as shown here: https://stackoverflow.com/a/23986765/2018047
然后我将测试的依赖项复制到 Jenkins 上的目标文件夹中,如下所示:https: //stackoverflow.com/a/23986765/2018047
We already have a flag that skips the execution of our tests when it's set, so I package my tests without running them:
mvn clean install -DskipMyTestModule=true
我们已经有一个标志,它在设置时跳过我们的测试的执行,所以我打包我的测试而不运行它们:
mvn clean install -DskipMyTestModule=true
And using the code from above and the invocation from below, I'll be able to make it all work...
并使用上面的代码和下面的调用,我将能够使其全部工作......
java -Dcucumber.options="src/test/resources/features --tags @b --format pretty:STDOUT --format html:target/cucumber-b --format json:target/cucumber-b.json" -Dname=value -cp target/artifact-1.2.8-SNAPSHOT-tests.jar;target/test-classes/libs/junit-4.11.jar;target/test-classes/libs/* org.junit.runner.JUnitCore com.example.foo.bar.test.cucumber.RunTest
Hope this helps someone in the future. :)
希望这对未来的人有所帮助。:)
回答by Omri Spector
With JUnit 4 this is supported using an extension called cpsuite. All you need to do is add it to your test classpath (maven io.takari.junit:takari-cpsuite
), create a dynamic test suite class:
在 JUnit 4 中,使用名为 cpsuite 的扩展支持这一点。您需要做的就是将它添加到您的测试类路径 (maven io.takari.junit:takari-cpsuite
),创建一个动态测试套件类:
package com.mycompany;
import org.junit.extensions.cpsuite.ClasspathSuite;
import org.junit.runner.RunWith;
@RunWith(ClasspathSuite.class)
@ClasspathSuite.IncludeJars(true)
public class RunAllTests {}
and run it:
并运行它:
java -cp ${CP} org.junit.runner.JUnitCore com.mycompany.RunAllTests
Your classpath ${CP}
should include your test jar, junit, hamcrest and cpsuite.
你的类路径${CP}
应该包括你的测试 jar、junit、hamcrest 和 cpsuite。