JUnit task参数说明

本文介绍了如何使用JUnit框架来运行和测试Java代码,包括任务的基本描述、参数、属性、元素及其用途。详细说明了如何配置JUnit任务,如使用不同格式的报告、设置测试类和方法、控制测试运行环境等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://ant.apache.org/manual/Tasks/junit.html

JUnit

Description

This task runs tests from the JUnit testing framework. The latestversion of the framework can be found athttp://www.junit.org.This task has been tested with JUnit 3.0 up to JUnit 3.8.2; it won'twork with versions prior to JUnit 3.0. It also works with JUnit 4.0, including"pure" JUnit 4 tests using only annotations and no JUnit4TestAdapter.

Note: This task depends on external libraries not includedin the Apache Ant distribution. See Library Dependencies for more information.

Note:You must have junit.jar available.You can do one of:

  1. Put both junit.jar and ant-junit.jar inANT_HOME/lib.
  2. Do not put either in ANT_HOME/lib, and insteadinclude their locations in your CLASSPATH environment variable.
  3. Add both JARs to your classpath using -lib.
  4. Specify the locations of both JARs usinga <classpath> element in a <taskdef> in the build file.
  5. Leave ant-junit.jar in its default location in ANT_HOME/libbut include junit.jar in the <classpath> passedto <junit>. (since Ant 1.7)

See theFAQ for details.

Tests are defined by nested test orbatchtest tags (see nestedelements).

Parameters

AttributeDescriptionRequired
printsummaryPrint one-line statistics for each testcase. Can take the values on, off, and withOutAndErr. withOutAndErr is the same as on but also includes the output of the test as written to System.out and System.err.No; default is off.
forkRun the tests in a separate VM.No; default is off.
forkmodeControls how many Java Virtual Machines get created if you want to fork some tests. Possible values are "perTest" (the default), "perBatch" and "once". "once" creates only a single Java VM for all tests while "perTest" creates a new VM for each TestCase class. "perBatch" creates a VM for each nested <batchtest> and one collecting all nested <test>s. Note that only tests with the same settings of filtertrace, haltonerror, haltonfailure, errorproperty and failureproperty can share a VM, so even if you set forkmode to "once", Ant may have to create more than a single Java VM. This attribute is ignored for tests that don't get forked into a new Java VM. since Ant 1.6.2No; default is perTest.
haltonerrorStop the build process if an error occurs during the test run.No; default is off.
errorpropertyThe name of a property to set in the event of an error.No
haltonfailureStop the build process if a test fails (errors are considered failures as well).No; default is off.
failurepropertyThe name of a property to set in the event of a failure (errors are considered failures as well).No.
filtertraceFilter out Junit and Ant stack frames from error and failure stack traces.No; default is on.
timeoutCancel the individual tests if they don't finish in the given time (measured in milliseconds). Ignored if fork is disabled. When running multiple tests inside the same Java VM (see forkMode), timeout applies to the time that all tests use together, not to an individual test.No
maxmemoryMaximum amount of memory to allocate to the forked VM. Ignored if fork is disabled. Note: If you get java.lang.OutOfMemoryError: Java heap space in some of your tests then you need to raise the size like maxmemory="128m"No
jvmThe command used to invoke the Java Virtual Machine, default is 'java'. The command is resolved by java.lang.Runtime.exec(). Ignored if fork is disabled.No; default is java.
dirThe directory in which to invoke the VM. Ignored if fork is disabled.No
newenvironmentDo not propagate the old environment when new environment variables are specified. Ignored if fork is disabled.No; default is false.
includeantruntimeImplicitly add the Ant classes required to run the tests and JUnit to the classpath in forked mode.No; default is true.
showoutputSend any output generated by tests to Ant's logging system as well as to the formatters. By default only the formatters receive the output.No
outputtoformattersSince Ant 1.7.0.
Send any output generated by tests to the test formatters. This is "true" by default.
No
tempdirWhere Ant should place temporary files. Since Ant 1.6.No; default is the project's base directory.
reloadingWhether or not a new classloader should be instantiated for each test case.
Ignore if fork is set to true. Since Ant 1.6.
No; default is true.
clonevmIf set to true true, then all system properties and the bootclasspath of the forked Java Virtual Machine will be the same as those of the Java VM running Ant. Default is "false" (ignored if fork is disabled). since Ant 1.7No
logfailedtestsWhen Ant executes multiple tests and doesn't stop on errors or failures it will log a "FAILED" message for each failing test to its logging system. If you set this option to false, the message will not be logged and you have to rely on the formatter output to find the failing tests. since Ant 1.8.0No
enableTestListenerEventsWhether Ant should send fine grained information about the running tests to Ant's logging system at the verbose level. Such events may be used by custom test listeners to show the progress of tests.
Defaults to false.
Can be overridden by a magic property.
since Ant 1.8.2 - Ant 1.7.0 to 1.8.1 behave as if this attribute was true by default.
No

By using the errorproperty and failurepropertyattributes, it is possible toperform setup work (such as starting an external server), execute the test,clean up, and still fail the build in the event of a failure.

The filtertrace attribute condenses error and failurestack traces before reporting them.It works with both the plain and XML formatters. It filters out any linesthat begin with the following string patterns:

   "junit.framework.TestCase"
   "junit.framework.TestResult"
   "junit.framework.TestSuite"
   "junit.framework.Assert."
   "junit.swingui.TestRunner"
   "junit.awtui.TestRunner"
   "junit.textui.TestRunner"
   "java.lang.reflect.Method.invoke("
   "sun.reflect."
   "org.apache.tools.ant."
   "org.junit."
   "junit.framework.JUnit4TestAdapter"

Nested Elements

The <junit> tasksupports a nested <classpath>element that represents a PATH likestructure.

As of Ant 1.7, this classpath may be used to refer to junit.jaras well as your tests and the tested code.

jvmarg

If fork is enabled, additional parameters may be passed tothe new VM via nested <jvmarg> elements. For example:

<junit fork="yes">
  <jvmarg value="-Djava.compiler=NONE"/>
  ...
</junit>

would run the test in a VM without JIT.

<jvmarg> allows all attributes described in Command-line Arguments.

sysproperty

Use nested <sysproperty> elements to specify systemproperties required by the class. These properties will be made availableto the VM during the execution of the test (either ANT's VM or the forked VM,if fork is enabled).The attributes for this element are the same as for environment variables.

<junit fork="no">
  <sysproperty key="basedir" value="${basedir}"/>
  ...
</junit>

would run the test in ANT's VM and make the basedir propertyavailable to the test.

syspropertyset

You can specify a set of properties to be used as system propertieswith syspropertysets.

since Ant 1.6.

env

It is possible to specify environment variables to pass to theforked VM via nested <env> elements. For a descriptionof the <env> element's attributes, see thedescription in the exec task.

Settings will be ignored if fork is disabled.

bootclasspath

The location of bootstrap class files can be specified using thisPATH like structure - will be ignoredif fork is not true or the target VM doesn'tsupport it (i.e. Java 1.1).

since Ant 1.6.

permissions

Security permissions can be revoked and granted during the execution of the class via a nested permissions element. For more information pleasesee permissions

Settings will be ignored if fork is enabled.

since Ant 1.6.

assertions

You can control enablement of Java 1.4 assertions with an<assertions>subelement.

Assertion statements are currently ignored in non-forked mode.

since Ant 1.6.

formatter

The results of the tests can be printed in differentformats. Output will always be sent to a file, unless you set theusefile attribute to false.The name of the file is determined by thename of the test and can be set by the outfile attributeof <test>.

There are four predefined formatters - one prints the test resultsin XML format, the other emits plain text. The formatter namedbrief will only print detailed information for testcasesthat failed, while plain gives a little statistics linefor all test cases. Custom formatters that need to implementorg.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormattercan be specified.

If you use the XML formatter, it may not include the same outputthat your tests have written as some characters are illegal in XMLdocuments and will be dropped.

The fourth formatter named failure (since Ant 1.8.0) collects all failing testXXX()methods and creates a new TestCase which delegates only thesefailing methods. The name and the location can be specified via Java System property or Ant propertyant.junit.failureCollector. The value has to point to the directory and the name of the resulting class (without suffix). It defaults to java-tmp-dir/FailedTests.

AttributeDescriptionRequired
typeUse a predefined formatter (either xml, plain, brief or failure).Exactly one of these.
classnameName of a custom formatter class.
extensionExtension to append to the output filename.Yes, if classname has been used.
usefileBoolean that determines whether output should be sent to a file.No; default is true.
ifOnly use formatter if the named property is set.No; default is true.
unlessOnly use formatter if the named property is not set.No; default is true.
test

Defines a single test class.

AttributeDescriptionRequired
nameName of the test class.Yes
methodsComma-separated list of names of test case methods to execute. Since 1.8.2

The methods attribute can be useful in the following scenarios:

  • A test method has failed and you want to re-run the test method to test a fix or re-run the test under the Java debugger without having to wait for the other (possibly long running) test methods to complete.
  • One or more test methods are running slower than expected and you want to re-run them under a Java profiler (without the overhead of running the profiler whilst other test methods are being executed).

If the methods attribute is used but no test method is specified, then no test method from the suite will be executed.

No; default is to run all test methods in the suite.
forkRun the tests in a separate VM. Overrides value set in <junit>.No
haltonerrorStop the build process if an error occurs during the test run. Overrides value set in <junit>.No
errorpropertyThe name of a property to set in the event of an error. Overrides value set in <junit>.No
haltonfailureStop the build process if a test fails (errors are considered failures as well). Overrides value set in <junit>.No
failurepropertyThe name of a property to set in the event of a failure (errors are considered failures as well). Overrides value set in <junit>.No
filtertraceFilter out Junit and Ant stack frames from error and failure stack traces. Overrides value set in <junit>.No; default is on.
todirDirectory to write the reports to.No; default is the current directory.
outfileBase name of the test result. The full filename is determined by this attribute and the extension of formatter.No; default is TEST-name, where name is the name of the test specified in the name attribute.
ifOnly run test if the named property is set.No
unlessOnly run test if the named property is not set.No

Tests can define their own formatters via nested<formatter> elements.

batchtest

Define a number of tests based on pattern matching.

batchtest collects the included resources from any numberof nested Resource Collections. It thengenerates a test class name for each resource that ends in.java or .class.

Any type of Resource Collection is supported as a nested element,prior to Ant 1.7 only <fileset> has beensupported.

AttributeDescriptionRequired
forkRun the tests in a separate VM. Overrides value set in <junit>.No
haltonerrorStop the build process if an error occurs during the test run. Overrides value set in <junit>.No
errorpropertyThe name of a property to set in the event of an error. Overrides value set in <junit>.No
haltonfailureStop the build process if a test fails (errors are considered failures as well). Overrides value set in <junit>.No
failurepropertyThe name of a property to set in the event of a failure (errors are considered failures as well). Overrides value set in <junit>No
filtertraceFilter out Junit and Ant stack frames from error and failure stack traces. Overrides value set in <junit>.No; default is on.
todirDirectory to write the reports to.No; default is the current directory.
ifOnly run tests if the named property is set.No
unlessOnly run tests if the named property is not set.No

Batchtests can define their own formatters via nested<formatter> elements.

Forked tests and tearDown

If a forked test runs into a timeout, Ant will terminate the Java VM process it has created, which probably means the test's tearDown method will never be called. The same is true if the forked VM crashes for some other reason.

Starting with Ant 1.8.0, a special formatter is distributed with Ant that tries to load the testcase that was in the forked VM and invoke that class' tearDown method. This formatter has the following limitations:

  • It runs in the same Java VM as Ant itself, this is a different Java VM than the one that was executing the test and it may see a different classloader (and thus may be unable to load the tast class).
  • It cannot determine which test was run when the timeout/crash occured if the forked VM was running multiple test. I.e. the formatter cannot work with any forkMode other than perTest and it won't do anything if the test class contains a suite() method.

If the formatter recognizes an incompatible forkMode or a suite method or fails to load the test class it will silently do nothing.

The formatter doesn't have any effect on tests that were not forked or didn't cause timeouts or VM crashes.

To enable the formatter, add a formatter like

<formatter classname="org.apache.tools.ant.taskdefs.optional.junit.TearDownOnVmCrash"
           usefile="false"/>

to your junit task.

ant.junit.enabletestlistenerevents magic property

Since Ant 1.8.2 the enableTestListenerEvents attribute of the task controls whether fine grained logging messages will be sent to the task's verbose log. In addition to this attribute Ant will consult the property ant.junit.enabletestlistenerevents and the value of the property overrides the setting of the attribute.

This property exists so that containers running Ant that depend on the additional logging events can ensure they will be generated even if the build file disables them.

Examples

<junit>
  <test name="my.test.TestCase"/>
</junit>

Runs the test defined in my.test.TestCase in the sameVM. No output will be generated unless the test fails.

<junit printsummary="yes" fork="yes" haltonfailure="yes">
  <formatter type="plain"/>
  <test name="my.test.TestCase"/>
</junit>

Runs the test defined in my.test.TestCase in aseparate VM. At the end of the test, a one-line summary will beprinted. A detailed report of the test can be found inTEST-my.test.TestCase.txt. The build process will bestopped if the test fails.

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

Runs my.test.TestCase in the same VM, ignoring thegiven CLASSPATH; only a warning is printed if this test fails. Inaddition to the plain text test results, for this test a XML resultwill be output to result.xml. Then, for each matching file in the directory defined for${src.tests} atest is run in a separate VM. If a test fails, the build process isaborted. Results are collected in files namedTEST-name.txt and written to${reports.tests}.

<target name="test">
    <property name="collector.dir" value="${build.dir}/failingTests"/>
    <property name="collector.class" value="FailedTests"/>
    <!-- Delete 'old' collector classes -->
    <delete>
        <fileset dir="${collector.dir}" includes="${collector.class}*.class"/>
    </delete>
    <!-- compile the FailedTests class if present --> 
    <javac srcdir="${collector.dir}" destdir="${collector.dir}"/>
    <available file="${collector.dir}/${collector.class}.class" property="hasFailingTests"/>
    <junit haltοnerrοr="false" haltonfailure="false">
        <sysproperty key="ant.junit.failureCollector" value="${collector.dir}/${collector.class}"/>
        <classpath>
            <pathelement location="${collector.dir}"/>
        </classpath>
        <batchtest todir="${collector.dir}" unless="hasFailingTests">
            <fileset dir="${collector.dir}" includes="**/*.java" excludes="**/${collector.class}.*"/>
            <!-- for initial creation of the FailingTests.java -->
            <formatter type="failure"/>
            <!-- I want to see something ... -->
            <formatter type="plain" usefile="false"/>
        </batchtest>
        <test name="FailedTests" if="hasFailingTests">
            <!-- update the FailingTests.java -->
            <formatter type="failure"/>
            <!-- again, I want to see something -->
            <formatter type="plain" usefile="false"/>
        </test>
    </junit>
</target>

On the first run all tests are collected via the <batchtest/>element. It's plain formatter shows the output on the console. Thefailure formatter creates a java source file in ${build.dir}/failingTests/FailedTests.java which extends junit.framework.TestCase and returns from a suite()method a test suite for the failing tests.
On a second run the collector class exists and instead of the <batchtest/>the single <test/> will run. So only the failing test cases are re-run.The two nested formatters are for displaying (for the user) and for updating the collectorclass.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值