栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

答案是创建一个仅包含单元测试文件夹下的那些测试的测试套件,然后运行它。有一个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", "");    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/446792.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号