在jacoco-it目录中进行集成测试 [英] Have integration tests in jacoco-it directory

查看:100
本文介绍了在jacoco-it目录中进行集成测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有集成测试,可以很好地执行,但是Jacoco认为它们是单元测试.如何告诉Jacoco将其视为集成测试并显示其图形覆盖率,而不是在jacoco-ut目录中,而是在jacoco-it目录中显示该图形覆盖率?

I have integration tests and they are executed fine, but Jacoco considers them as unit tests. How to tell Jacoco to see them as integration tests and display their graph coverage, not in the jacoco-ut directory, but it the jacoco-it directory ?

在Maven配置中:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
      <argLine>${surefireArgLine}</argLine>
      <excludes>
        <exclude>**/it/java/*.java</exclude>
      </excludes>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.10</version>
    <executions>
      <execution>
        <id>add-test-source</id>
        <phase>process-test-sources</phase>
        <goals>
          <goal>add-test-source</goal>
        </goals>
        <configuration>
          <sources>
            <source>src/it/java</source>
          </sources>
        </configuration>
      </execution>
      <execution>
        <id>add-test-resource</id>
        <phase>generate-test-resources</phase>
        <goals>
          <goal>add-test-resource</goal>
        </goals>
        <configuration>
          <resources>
            <resource>
              <directory>src/it/resources</directory>
            </resource>
          </resources>
        </configuration>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
      <execution>
        <id>add-it-resources</id>
        <phase>pre-integration-test</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <outputDirectory>${project.build.directory}/it-classes</outputDirectory>
          <resources>
            <resource>
              <directory>src/it/resources</directory>
            </resource>
          </resources>
        </configuration>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
      <argLine>${failsafeArgLine}</argLine>
      <testSourceDirectory>src/it/java</testSourceDirectory>
      <testClassesDirectory>${project.build.directory}/it-classes</testClassesDirectory>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.6.201602180812</version>
    <configuration>
      <skip>${maven.test.skip}</skip>
      <output>file</output>
      <append>true</append>
      <excludes>
        <exclude>**/config/*</exclude>
        <exclude>**/dialect/*</exclude>
      </excludes>
    </configuration>        
    <executions>
      <execution>
        <id>pre-unit-test</id>
        <phase>process-test-classes</phase>
        <goals>
          <goal>prepare-agent</goal>
        </goals>
        <configuration>
          <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
          <propertyName>surefireArgLine</propertyName>
          <includes><include>**/ut/*</include></includes>
        </configuration>
      </execution>
      <execution>
        <id>post-unit-test</id>
        <phase>test</phase>
        <goals>
          <goal>report</goal>
        </goals>
        <configuration>
          <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
          <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
        </configuration>
      </execution>
      <execution>
        <id>pre-integration-test</id>
        <phase>pre-integration-test</phase>
        <goals>
          <goal>prepare-agent-integration</goal>
        </goals>
        <configuration>
          <destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
          <propertyName>failsafeArgLine</propertyName>
          <includes><include>**/it/*</include></includes>
        </configuration>
      </execution>
      <execution>
        <id>post-integration-test</id>
        <phase>post-integration-test</phase>
        <goals>
          <goal>report-integration</goal>
        </goals>
        <configuration>
          <dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
          <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>

更新:我在maven-failsafe-plugin插件中缺少此位:

UPDATE: I was missing this bit in the maven-failsafe-plugin plugin:

<executions>
  <execution>       
    <goals>                       
      <goal>integration-test</goal>               
    </goals>                                                  
  </execution>                                                          
</executions> 

按如下所示添加后:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
      <argLine>${failsafeArgLine}</argLine>
      <includes>
        <include>**/it/**</include>
      </includes>
    </configuration>
    <executions>
      <execution>       
        <goals>                       
          <goal>integration-test</goal>               
        </goals>                                                  
      </execution>                                                          
    </executions>                                                                   
  </plugin>

集成测试报告显示了集成测试.

the integration tests report shows the integration tests.

最终的完整配置是:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
      <argLine>${surefireArgLine}</argLine>
      <includes>
        <include>**/ut/**</include>
      </includes>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.10</version>
    <executions>
      <execution>
        <id>add-test-source</id>
        <phase>process-test-sources</phase>
        <goals>
          <goal>add-test-source</goal>
        </goals>
        <configuration>
          <sources>
            <source>src/it/java</source>
          </sources>
        </configuration>
      </execution>
      <execution>
        <id>add-test-resource</id>
        <phase>generate-test-resources</phase>
        <goals>
          <goal>add-test-resource</goal>
        </goals>
        <configuration>
          <resources>
            <resource>
              <directory>src/it/resources</directory>
            </resource>
          </resources>
        </configuration>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
      <argLine>${failsafeArgLine}</argLine>
      <includes>
        <include>**/it/**</include>
      </includes>
    </configuration>
    <executions>
      <execution>
        <goals>
          <goal>integration-test</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.6.201602180812</version>
    <configuration>
      <skip>${maven.test.skip}</skip>
      <output>file</output>
      <append>true</append>
      <excludes>
        <exclude>**/config/*</exclude>
        <exclude>**/dialect/*</exclude>
      </excludes>
    </configuration>        
    <executions>
      <execution>
        <id>pre-unit-test</id>
        <phase>process-test-classes</phase>
        <goals>
          <goal>prepare-agent</goal>
        </goals>
        <configuration>
          <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
          <propertyName>surefireArgLine</propertyName>
        </configuration>
      </execution>
      <execution>
        <id>post-unit-test</id>
        <phase>test</phase>
        <goals>
          <goal>report</goal>
        </goals>
        <configuration>
          <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
          <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
        </configuration>
      </execution>
      <execution>
        <id>pre-integration-test</id>
        <phase>pre-integration-test</phase>
        <goals>
          <goal>prepare-agent-integration</goal>
        </goals>
        <configuration>
          <destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
          <propertyName>failsafeArgLine</propertyName>
        </configuration>
      </execution>
      <execution>
        <id>post-integration-test</id>
        <phase>post-integration-test</phase>
        <goals>
          <goal>report-integration</goal>
        </goals>
        <configuration>
          <dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
          <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>

推荐答案

您误解了maven-failsafe-plugin的include标签的模式.该模式用于测试类路径中类的包名称(另请参见 https://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html ).

You misunderstood the pattern for the include tag of maven-failsafe-plugin. The pattern is used for the package name of the classes within the test classpath (see also https://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html).

build-helper-maven-plugin在测试类路径中包括src/it/java中的所有类.然后,两个测试插件(surefire,failsafe)中的include标记模式都会过滤该测试类路径中的类.这些过滤的类由测试插件执行.

The build-helper-maven-plugin includes all classes in src/it/java to the test classpath. Then the pattern of include tag in both test plugins (surefire, failsafe) filters the classes within this test classpath. These filtered classes are executed by the test plugins.

所以工作流程是

  1. Build-Helper-Plugin扩展了必须编译和执行的测试类集.因此,将使用source指令.此source指令中定义的路径位置与Maven项目路径有关.
  2. 编译器插件编译Java类.
  3. Jacoco-Plugin应该使用单元测试来衡量生产类别中的覆盖率.因此,必须在执行测试之前准备正常代理.您可以指定在覆盖率测量中应包括或排除哪些生产代码(include/exclude指令).这些指令中的模式是基于包的(使用斜线代替点).
  4. Surefire-Plugin执行标准类名与include指令的模式匹配的测试.该模式使用斜杠代替点.
  5. 现在,Jacoco-Plugin应该使用集成测试来测量生产类别中的覆盖率.因此,必须在执行测试之前准备集成代理.同样,您可以指定在覆盖率测量中应该包括或排除哪些生产代码(include/exclude指令).这些指令中的模式是基于包的(使用斜线代替点).
  6. 故障安全插件也是如此.它执行其完全合格的类名与include指令的模式匹配的测试.该模式使用斜杠代替点.

这篇关于在jacoco-it目录中进行集成测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆