繁体   English   中英

使用cobertura插件跳过* test *

[英]Skip a *test* with cobertura plugin

我们正在开发一个大项目,许多交互模块和团队正在开发所述模块。 我们用jenkins实现了CI,它执行定期和on-commit构建,运行junit测试,fitnesse测试和cobertura覆盖。

由于我们正在与如此多的组件进行交互,因此对于某些特定项目,我们实现了集成测试,这些测试会调出Spring上下文,并运行许多模拟应用程序流的重要部分的测试用例。 为了简单/方便,这些实现为Junit,放在src / test文件夹中,但不是单元测试。

我们的问题是一些组件构建需要很长时间才能运行,并且已确定的问题之一是长时间运行的集成测试运行两次,一次在测试阶段,一次在cobertura阶段(作为cobertura仪器的类,然后再次运行测试)。 这导致了一个问题: 是否可以从cobertura执行中排除测试

在pom中使用exclude或ignore cobertura config仅适用于src / java类,而不适用于测试类。 我在cobertura插件文档中找不到任何内容。 我正在尝试通过配置找到一种方法。 我认为可以实现的唯一另一种方法是将这些测试移动到另一个没有启用cobertura插件的maven模块,并让该模块成为集成测试的主页。 这样,父pom的构建将触发集成测试,但它不属于cobertura的范围。 但如果它可以通过配置来完成,那将会更加容易:)

在此先感谢,JG

====更新和解决方案====

在构建kkamilpl的答案之后(再次感谢!)我能够在不改变目录结构中的任何内容的情况下包含和排除所需的测试。 只有java风格的表达式,一旦你意识到在surefire插件设置中覆盖,你可以设法运行“除了包之外的所有包”/“只有给定的包”,如下所示:

<profiles>
    <profile>
        <id>unit-tests</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <testcase.include>**/*.class</testcase.include>
            <testcase.exclude>**/integration/*.class</testcase.exclude>
        </properties>
    </profile>
    <profile>
        <id>integration-tests</id>
        <properties>
            <testcase.include>**/integration/*.class</testcase.include>
            <testcase.exclude>**/dummyStringThatWontMatch/*.class</testcase.exclude>
        </properties>
    </profile>
</profiles>

我能够使用test目标和默认配置文件运行所有单元测试(即除了integration测试文件夹的内容之外的所有内容),然后使用integration-tests配置文件调用目标test以运行集成测试。 然后就是在jenkins中添加对新配置文件的调用作为新的顶级目标调用(它针对父pom),以便jenkins构建将运行集成测试,但只运行一次,而不是让它们重新运行当cobertura使用测试目标时。

您可以随时使用maven配置文件: http//maven.apache.org/guides/introduction/introduction-to-profiles.html

将测试分开到不同的目录,例如:

testsType1
    SomeTest1.java
    SomeTest2.java
testsType2
    OtherTest1.java
    OtherTest2.java

接下来在pom中定义每种测试类型的正确配置文件,例如:

    <profile>
        <id>testsType1</id>
        <properties>
            <testcase.include>%regex[.*/testsType1/.*[.]class]</testcase.include>
            <testcase.exclude>%regex[.*/testsType2/.*[.]class]</testcase.exclude>
        </properties>
    </profile>

要定义默认配置文件:

        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>

最后定义surefire插件:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>>
            <configuration>
                <includes>
                    <include>${testcase.include}</include>
                </includes>
                <excludes>
                    <exclude>${testcase.exclude}</exclude>
                </excludes>
            </configuration>
        </plugin>

用它来打电话

 mvn test #to use default profile
 mvn test -P profileName #to use defined profileName

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM