在Eclipse中,我可以轻松地运行应用程序中的所有JUnit测试。
我希望能够在没有Eclipse (或Ant、Maven或任何其他开发工具)的情况下,从应用程序jar在目标系统上运行测试。
我可以看到如何从命令行运行特定的测试或套件。
我可以手动创建一个套件,列出我的应用程序中的所有测试,但这似乎很容易出错-我确信在某个时候我会创建一个测试,然后忘记将其添加到套件中。
Eclipse JUnit插件有一个创建测试套件的向导,但是由于某些原因,它没有“看到”我的测试类。它可能正在寻找JUnit 3测试,而不是JUnit 4注释测试。
我可以编写一个工具,通过扫描源文件自动创建套件。
或者,我可以编写代码,以便应用程序扫描自己的jar文件以进行测试(通过命名约定或查找@Test注释)。
似乎应该有一种更简单的方法。我遗漏了什么?
发布于 2010-03-10 05:34:29
我在上一个解决方案中遇到了一个小问题。如果我在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 (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");
}
}发布于 2010-03-10 13:05:05
根据JUnit邮件列表上的a recent thread,ClasspathSuite可以收集并运行类路径上的所有JUnit测试。这并不完全是您想要的,因为它是一个类级注释,但是源代码是可用的,所以您可以扩展它的内部发现机制。
发布于 2010-03-10 04:59:35
基于http://burtbeckwith.com/blog/?p=52,我得出了以下结论。它似乎工作得很好。
我可以在我的代码中使用以下命令运行它:
org.junit.runner.JUnitCore.main("suneido.AllTestsSuite");一个弱点是它依赖于命名约定("Test“后缀)来识别测试。另一个弱点是jar文件的名称是硬编码的。
package suneido;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.model.InitializationError;
/**
* Discovers all JUnit tests in a jar file and runs them in a suite.
*/
@RunWith(AllTestsSuite.AllTestsRunner.class)
public final class AllTestsSuite {
private final static String JARFILE = "jsuneido.jar";
private AllTestsSuite() {
}
public static class AllTestsRunner extends Suite {
public AllTestsRunner(final Class<?> clazz) throws InitializationError {
super(clazz, findClasses());
}
private static Class<?>[] findClasses() {
List<String> classFiles = new ArrayList<String>();
findClasses(classFiles);
List<Class<?>> classes = convertToClasses(classFiles);
return classes.toArray(new Class[classes.size()]);
}
private static void findClasses(final List<String> classFiles) {
JarFile jf;
try {
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("$"))
classFiles.add(name.replaceAll("/", ".")
.substring(0, name.length() - 6));
}
jf.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static List<Class<?>> convertToClasses(
final List<String> classFiles) {
List<Class<?>> classes = new ArrayList<Class<?>>();
for (String name : classFiles) {
Class<?> c;
try {
c = Class.forName(name);
}
catch (ClassNotFoundException e) {
throw new AssertionError(e);
}
if (!Modifier.isAbstract(c.getModifiers())) {
classes.add(c);
}
}
return classes;
}
}
}https://stackoverflow.com/questions/2411938
复制相似问题