Jacoco代码覆盖 - 在货物中运行的战争文件

时间:2016-03-10 16:29:44

标签: maven integration-testing jacoco cargo

之前我问过这个问题但从来没有得到答案,我从博客文章中假设我已经阅读过这是可能的但是我无法让它起作用。

我想在我的maven构建中使用selenium运行集成测试,我使用货物插件将我的war文件部署到tomcat,集成测试使用selenium。我在启动时将jacoco arg line参数传递给货物,但无法获得覆盖结果。

我的所有测试都在运行,但每当我查看生成的jacoco报告进行集成测试时,总是会有零覆盖。

我发布了我的整个pom文件,因为它非常小,这可能意味着我得到了一些帮助以使其正常工作。

 <?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>uk.co</groupId>
<artifactId>netart</artifactId>
<name>MockitoTests</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
    <java-version>1.6</java-version>
    <org.springframework-version>3.2.7.RELEASE</org.springframework-version>
    <org.aspectj-version>1.6.10</org.aspectj-version>
    <org.slf4j-version>1.6.6</org.slf4j-version>                      
</properties>

<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${org.springframework-version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>[2.3.0,2.3.99]</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>[2.3.0,2.3.99]</version>
    </dependency>

    <!-- AspectJ -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${org.aspectj-version}</version>
    </dependency>

    <!-- Logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${org.slf4j-version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.15</version>
        <exclusions>
            <exclusion>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
            </exclusion>
            <exclusion>
                <groupId>javax.jms</groupId>
                <artifactId>jms</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jdmk</groupId>
                <artifactId>jmxtools</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jmx</groupId>
                <artifactId>jmxri</artifactId>
            </exclusion>
        </exclusions>
        <scope>runtime</scope>
    </dependency>

    <!-- @Inject -->
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- Test -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.7</version>
        <scope>test</scope>
        <!-- Exclude the Hamcrest core package that comes with JUNIT -->
        <exclusions>
            <exclusion>
                <artifactId>hamcrest-core</artifactId>
                <groupId>org.hamcrest</groupId>
            </exclusion>
        </exclusions>
    </dependency>       
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path-assert</artifactId>
        <version>0.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.github.detro</groupId>
        <artifactId>phantomjsdriver</artifactId>
        <version>1.2.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.6.201602180812</version>
    </dependency>
</dependencies>

<build>
    <finalName>${project.name}</finalName>

    <plugins>
        <plugin>
            <groupId>org.eclipse.m2e</groupId>
            <artifactId>lifecycle-mapping</artifactId>
            <version>1.0.0</version>
            <configuration>
                <lifecycleMappingMetadata>
                    <pluginExecutions>
                        <pluginExecution>
                            <pluginExecutionFilter>
                                <groupId>
                                    com.github.klieber
                                </groupId>
                                <artifactId>
                                    phantomjs-maven-plugin
                                </artifactId>
                                <versionRange>[0.4,)</versionRange>
                                <goals>
                                    <goal>install</goal>
                                </goals>
                            </pluginExecutionFilter>
                            <action>
                                <execute></execute>
                            </action>
                        </pluginExecution>
                    </pluginExecutions>
                </lifecycleMappingMetadata>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <additionalProjectnatures>
                    <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                </additionalProjectnatures>
                <additionalBuildcommands>
                    <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                </additionalBuildcommands>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <compilerArgument>-Xlint:all</compilerArgument>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
                <mainClass>org.test.int1.Main</mainClass>
            </configuration>
        </plugin>

        <!-- Jacoco code coverage plugin for maven (see surefire plugin config) -->
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.5.201505241946</version>
            <executions>
                <!-- Prepares the property pointing to the JaCoCo runtime agent which 
                    is passed as VM argument when Maven the Surefire plugin is executed. -->
                <execution>
                    <id>pre-unit-test</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <!-- Sets the path to the file which contains the execution data. -->
                        <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
                        <!-- Sets the name of the property containing the settings for JaCoCo 
                            runtime agent. -->
                        <propertyName>surefireArgLine</propertyName>
                        <includes>
                            <include>uk/co/**</include>
                        </includes>
                    </configuration>
                </execution>
                <!-- Ensures that the code coverage report for unit tests is created 
                    after unit tests have been run. -->
                <execution>
                    <id>post-unit-test</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <!-- Sets the path to the file which contains the execution data. -->
                        <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
                        <!-- Sets the output directory for the code coverage report. -->
                        <outputDirectory>${project.reporting.outputDirectory}/jacoco-unit-tests</outputDirectory>
                    </configuration>
                </execution>
                <!-- Prepares the property pointing to the JaCoCo runtime agent which 
                    is passed as VM argument when Maven Failsafe plugin is executed. -->
                <execution>
                    <id>pre-integration-test</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <!-- Sets the path to the file which contains the execution data. -->
                        <destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
                        <includes>
                            <include>uk/co/**</include>
                        </includes>
                        <!-- Sets the name of the property containing the settings for JaCoCo 
                            runtime agent. -->
                        <propertyName>failsafeArgLine</propertyName>                                                
                    </configuration>
                </execution>
                <!-- Ensures that the code coverage report for integration tests after 
                    integration tests have been run. -->
                <execution>
                    <id>post-integration-test</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <!-- Sets the path to the file which contains the execution data. -->
                        <dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
                        <!-- Sets the output directory for the code coverage report. -->
                        <outputDirectory>${project.reporting.outputDirectory}/jacoco-int-tests</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- Sure fire unit test plugin configuration -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.15</version>
            <configuration>
                <!-- Sets the VM argument line used when unit tests are run. -->
                <argLine>${surefireArgLine}</argLine>
                <!-- Skips unit tests if the value of skip.unit.tests property is true -->
                <skipTests>${skip.unit.tests}</skipTests>
                <!-- Excludes integration tests when unit tests are run. -->
                <excludes>
                    <exclude>**/IT*.java</exclude>
                </excludes>
            </configuration>
        </plugin>

        <!-- Maven Failsafe Plugin for integration testing -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.12</version>
            <executions>
                <execution>
                    <id>default</id>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!-- Sets the VM argument line used when integration tests are run. -->
                <argLine>${failsafeArgLine}</argLine>
                <!-- Skips integration tests if the value of skip.integration.tests 
                    property is true -->
                <!-- <skipTests>${skip.integration.tests}</skipTests> -->
                <systemPropertyVariables>
                    <phantomjs.binary>${phantomjs.binary}</phantomjs.binary>
                </systemPropertyVariables>
                    <includes>
                        <include>**/*IT*</include>
                    </includes>                 
            </configuration>                
        </plugin>

        <!-- Installs PhantomJS so it doesn't have to be pre-installed -->
        <plugin>
            <groupId>com.github.klieber</groupId>
            <artifactId>phantomjs-maven-plugin</artifactId>
            <version>0.7</version>
            <executions>
                <execution>
                    <goals>
                        <goal>install</goal>
                    </goals>
                </execution>
            </executions>
            <!-- The version of phantom js -->
            <configuration>
                <version>2.0.0</version>
            </configuration>
        </plugin>

        <!-- Maven Cargo Plugin - starts tomcat or jetty for integration tests -->
        <plugin>
            <groupId>org.codehaus.cargo</groupId>
            <artifactId>cargo-maven2-plugin</artifactId>
            <version>1.2.0</version>
            <configuration>
                <type>standalone</type>
                <!-- <skip>${skip.integration.tests}</skip> -->
                <!-- Install apache tomcat into the target directory -->
                <container>
                    <containerId>tomcat7x</containerId>
                    <zipUrlInstaller>
                        <url>http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.16/bin/apache-tomcat-7.0.16.zip
                        </url>
                        <downloadDir>${project.build.directory}/downloads</downloadDir>
                        <extractDir>${project.build.directory}/extracts</extractDir>
                    </zipUrlInstaller>                      
                </container>
                <deployables>
                    <deployable>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>${project.artifactId}</artifactId>
                        <type>war</type>                           
                    </deployable>
                </deployables>                                  
            </configuration>

            <executions>
                <execution>
                    <id>start container</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>start</goal>
                    </goals>
                    <configuration>
                        <configuration>
                            <properties>
                                <cargo.jvmargs>${failsafeArgLine}</cargo.jvmargs>                           
                                <cargo.servlet.port>10001</cargo.servlet.port>                                          
                            </properties>
                        </configuration>
                    </configuration>                                                               
                </execution>
                <execution>
                    <id>stop</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
                <groupId>com.soebes.maven.plugins</groupId>
              <artifactId>maven-echo-plugin</artifactId>
              <version>0.1</version>
              <executions>
                <execution>
                  <phase>pre-integration-test</phase>
                  <goals>
                    <goal>echo</goal>
                  </goals>
                </execution>
              </executions>
            <configuration>
            <echos>
              <echo>This is the Text which will be printed out.</echo>
              <echo>Displaying value of failsafeArgLineProperty</echo>
            <echo>[failsafeArgLine] ${failsafeArgLine}</echo>
            </echos>
          </configuration>
        </plugin>       
    </plugins>
    <pluginManagement>
        <plugins>
            <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>
                                        com.soebes.maven.plugins
                                    </groupId>
                                    <artifactId>
                                        maven-echo-plugin
                                    </artifactId>
                                    <versionRange>
                                        [0.1,)
                                    </versionRange>
                                    <goals>
                                        <goal>echo</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

为集成测试和单元测试创​​建了jacoco报告,单元测试结果还可以,报告显示它们,但无论我尝试过什么,我都无法获得硒驱动集成测试的测试覆盖率结果。

希望有人这样做并且可以帮助我!

1 个答案:

答案 0 :(得分:0)

也许你会收到这样的消息?

[WARN] [09:31:44.247] Coverage information was not collected. Perhaps you forget to include debug information into compiled classes? 

在这种情况下,您可能需要使用debug = true配置maven-compiler-plugin以将调试信息包含到已编译的类中:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <inherited>true</inherited>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <debug>true</debug>
            </configuration>
        </plugin>
    </plugins>
</build>