Using Ant in Intellij IDEA for automatic test and generating HTML format test report

Article directory

1, Environmental preparation

Please refer to the article first Unit test with JUnit4 in Intellij IDEA Configure JUnit test environment.

1. install Ant

Directly in Official website Download the latest Ant. The version used in this experiment is apache-ant-1.10.7

2. configure Ant

   add the Ant Path to the Path of the system environment variable. For example, my Path is F:\apache-ant-1.10.7\bin

3. Test whether Ant is configured successfully


2, Write test code

1. Write test class in src directory
public class Item{

  private String itemId;
  private String itemPrice;
  private int quantity;

  public Item(String itemId, String itemPrice, int quantity){
    this.itemId = itemId.trim();
    this.itemPrice = itemPrice;
    this.quantity = quantity;

  // ID acquisition of goods
  public String getItemId() {
    System.out.println("itemId : " + itemId);
    return itemId;

  // Quantity acquisition of goods
  public int getQuantity() {
    System.out.println("quantity : " + quantity);
    return quantity;

  // Price acquisition of goods
  public String getItemPrice() {
    System.out.println("itemPrice : " + itemPrice);
    return itemPrice;

2. Write a test class in the test directory (if not, create a new test and set it to Test Resources Root)

import org.junit.Assert;
import org.junit.Test;

public class TestJunit {
    private String itemId = "A02";
    private String itemPrice = "$8.88";
    private int quantity = 8;

    private Item item = new Item(itemId, itemPrice, quantity);

    public void testGetItemId() {
        Assert.assertEquals(itemId, item.getItemId());

    public void testGetQuantity() {
        Assert.assertEquals(quantity, item.getQuantity());

    public void testGetItemPrice() {
        Assert.assertEquals(itemPrice, item.getItemPrice());

3, Write Ant profile

   the general configuration file is named build.xml. Create the configuration file build.xml in the root directory of the project, then select [Ant] on the right side of the interface, click and select [+] to add the file to Ant.

<?xml version="1.0"?>

    <project name="ant and junit" default="test auot junit and report" basedir=".">

        <!-- Define project dependent jar Location of package storage -->
        <property name="lib.dir" value="lib"/>
        <path id="classpath">
            <fileset dir="${lib.dir}" includes="**/*.jar"/>

        <property name="output folder" value="classes"/>
        <property name="src folder" value="src"/>
        <property name="test folder" value="test"/>
        <property name="report folder" value="report"/>

        <target name="clean">
            <delete dir="report"/>
            <echo>Clear test report file succeeded!</echo>

        <target name="compile init">
            <mkdir dir="${output folder}"/>
            <echo>Create compilation folder successfully!</echo>

        <target name="report init" depends="clean">
            <mkdir dir="${report folder}"/>
            <echo>Test report folder created successfully!</echo>

        <target name="compile" depends="compile init">
            <javac srcdir="${src folder}" destdir="${output folder}" classpathref="classpath"/>
            <echo>Project source file compiled successfully!</echo>

        <target name="test compile" depends="report init">
            <javac srcdir="${test folder}" destdir="${output folder}" classpathref="classpath"/>
            <echo>Project test file compiled successfully!</echo>

        <target name="all compile" depends="compile, test compile">

        <target name="test auot junit and report" depends="all compile">
            <junit printsummary="on" fork="true" showoutput="true">
                    <fileset dir="${lib.dir}" includes="**/*.jar"/>
                    <pathelement path="${output folder}"/>
                <formatter type="xml"/>
                <batchtest todir="${report folder}">
                    <fileset dir="${output folder}">
                        <include name="**/Test*.*"/>
            <junitreport todir="${report folder}">
                <fileset dir="${report folder}">
                    <include name="TEST-*.xml"/>
                <report format="frames" todir="${report folder}"/>


4, Operation results

1. Project directory structure


2. Select the Ant class and click Run. If the test is correct, the normal compilation result will be displayed on the terminal.

3. Enter the path / report/index.html of the project in the browser to view the report results of Ant automation test.

Reference article: [1] Automation of unit testing with ant and junit

     [2]Resolve Junit in IDEA is unavailable (prompt package not found)

Published 193 original articles, won praise 108, visited 80000+
Private letter follow

Tags: Junit xml Apache IntelliJ IDEA

Posted on Sat, 11 Jan 2020 22:35:03 -0800 by audiomove