Updated on 2024-05-30 GMT+08:00

Configuring a Unit Test

Configuration Description

To process the unit test results, configure the unit test function provided by the build with Maven build. Compile the unit test code in the project and ensure that the following conditions are met:

  • The storage location of unit test code must comply with the default unit test case directory specifications and naming specifications of Maven. You can specify the case location in the configuration.

    For example, if the unit test cases are stored in src/test/java/{{package}}/, the unit test is automatically executed during a Maven build task.

  • The project cannot contain the configuration code of ignoring unit test cases.
    Click the name of the code repository. The Files page of CodeArts Repo is displayed. Verify that the following code does not exist in the pom.xml file of the project:
    1
    2
    3
    4
    5
    6
    7
    8
    <plugin>  
        <groupId>org.apache.maven.plugins</groupId>  
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18.1</version>  
        <configuration>  
            <skipTests>true</skipTests>  
        </configuration>  
    </plugin>
    
  • Click the code repository name. On the Files page of CodeArts Repo, import the JUnit dependency to the pom.xml file. The following shows the code example.
    1
    2
    3
    4
    5
    <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.7</version>
     </dependency>
    

Procedure

  1. Create a code repository and upload the code to the code repository.
  2. Create a unit test class in the src directory, as shown in the following figure.

    The code of the demo project is as follows:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    package test;
     
    public class Demo {
        public String test(Integer i) {
            switch (i) {
                case 1:
                    return "1";
                case 2:
                    return "2";
                default:
                    return "0";
            }
    }
    }
    

    The unit test code is shown in the following section. @Test indicates the test method annotation.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    package test;
     
    import org.junit.Test;
     
    public class DemoTest {
        private Demo demo=new Demo();
        @Test
        public void  test(){
            assert demo.test(1).equals("1");
            assert demo.test(2).equals("2");
            assert demo.test(3).equals("0");
        }
    }
    

  3. In the command window displayed in action build with Maven, use # to comment out the mvn package -Dmaven.test.skip=true -U -e -X -B command.

  4. Delete # before the #mvn deploy -Dmaven.test.skip=true -U -e -X -B command.

  5. Expand Unit Test.

    • Select Yes for Print Test Results.
    • Configure Ignore Test Failure as required.
      • If Yes is selected, the build task is successful when the test case fails.
      • If No is selected, the build task fails when the test case fails.
    • Configure the path of the unit test result file.

      The test report needs to collect the unit test result to generate a visual report. Therefore, specify the path of the unit test result file.

      • In most cases, retain the default path **/TEST*.xml.
      • To improve the accuracy of the result, you can specify a precise report path, for example, target/surefire-reports/TEST*.xml.
    • Configure Print Unit Test Results as required. For details about the configuration method, see Generating a Unit Test Report Using JaCoCo.
    • Configure the report location.

      A relative path in the project whose files will all be uploaded. Example: target/site/jacoco

  6. Run the build task.

    After the task is successfully executed, you can view the test report on the testing tab page of the task execution details page. If you set Print Unit Test Results to Yes, a test report is generated. You can click the button to download the test coverage report.

Generating a Unit Test Report Using JaCoCo

  • Configuration method for a single-module project

    The jacoco-maven-plugin add-on has been added to the project to generate the unit coverage report. That is, the following configuration has been added to the pom.xml file:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    <plugin>
         <groupId>org.jacoco</groupId>
         <artifactId>jacoco-maven-plugin</artifactId>
         <version>0.8.5</version>
         <executions>
             <execution>
                 <goals>
                     <goal>prepare-agent</goal>
                 </goals>
             </execution>
             <execution>
                 <id>report</id>
                 <phase>test</phase>
                 <goals>
                     <goal>report</goal>
                 </goals>
             </execution>
         </executions>
     </plugin>
    

    By default, the JaCoCo report target is in the verify phase. You need to define the report target as the test phase. When mvn test is executed, the unit test report is generated in the target/site/jacoco directory of the code.

  • Configuration method for a multi-module project

    Assume that the code structure of a multi-module project is as follows to describe how to configure and generate a unit test report.

    1
    2
    3
    4
    5
    6
    7
    ├── module1
    │   └── pom.xml
    ├── module2
    │   └── pom.xml
    ├── module3
    │   └── pom.xml
    ├── pom.xml
    
    1. Add a module for aggregation under the project. The name is report. The code structure after the aggregation module is added is as follows:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      ├── module1
      │   └── pom.xml
      ├── module2
      │   └── pom.xml
      ├── module3
      │   └── pom.xml
      ├── report
      │   └── pom.xml
      ├── pom.xml
      
    2. Add the jacoco-maven-plugin add-on to the pom.xml file in the root directory of the project.
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      <!-- Configure unit test coverage-->
       <plugin>
           <groupId>org.jacoco</groupId>
           <artifactId>jacoco-maven-plugin</artifactId>
           <version>0.8.3</version>
           <executions>
               <execution>
                   <goals>
                       <goal>prepare-agent</goal>
                   </goals>
               </execution>
           </executions>
       </plugin>
      
    3. Configure the pom.xml file of the aggregation module.

      Introduce all dependent modules in dependency mode and use report-aggregate to define the JaCoCo aggregation target.

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      <dependencies>
           <dependency>
               <groupId>${project.groupId}</groupId>
               <artifactId>module1</artifactId>
               <version>${project.version}</version>
           </dependency>
           <dependency>
               <groupId>${project.groupId}</groupId>
               <artifactId>module2</artifactId>
               <version>${project.version}</version>
           </dependency>
           <dependency>
               <groupId>${project.groupId}</groupId>
               <artifactId>module3</artifactId>
               <version>${project.version}</version>
           </dependency>
       </dependencies>
      
       <build>
           <plugins>
               <plugin>
                   <groupId>org.jacoco</groupId>
                   <artifactId>jacoco-maven-plugin</artifactId>
                   <version>0.8.3</version>
                   <executions>
                       <execution>
                           <id>report-aggregate</id>
                           <phase>test</phase>
                           <goals>
                               <goal>report-aggregate</goal>
                           </goals>
                       </execution>
                   </executions>
               </plugin>
           </plugins>
       </build>
      

      After the configuration, run mvn test in the root directory of the project. After the command is successfully executed, the coverage report of each module is generated in the report/target/site/jacoco-aggregate directory. You can also customize the output path of the report in outputDirectory.

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      <plugin>
           <groupId>org.jacoco</groupId>
           <artifactId>jacoco-maven-plugin</artifactId>
           <version>0.8.3</version>
           <executions>
               <execution>
                   <id>report-aggregate</id>
                   <phase>test</phase>
                   <goals>
                       <goal>report-aggregate</goal>
                   </goals>
                   <configuration>
                       <outputDirectory>target/site/jacoco</outputDirectory>
                   </configuration>
               </execution>
           </executions>
       </plugin>