简体   繁体   中英

JUnit execute all test classes of all packages

Is there any way to build all test cases of JUnit at once?Like on "Ruby on rails" by the command "rake test" all test classes got executed. For Java I saw some solutions of executing all tests within a package. but I want to execute all test cases for all packages. Is it possible? how should I do on the build.xml file?

I can't add a comment yet that's why I am posting this an answer.

I think what you need is a test suite class.

That goes something like follows.

package com.emeter.test.predeploy.sdm.common;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

import com.emeter.test.predeploy.sdm.svc.TestOutdatedComponentRpt;
import com.emeter.test.predeploy.sdm.svc.TestSubstationSvc;
import com.emeter.test.predeploy.sdm.svc.TestSvmComponentSvc;
import com.emeter.test.predeploy.sdm.svc.TestSvmNotificationSvc;

@RunWith(Suite.class)
@SuiteClasses({

TestSubstationSvc.class,
TestSvmComponentSvc.class,
TestSvmNotificationSvc.class,
TestOutdatedComponentRpt.class
 }
)
public class TestSuite {

}

You can import the required class from any package and then run them all at once. The classes containing the test cases are put under 'SuiteClasses' annotation.

Edit: You just run this like any other test case file in eclipse.

You can run all tests or specific tests filtered by package name or class name. Here is an example of <batchtest> taken from the JUnit Task manual :

<junit printsummary="yes" haltonfailure="yes">
    <classpath>
        <pathelement location="${build.tests}"/>
        <pathelement path="${java.class.path}"/>
    </classpath>

    <formatter type="plain"/>

    <test name="my.test.TestCase" haltonfailure="no" outfile="result">
        <formatter type="xml"/>
    </test>

    <batchtest fork="yes" todir="${reports.tests}">
        <fileset dir="${src.tests}">
            <include name="**/*Test*.java"/>
            <exclude name="**/AllTests.java"/>
        </fileset>
    </batchtest>
</junit>

You can adapt the <include name=""/> / <exclude name=""/> elements to your needs or add more include/exclude elements. You could then create different ant <target/> s for different tests like <target name="all-tests"/> , <target name="package-foo-tests"/> etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM