简体   繁体   中英

junit test suites

I have a script(test.bat) that allows me to launch one java test by command line : -java -cp() org.junit.runner.JUnitCore package.Class

Now I want to do the same for several java tests ? how could I do it? should I have to add the byte code for each java test? could I have an example , please?

You can use Ant to run your tests with a single command with the junit ant task. Here's an example on how to use it:

<target name="runtests" depends="clean,compiletests">           
    <junit printsummary="yes" haltonfailure="no">
        <classpath>
            <path refid="test.classpath" />                 
            <pathelement location="${test.classes}"/>
        </classpath>                                
        <formatter type="xml"/>     
        <batchtest fork="yes" todir="${test.reports}">
            <fileset dir="${test.src}">
                <include name="**/*Test*.java"/>
            </fileset>
        </batchtest>
    </junit>        
</target>

That target uses batchtest which is part of the junit ant task. It sets your test classpath so all your tests that contain the Test .java pattern in their class name will be included. Check out the JUnit Task documentation .

In JUnit, you can group your tests into a test suite and then run that with a single command.

Here is a tutorial on using test suites in JUnit 3 , and here is an SO post about same with JUnit 4 . Moreover, here is a tutorial on how to use the new features of JUnit 4 .

However, if you are practically trying to write a build script in your batch file, I recommend using an existing build system instead, be it Ant , Maven , Buildr or something else.

JUnit中使用的约定是拥有一个AllTests测试套件,该套件将项目中的所有测试组合在一起,并具有一个Ant脚本或执行AllTests测试套件的任何程序。

I see 3 possibilities:

  • Use ant (see other answers while I'm typping)
  • update your batch to java -cp ... file1 file2 filen
  • use something like this:

     org.junit.runner.JUnitCore.runClasses(TestClass1.class, ...); public class GlobalTest { } 

You can create a set of suites using the JUnit annotation syntax. I describe it in more detail here .

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