Code Coverage for Cucumber Tests using Jacoco

妖精的绣舞 提交于 2021-01-29 10:52:17

问题


I am trying to integrate Jacoco to get the code coverage for my Cucumber tests using Maven. Following is my project structure:

  • -src-main-java-Pages

  • -src-main-java-Helper

  • -src-test-java-resources-features

  • -src-test-java-Steps

Following is the Jacoco configuration in my POM.xml

          <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.2</version>
            <configuration>
                <destFile>${basedir}/target/coverage-reports/jacoco.exec</destFile>
                <dataFile>${basedir}/target/coverage-reports/jacoco.exec</dataFile>
                <outputDirectory>${basedir}/target/coverage-reports/jacoco</outputDirectory>
            </configuration>
            <executions>
                <execution>
                    <id>prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
                <execution>
                    <id>check</id>
                    <goals>
                        <goal>check</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <rule>
                                <element>PACKAGE</element>
                                <includes>
                                    <include>**/*Steps.*</include>
                                </includes>
                            </rule>
                            <rule>
                                <element>PACKAGE</element>
                                <excludes>
                                    <exclude>**/*Pages.*</exclude>
                                    <exclude>**/*Page.*</exclude>
                                </excludes>
                            </rule>
                        </rules>
                    </configuration>
                </execution>
            </executions>
        </plugin>

I am able to generate the code coverage report. However, in the report, it covers all the classes in the -src-main packages. As per different google researches and SO posts, I tried to modify my POM to exclude Pages and include only Steps in my code coverage report. But somehow, it keeps on displaying all the files in the main package.

Am I taking the wrong approach? I just want to create a report which does code coverage for only the tests that I execute rather than all the files in the main package.


回答1:


See https://maven.apache.org/guides/mini/guide-configuring-plugins.html - you specify exclusions in configuration of

<execution>
    <id>check</id>
    <goals>
        <goal>check</goal>

but not in configuration of

<execution>
    <id>report</id>
    <goals>
        <goal>report</goal>

so it has effect on check, but not on report.



来源:https://stackoverflow.com/questions/58536904/code-coverage-for-cucumber-tests-using-jacoco

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!