-2

我有一个具有单元和集成测试的项目。我有两个测试套件:

@RunWith(ClasspathSuite.class)
@ClasspathSuite.ClassnameFilters({ ".*Test", "!.*IntegrationTest", "!.*ResourceTest", "!.*DAOTest" })
@ClasspathSuite.SuiteTypes({ SuiteType.JUNIT38_TEST_CLASSES, SuiteType.TEST_CLASSES, SuiteType.RUN_WITH_CLASSES })
public class AutoTestSuite {
}

在大约 20 秒内运行 70 次测试

@RunWith(ClasspathSuite.class)
@ClasspathSuite.SuiteTypes({ SuiteType.JUNIT38_TEST_CLASSES, SuiteType.TEST_CLASSES, SuiteType.RUN_WITH_CLASSES })
@ClasspathSuite.ClassnameFilters({ ".*IntegrationTest" })
public class IntegrationTestSuite {
}

它在大约 80 秒内运行 99 个集成测试。

这很好,但是当我尝试在本地和 Jenkins 内部运行我的 maven 构建时,测试数字在 12-18 分钟之间运行 348 个测试。显然是我的 pom.xml,甚至考虑到 jacoco 来检查。谁能看到我用“ mvn clean package调用的以下内容有什么问题:

<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.6.0.201210061924</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <propertyName>jacoco.argLine.unit</propertyName>
                            <destFile>${jacoco.destFile.unit}</destFile>
                        </configuration>
                    </execution>
                    <execution>
                        <id>pre-integration-test</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <propertyName>jacoco.argLine.it</propertyName>
                            <destFile>${jacoco.destFile.it}</destFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- UNIT tests only with mvn clean test -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${version.surefire}</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>${version.surefire}</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <argLine>${jacoco.argLine.unit}
                        -Dfile.encoding=${project.build.sourceEncoding} -Xmx512m
                    </argLine>
                    <forkMode>always</forkMode>
                    <parallel>classes</parallel>
                    <includes>
                        <include>**/*.class</include>
                    </includes>
                    <excludes>
                        <exclude>**/*.IntegrationTest.java</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <!-- INTEGRATION tests that are run with "mvn clean verify" -->

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>${version.surefire}</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>${version.surefire}</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <forkMode>pertest</forkMode>
                    <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
                    <argLine>${jacoco.argLine.it}
                        -Dfile.encoding=${project.build.sourceEncoding} -Xmx512m
                    </argLine>
                    <includes>
                        <include>**/*.class</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <id>integration-test</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>integration-test</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>verify</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

测试似乎重复并且花费了这么长时间是否有原因?

4

1 回答 1

2

我不是 maven 或 surefire 专家,但我认为 surefire 插件会执行它在您的测试目录下找到的每个测试文件。这意味着默认配置不需要测试套件。Surefire 找到并运行您的测试套件以及您的其他测试可能是要运行的测试数量增加 2 倍的原因。但我不知道为什么它需要两倍以上的时间才能运行。

如果是这种情况,您可以告诉surefire 只运行您的测试套件:

<configuration>
  ...
  <includes>
    <include>AutoTestSuite.java</include>
  </includes>
</configuration>

代替**/*.class

编辑: 因为 Jacoco 已经检测了您需要使用的类

<configuration>
  ...
  <includes>
    <include>AutoTestSuite.class</include>
  </includes>
</configuration>

相反,否则您不会运行检测版本。

于 2013-10-07T23:16:33.837 回答