在源树中运行所有测试,而不是包

我的unit testing位于与集成测试不同的目录树中,但具有相同的包结构。 我的集成测试需要外部资源(例如服务器),但我的unit testing正确地相互独立和环境。

在IntelliJ-IDEA(v7)中,我定义了一个JUnit运行/调试配置来运行顶级包中的所有测试,这当然会选择失败的集成测试。

我想定义一个运行所有unit testing的run-junit配置。 有任何想法吗?

答案是创建一个测试套件,该套件仅包含unit testing文件夹下的那些测试并运行它。 这是一个名为DirectorySuiteBuilder的junit-addon,但是在我重新发明了这个轮子之后我才发现这个。

它已经在这里被问到了!

import junit.framework.JUnit4TestAdapter; import junit.framework.TestSuite; import java.io.File; import java.io.IOException; public class DirectoryTestSuite { static final String rootPath = "proj\\src\\test\\java\\"; static final ClassLoader classLoader = DirectoryTestSuite.class.getClassLoader(); public static TestSuite suite() throws IOException, ClassNotFoundException { final TestSuite testSuite = new TestSuite(); findTests(testSuite, new File(rootPath)); return testSuite; } private static void findTests(final TestSuite testSuite, final File folder) throws IOException, ClassNotFoundException { for (final String fileName : folder.list()) { final File file = new File( folder.getPath() + "/" +fileName); if (file.isDirectory()) { findTests(testSuite, file); } else if (isTest(file)) { addTest(testSuite, file); } } } private static boolean isTest(final File f) { return f.isFile() && f.getName().endsWith("Test.java"); } private static void addTest(final TestSuite testSuite, final File f) throws ClassNotFoundException { final String className = makeClassName(f); final Class testClass = makeClass(className); testSuite.addTest(new JUnit4TestAdapter(testClass)); } private static Class makeClass(final String className) throws ClassNotFoundException { return (classLoader.loadClass(className)); } private static String makeClassName(final File f) { return f.getPath().replace(rootPath, "").replace("\\", ".").replace(".java", ""); } } 

IntelliJ IDEA CE 10.5有一个(new?)选项可以在配置的目录中运行所有测试:

JUnit运行/调试配置

遗憾的是,除了单个模块中的类和测试类之外,没有办法将输出与IntelliJ编译分开(这是测试运行器正在查看的类)。

因此,当我进行集成测试时,我只需使用特定于这些测试的第二个模块来解决此问题,并根据每个模块的需要指定输出目录。