通过 Ant 运行 Junit 似乎没有使用自定义类运行器 [英] Running Junit through Ant does not seem to use custom class runner

查看:19
本文介绍了通过 Ant 运行 Junit 似乎没有使用自定义类运行器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a custom runner that ships Junit tests over a socket connection to a Junit server running on other hardware. The tests run as intended with the following target:

    <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>

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

However, when I add in the following <batchtest> element...

<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>

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

nothing gets shipped over to the hardware, which leads me to believe that my @RunWith(com.company.name.RemoteTestCaseRunner.class) annotation is not being honored within the context of <batchtest>. Is there something I am forgetting to do, or perhaps that must be done additionally for my @RunWith annotation to be called?

测试仍在运行并创建报告,并且某些不依赖于平台的测试确实运行并通过了,只是不需要与目标硬件上的服务通信的测试.

The tests are still being run and the reports are created, and certain tests that are not platform dependent do run and pass, just not the ones that require communication with services on the target hardware.

UPDATE 我已经确定这在使用@RunWith(Suite.class) 和@SuiteClasses({}) 时工作​​正常,只是如果我明确给它一个测试用例就不行.所以现在我真的不知道问题出在哪里.

UPDATE I have determined that this works fine when using @RunWith(Suite.class) paired with @SuiteClasses({}), just not if I explicitly give it a test case. So now I'm really not sure where the problem lies.

UPDATE 虽然我没有发现任何可靠的信息,但我的测试行为似乎暗示了以下内容:基于我的测试的格式化方式(它们扩展了 TestCase),我认为 Ant 是将我的测试用例作为 Junit3 测试执行.如上所述,当我运行针对 Junit4 格式化的测试套件(仅使用注释)时,我的测试会按预期运行并执行.好像我直接通过Junit3格式的测试用例的时候是我的注解不兑现,这意味着正在使用Junit3运行器.

UPDATE While I haven't found anything solid on this, the behavior of my tests seem to imply the following: based on the way my tests are formatted (they extend TestCase) I think Ant is executing my test cases as Junit3 tests. As stated above, when I run a test suite formatted for Junit4 (using only annotations) my tests run and are executed as intended. It seems that when I pass the Junit3 formatted test case directly is when my annotations are not honored, which implies that the Junit3 runner is being used.

我的新问题是:有没有办法明确告诉 ant 使用 Junit 4 runner?

My new question is this: is there a way to explicitly tell ant to use the Junit 4 runner?

推荐答案

似乎 Ant 会根据您的测试用例是否使用 Junit4TestAdapter 自动解析要使用的运行程序.我最终不得不将以下方法添加到我的所有测试用例中:

It seems that Ant automatically resolves which runner to use based on if your test case uses the Junit4TestAdapter. I ended up having to add the following method to all of my test cases:

public class MyTestCase extends TestCase
    public static junit.framework.Test suite() {
        return new JUnit4TestAdapter(MyTestCase.class);
    }
    ...
}

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

Most of you will probably not need to extend TestCase, but in my case this is required because Junit4 is acting as a client to a Junit3 server.

这篇关于通过 Ant 运行 Junit 似乎没有使用自定义类运行器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆