0

My project structure is as follows:

myapp
+- module1
|  +- src/main
|  \- src/unit-tests
+- module2
|  +- src/main
|  \- src/unit-tests
+- module-FT
|  +- src/tests
|  \- pom.xml
\- pom.xml

In Jenkins job, the root pom is pom.xml (because I want the whole project to be built for JaCoCo reports on functional coverage), and when running the Maven goal for coverage report generation I have specify the pom as module-FT/pom.xml.

Now when the test results are shown, it shows me a total being = FTs + all unit tests in other modules while I want to exclude the UTs from report generation.

But I dont think it has anything to do with JaCoCo, since I am talking about not the JaCoCo report but the Test Results. The unit tests are also getting counted which I dont want. I think this is so because in the Maven root pom below, I have given pom.xml which is causing the whole project to be built. But that's necessary for the JaCoCo to put out the coverage. I am not using JaCoCo plugin since the jenkins version is old and its beyond my scope to upgrade that.

enter image description here

Below are the Jenkins job details:

Build:

  1. maven version: maven 3.3.3
  2. root pom: pom.xml
  3. Goals and options: -B -U -X clean test -f functional-tests/pom.xml -DsuiteXmlFile=src/test/resources/suites/${TEST_SUITE} -Dhostname=${TEST_HOST} -Dprotocol=https -Dport=${TEST_PORT}

Post Steps:

  1. Execute shell

      #!/bin/bash
      # Cleanup workspace
      rm -rf target
      echo --------------------------------------
      echo TEST_HOST is ${TEST_HOST}
      echo manifestVersion is ${manifestVersion}
      echo --------------------------------------
      temp=${manifestVersion}
      appname=(${temp//-/ })
      manifestid=(${temp// / })
      echo appname is ${appname[0]}
      echo manifestid is ${manifestid[0]}
    
    
      mkdir -p ${WORKSPACE}/target/classes
    
      ### Copy class files to common folder for analysis
      cp -R ${WORKSPACE}/module-common/target/classes/* ${WORKSPACE}/target/classes
    cp -R ${WORKSPACE}/module-data/target/classes/* ${WORKSPACE}/target/classes
    cp -R ${WORKSPACE}/module-event/target/classes/* ${WORKSPACE}/target/classes
    cp -R ${WORKSPACE}/module-dependencies/target/classes/* ${WORKSPACE}/target/classes
    cp -R ${WORKSPACE}/module-core/target/classes/* ${WORKSPACE}/target/classes
    cp -R ${WORKSPACE}/module-api/target/classes/* ${WORKSPACE}/target/classes
    cp -R ${WORKSPACE}/module-legacy-api/target/classes/* ${WORKSPACE}/target/classes
    cp -R ${WORKSPACE}/module-messaging/target/classes/* ${WORKSPACE}/target/classes
    cp -R ${WORKSPACE}/module-service/target/classes/* ${WORKSPACE}/target/classes
    
    # build jacoco pom file in order to dump coverage file from app server
    echo '''
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.myorg</groupId>
      <artifactId>CodeCoverage</artifactId>
      <version>0.0.1</version>
      <build>
        <plugins>
          <!-- Dependencies -->
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
              <!-- Copy the ant tasks jar. Needed for ts.jacoco.report-ant . -->
              <execution>
                <id>jacoco-dependency-ant</id>
                <goals>
                  <goal>copy</goal>
                </goals>
                <phase>process-test-resources</phase>
                <inherited>false</inherited>
                <configuration>
                  <artifactItems>
                    <artifactItem>
                      <groupId>org.jacoco</groupId>
                      <artifactId>org.jacoco.ant</artifactId>
                      <version>0.7.4.201502262128</version>
                    </artifactItem>
                  </artifactItems>
                  <stripVersion>true</stripVersion>
                  <outputDirectory>${basedir}/target/jacoco-jars</outputDirectory>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.4.201502262128</version>
            <executions>
              <execution>
                <id>default-cli</id>
                <phase>post-integration-test</phase>
                <goals>
                  <goal>dump</goal>
                </goals>
                <configuration>
                  <reset>${Reset}</reset>
                  <address>${TEST_HOST}</address>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <!-- Ant plugin. -->
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
              <execution>
                <id>report</id>
                <phase>post-integration-test</phase>
                <goals>
                  <goal>run</goal>
                </goals>
                <configuration>
                  <properties>
                      <srcdir>${env.srcDir}</srcdir>
                      <classdir>${env.clsDir}</classdir>
                      <appname>${env.appName}</appname>
                  </properties>
                  <target>
                    <property environment="env" />
                    <!-- Execute an ant task within maven -->
                    <echo message="Generating JaCoCo Reports" />
                    <taskdef name="report" classname="org.jacoco.ant.ReportTask">
                      <classpath path="${basedir}/target/jacoco-jars/org.jacoco.ant.jar" />
                    </taskdef>
                    <mkdir dir="${basedir}/target/coverage-report" />
                    <report>
                      <executiondata>
                        <fileset dir="${basedir}/target">
                          <include name="jacoco.exec" />
                        </fileset>
                      </executiondata>
                      <structure name="Raptor Coverage Project">
                        <group name="${env.appName}">
                          <classfiles>
                            <fileset dir="${env.clsDir}" />
                          </classfiles>
                          <sourcefiles encoding="UTF-8">
                            <fileset dir="${env.srcDir}" />
                          </sourcefiles>
                        </group>
                      </structure>
                      <html destdir="${basedir}/target/coverage-report/html" />
                      <xml destfile="${basedir}/target/coverage-report/coverage-report.xml" />
                      <csv destfile="${basedir}/target/coverage-report/coverage-report.csv" />
                    </report>
                  </target>
                </configuration>
              </execution>
            </executions>
            <dependencies>
              <dependency>
                <groupId>org.jacoco</groupId>
                <artifactId>org.jacoco.ant</artifactId>
                <version>0.7.4.201502262128</version>
              </dependency>
            </dependencies>
          </plugin>
        </plugins>
      </build>
    </project>''' > jacoco_pom.xml
    
  2. Invoke top level maven targets:

    Maven version: 3.3.3

    Goals: -f jacoco_pom.xml jacoco:dump antrun:run@report

tmarwen
  • 15,750
  • 5
  • 43
  • 62
xploreraj
  • 3,792
  • 12
  • 33
  • 51

1 Answers1

0

Would suggest that you first structure your projects along the standard maven configuration. This isn't a requirement, but will save you some manual configuration.

modulexx
    src
        main/java
        test/java <-- Unit tests always expected here. Usually with suffix '***Test.java'
        it/java   <-- Integration tests always expected here. Usually with suffix '***IT.java'

You can then configure the Maven Surefire plugin and maven failsafe plugin for running your Unit tests and Integration tests separately, and produce independent reports. You'll find details in the links below for how to configure your pom.xml to use these separate folders.

http://maven.apache.org/surefire/maven-surefire-plugin/

http://maven.apache.org/surefire/maven-failsafe-plugin/

beirtipol
  • 823
  • 5
  • 21
  • FTs are located in external folder and it can not be changed. Check the edits I made please, it might make more sense. – xploreraj Mar 07 '17 at 06:59
  • You can still leverage the separate runners for maven to execute the tests. See the documentation for configuring source paths for integration tests. This will give you separate output folders for results which you can then configure your CI tooling to read – beirtipol Mar 07 '17 at 08:22
  • Hi, I followed the regex solution in http://stackoverflow.com/a/39562385/1841181. But with this, in the jenkins job for test running and jacoco report making, I am getting error: `[ERROR] Could not find goal 'run@report' in plugin org.apache.maven.plugins:maven-antrun-plugin:1.3 among available goals help, run -> [Help 1]` – xploreraj Mar 14 '17 at 10:08
  • Sounds like a typo. Paste your full command line in a comment t – beirtipol Mar 14 '17 at 22:10
  • See the post steps in last part of post. The goal is there. Same. All in all see, the structure I have posted. Now I cant permanently modify pom of each module to exclude unit tests while the code build runs in jenkins, its needed. But I just want to exclude the modules that are not functional tests (only one is FT) from unit tests while code build is done. This because, when the build runs, then Jenkins shows a link called Test Results which shows how many tests have failed, passed. – xploreraj Mar 15 '17 at 18:45