通过Ant运行Junit似乎不使用自定义类运行器

Ben*_*est 7 java ant junit unit-testing

我有一个自定义运行器,它通过套接字连接将Junit测试发送到在其他硬件上运行的Junit服务器.测试按预期运行,具有以下目标:

    <target name="run">
        <mkdir dir="reports" />
        <junit fork="yes" haltonfailure="no">
            <test name="${CurrentTest}" />
            <formatter type="xml" />
            <classpath refid="mastersuite.classpath" />
        </junit>

        <junitreport todir="${JunitReport.dir}">
            <fileset dir=".">
                <include name="TEST-*.xml" />           
            </fileset>
            <report todir="${JunitReport.dir}" />
        </junitreport>
    </target>
Run Code Online (Sandbox Code Playgroud)

但是,当我添加以下<batchtest>元素时......

<target name="run">
    <delete dir="reports" failonerror="false" />        
    <!-- Make the reports directory -->
    <mkdir dir="reports" />

    <!-- Execute the tests and saves the results to XML -->
    <junit fork="yes" printsummary="no" haltonfailure="no">
        <batchtest fork="yes" todir="${JunitReport.dir}">
            <fileset dir="${APITesting.classes}">
                <include name="test/api/**/*Test.class" />
            </fileset>
        </batchtest>
        <formatter type="xml" />
        <classpath refid="mastersuite.classpath" />
    </junit>

    <!-- Compile the resulting XML file into an HTML based report. -->
    <junitreport todir="${JunitReport.dir}">
        <fileset dir="${JunitReport.dir}">
            <include name="TEST-*.xml" />
        </fileset>
        <report todir="${JunitReport.dir}" />
    </junitreport>
</target>
Run Code Online (Sandbox Code Playgroud)

没有任何东西被运送到硬件,这使我相信我的@RunWith(com.company.name.RemoteTestCaseRunner.class)注释在<batchtest>的上下文中没有得到尊重.有没有我忘记做的事情,或者为了调用我的@RunWith注释,可能必须另外做一些事情?

测试仍在运行并且报告已创建,并且某些非平台相关的测试会运行并通过,而不是那些需要与目标硬件上的服务进行通信的测试.

更新我已经确定,当使用与@SuiteClasses({})配对的@ RunWith(Suite.class)时,这样可以正常工作,但如果我明确地给它一个测试用例,那就不行了.所以现在我真的不确定问题出在哪里.

更新虽然我没有找到任何可靠的东西,但我的测试行为似乎意味着以下内容:基于我的测试格式化方式(它们扩展TestCase)我认为Ant正在执行我的测试用例Junit3测试.如上所述,当我运行为Junit4格式化的测试套件(仅使用注释)时,我的测试运行并按预期执行.似乎当我直接传递Junit3格式的测试用例时,我的注释没有得到尊重,这意味着正在使用Junit3运行器.

我的新问题是:有没有办法明确告诉蚂蚁使用Junit 4跑步者?

Ben*_*est 8

根据您的测试用例是否使用Junit4TestAdapter,Ant似乎会自动解析使用哪个运行器.我最终不得不在我的所有测试用例中添加以下方法:

public class MyTestCase extends TestCase
    public static junit.framework.Test suite() {
        return new JUnit4TestAdapter(MyTestCase.class);
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

大多数人可能不需要扩展TestCase,但在我的情况下这是必需的,因为Junit4充当Junit3服务器的客户端.

  • 我必须给你+100的答案.经过2天的搜索,这似乎解决了我的问题!我的原帖http://stackoverflow.com/q/26848673/668650. (2认同)