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

运行JUnit @Test方法的子集

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

运行JUnit @Test方法的子集

guerda的解决方案很好。这就是我最终要做的事情(这是我以前联系过的卢克·弗朗克食谱的混合物,还有我在网上看到的其他东西):

import org.junit.runner.manipulation.Filter;import org.junit.runner.Description;public final class AntCLFilter extends Filter {    private static final String TEST_CASES = "tests";    private static final String ANT_PROPERTY = "${tests}";    private static final String DELIMITER = "\,";    private String[] testCaseNames;    public AntCLFilter() {        super();        if (hasTestCases()) testCaseNames = getTestCaseNames();    }    public String describe() {        return "Filters out all tests not explicitly named in a comma-delimited list in the system property 'tests'.";    }    public boolean shouldRun(Description d) {        String displayName = d.getDisplayName();        // cut off the method name:        String testName = displayName.substring(0, displayName.indexOf('('));        if (testCaseNames == null) return true;        for (int i = 0; i < testCaseNames.length; i++) if (testName.equals(testCaseNames[i]))     return true;        return false;    }        public static boolean hasTestCases() {        return System.getProperty( TEST_CASES ) == null || System.getProperty( TEST_CASES ).equals( ANT_PROPERTY ) ? false : true;    }        private static String[] getTestCaseNames() {        if ( System.getProperty( TEST_CASES ) == null ) { throw new NullPointerException( "Test case property is not set" );        }        String testCases = System.getProperty( TEST_CASES );        String[] cases = testCases.split(DELIMITER);        return cases;    }}import org.junit.internal.runners.*;import org.junit.runner.manipulation.Filter;import org.junit.runner.manipulation.NoTestsRemainException;public class FilteredRunner extends TestClassRunner {    public FilteredRunner(Class<?> clazz) throws InitializationError {        super(clazz);        Filter f = new AntCLFilter();        try { f.apply(this);        } catch (NoTestsRemainException ex) { throw new RuntimeException(ex);        }    }}

然后,我用以下注释了测试类:

@RunWith(FilteredRunner.class)public class MyTest {

并将以下内容放入我的ant buildfile中:

<target name="runtest"        description="Runs the test you specify on the command line with -Dtest="        depends="compile, ensure-test-name">    <junit printsummary="withOutAndErr" fork="yes">        <sysproperty key="tests" value="${tests}" />        <classpath refid="classpath" />        <formatter type="plain" usefile="false" />        <batchtest> <fileset dir="${src}">     <include name="**/${test}.java" /> </fileset>        </batchtest>    </junit></target>

关键行是sysproperty标签。

现在我可以跑步

ant runtest -Dtest=MyTest -Dtests=testFoo,testBar

如预期的。这适用于JUnit 4.1
—在4.4中是JUnit4ClassRunner的子类,在4.5及更高版本中是BlockJUnit4ClassRunner的子类。



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

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

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