Run all tests in the source tree, not in the package

My unit tests are in a separate directory tree from my integration tests, but with the same package structure. My integration tests require external resources (such as a server), but my unit tests are independent of each other and the environment.

In IntelliJ-IDEA (v7), I defined a JUnit run / debug configuration to run all the tests in a top-level package, and this, of course, takes away my integration tests that fail.

I want to define a run-junit configuration that runs all my unit tests. Any ideas?

+5
java intellij-idea unit-testing junit
Aug 18 '09 at 13:33
source share
3 answers

The answer is to create a test suite that contains only those tests under the unit test folder and runs them instead. There is a junit-addon that does this only with DirectorySuiteBuilder , but I only found it after I invented the wheel a lot.

And it has already been asked here!

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", ""); } } 
+5
Aug 18 '09 at 15:29
source share

IntelliJ IDEA CE 10.5 has a (new?) Option to run all tests inside a configured directory:

JUnit Run / Debug configuration

+5
May 31 '11 at 13:32
source share

Unfortunately, there is no way to separate the output from the IntelliJ compilation, different from classes and test classes within the same module (these are the classes that the testing runner is looking at).

Therefore, when I have integration tests, I simply use the second module specific to these tests to get around this problem by specifying the output directories needed for each module.

+2
Aug 18 '09 at 13:54
source share



All Articles