Ant 简明教程

Ant - JUnit Integration

JUnit 是基于 Java 开发的常用单元测试框架。它易于使用和扩展。有许多可用 JUnit 扩展。如果您不熟悉 JUnit,则应从 www.junit.org 下载它并阅读其手册。

JUnit is the commonly used unit testing framework for Java-based developments. It is easy to use and easy to extend. There are a number of JUnit extensions available. If you are unfamiliar with JUnit, you should download it from www.junit.org and read its manual.

本章介绍如何使用 Ant 执行 JUnit 测试。Ant 的使用通过 JUnit 任务变得简单。

This chapter shows how to execute JUnit tests by using Ant. The use of Ant makes it straight forward through the JUnit task.

JUnit 任务的属性如下所示 −

The attributes of the JUnit task are presented below −

Sr.No

Properties & Description

1

dir Where to invoke the VM from. This is ignored when fork is disabled.

2

jvm Command used to invoke the JVM. This is ignored when fork is disabled.

3

fork Runs the test in a separate JVM.

4

errorproperty The name of the property to set if there is a JUnit error.

5

failureproperty The name of the property to set if there is a JUnit failure.

6

haltonerror Stops execution when a test error occurs.

7

haltonfailure Stops execution when a failure occurs.

8

printsummary Advises Ant to display simple statistics for each test.

9

showoutput Advises Ant to send the output to its logs and formatters.

10

tempdir Path to the temporary file that Ant will use.

11

timeout Exits the tests that take longer to run than this setting (in milliseconds).

让我们继续 Hello World 传真 Web 应用程序的主题,并添加 JUnit 目标。

Let us continue the theme of the Hello World Fax web application and add a JUnit target.

以下示例显示了一个简单的 JUnit 测试执行 −

The following example shows a simple JUnit test execution −

<target name="unittest">
   <junit haltonfailure="true" printsummary="true">
      <test name="com.tutorialspoint.UtilsTest"/>
   </junit>
</target>

此示例显示在 com.tutorialspoint.UtilsTest junit 类上执行 JUnit。

This example shows the execution of JUnit on the com.tutorialspoint.UtilsTest junit class.

运行以上代码将产生以下输出 −

Running the above code produces the following output −

test:
[echo] Testing the application
[junit] Running com.tutorialspoint.UtilsTest
[junit] Tests run: 12, Failures: 0, Errors: 0, Time elapsed: 16.2 sec
BUILD PASSED