我如何以编程方式运行Java应用程序中的所有JUnit测试? [英] How do I programmatically run all the JUnit tests in my Java application?
问题描述
从Eclipse我可以轻松地运行我的应用程序中的所有JUnit测试。
我想能够从应用程序jar运行目标系统上的测试,没有Eclipse(或Ant或Maven或任何其他开发工具)。
我可以看到如何从命令行运行特定的测试或套件。
我可以手动创建一个列出我的应用程序中的所有测试的套件,但这似乎很容易出错 - 我确定在某个时候我会创建一个测试,并忘记添加到套件。
Eclipse JUnit插件有一个向导来创建一个测试套件,但由于某种原因,它不会看到我的测试类。它可能正在寻找JUnit 3测试,而不是JUnit 4注释测试。
我可以编写一个可以通过扫描源文件自动创建套件的工具。 >
或者我可以编写代码,因此应用程序将扫描自己的jar文件进行测试(通过命名约定或通过查找@Test注释)。
似乎应该有一个更简单的方法。我缺少什么?
我遇到了最后一个解决方案的小问题。如果我从Eclipse运行所有测试,他们运行两次,因为他们运行了单独的测试和套件。我可以解决这个问题,但是我意识到有一个更简单的解决方案:
package suneido;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class RunAllTests {
public static void run(String jarfile){
String [] tests = findTests(jarfile);
org.junit.runner.JUnitCore.main(tests);
}
private static String [] findTests(String jarfile){
ArrayList< String> tests = new ArrayList< String>();
try {
JarFile jf = new JarFile(jarfile);
for(枚举< JarEntry> e = jf.entries(); e.hasMoreElements();){
String name = e.nextElement()。getName();
if(name.startsWith(suneido /)&& name.endsWith(Test.class)
&!name.contains($))
tests.add(name.replaceAll(/,。)
.substring(0,name.length() - 6));
}
jf.close();
} catch(IOException e){
throw new RuntimeException(e);
}
return tests.toArray(new String [0]);
}
public static void main(String [] args){
run(jsuneido.jar);
}
}
From Eclipse I can easily run all the JUnit tests in my application.
I would like to be able to run the tests on target systems from the application jar, without Eclipse (or Ant or Maven or any other development tool).
I can see how to run a specific test or suite from the command line.
I could manually create a suite listing all the tests in my application, but that seems error prone - I'm sure at some point I'll create a test and forget to add it to the suite.
The Eclipse JUnit plugin has a wizard to create a test suite, but for some reason it doesn't "see" my test classes. It may be looking for JUnit 3 tests, not JUnit 4 annotated tests.
I could write a tool that would automatically create the suite by scanning the source files.
Or I could write code so the application would scan it's own jar file for tests (either by naming convention or by looking for the @Test annotation).
It seems like there should be an easier way. What am I missing?
I ran into a minor problem with my last solution. If I ran "all tests" from Eclipse they ran twice because they ran the individual tests AND the suite. I could have worked around that, but then I realized there was a simpler solution:
package suneido;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class RunAllTests {
public static void run(String jarfile) {
String[] tests = findTests(jarfile);
org.junit.runner.JUnitCore.main(tests);
}
private static String[] findTests(String jarfile) {
ArrayList<String> tests = new ArrayList<String>();
try {
JarFile jf = new JarFile(jarfile);
for (Enumeration<JarEntry> e = jf.entries(); e.hasMoreElements();) {
String name = e.nextElement().getName();
if (name.startsWith("suneido/") && name.endsWith("Test.class")
&& !name.contains("$"))
tests.add(name.replaceAll("/", ".")
.substring(0, name.length() - 6));
}
jf.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return tests.toArray(new String[0]);
}
public static void main(String[] args) {
run("jsuneido.jar");
}
}
这篇关于我如何以编程方式运行Java应用程序中的所有JUnit测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!