
答案是创建一个仅包含单元测试文件夹下的那些测试的测试套件,然后运行它。有一个junit-
addon就是这个名字,叫做DirectorySuiteBuilder,但是我只是在重新发明轮子之后才发现的。
在这里已经有人问过!
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", ""); }}欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)