首页 > 解决方案 > 执行 mvn install 时单元测试执行两次

问题描述

单元测试被执行两次。

当我从 pom 中的 maven 插件中删除目标报告和阶段准备包时,测试会执行一次,但不会在控制台中生成覆盖率。

但是当我在 pom 中添加来自 maven 插件的目标报告和阶段准备包时,我在控制台中得到了覆盖,但单元测试被执行了两次。

我需要在我的 pom 中有目标报告和阶段准备包才能获得覆盖,但只需要运行一次测试用例。什么是让测试用例只执行一次覆盖的方法。

<plugins>
        <!-- Configure maven-compiler-plugin to use the desired Java version -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
        <!-- Use build-helper-maven-plugin to add Scala source and test source
            directories -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/main/scala</source>
                        </sources>
                    </configuration>
                </execution>
                <execution>
                    <id>add-test-source</id>
                    <phase>generate-test-sources</phase>
                    <goals>
                        <goal>add-test-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/test/scala</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- Use scala-maven-plugin for Scala support -->
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <version>3.2.2</version>
            <executions>
                <execution>
                    <goals>
                        <!-- Need to specify this explicitly, otherwise plugin won't be called
                            when doing e.g. mvn compile -->
                        <goal>compile</goal>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- disable surefire -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.7</version>
            <configuration>
           <skipTests>true</skipTests>

         </configuration>
        </plugin>

        <plugin>
            <groupId>org.scoverage</groupId>
            <artifactId>scoverage-maven-plugin</artifactId>
            <version>${scoverage.plugin.version}</version>
            <configuration>
                <aggregate>true</aggregate>
                <highlighting>true</highlighting>
                <scalacPluginVersion>1.3.0</scalacPluginVersion>
                <minimumCoverage>30</minimumCoverage>
                <failOnMinimumCoverage>false</failOnMinimumCoverage>
            </configuration>

                            <executions>
                                <execution>
                                    <goals>
                                        <goal>report</goal> <!-- or integration-check -->
                                    </goals>
                                    <phase>prepare-package</phase> <!-- or any other phase -->

                              </execution>
                            </executions>

        </plugin>
        <!-- enable scalatest -->
        <plugin>
            <groupId>org.scalatest</groupId>
            <artifactId>scalatest-maven-plugin</artifactId>
            <version>1.0</version>
            <configuration>
                <argLine>
                    -javaagent:"${settings.localRepository}"/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
                </argLine>

                <forkMode>once</forkMode>

                <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
                <junitxml>.</junitxml>
                <filereports>WDF TestSuite.txt</filereports>
                <!-- The scalatest-maven-plugin seems broken for the spanScaleFactor,
                    so pass it via system property, instead -->
                <!-- <spanScaleFactor>${scalatest.span.scale.factor}</spanScaleFactor> -->
                <systemProperties>
                    <spanScaleFactor>${scalatest.span.scale.factor}</spanScaleFactor>
                </systemProperties>

            </configuration>
            <executions>
                <execution>
                    <id>test</id>
                    <goals>
                        <goal>test</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>enter code here

标签: maven

解决方案


你需要做两件事:

  1. 在“coverage-maven-plugin”插件的“配置”部分添加以下行。

    <additionalForkedProjectProperties>skipTests=false</additionalForkedProjectProperties>
    
  2. 在根 pom.xml 中设置以下属性

    <properties>
         <!--  Add other properties here... -->
    
         <!--  skipTests is set to 'true' here to avoid duplicate runs of all test cases. It's set to false in scoverage-maven-plugin below. -->
         <skipTests>true</skipTests>
     </properties>
    

推荐阅读