Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Junit through Ant does not seem to use custom class runner

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>

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>

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

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

like image 527
Bender the Greatest Avatar asked Oct 17 '11 15:10

Bender the Greatest


1 Answers

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);
    }
    ...
}

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.

like image 102
Bender the Greatest Avatar answered Oct 24 '22 13:10

Bender the Greatest