有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用cobertura插件跳过*测试*

我们正在开发一个大项目,许多交互模块和团队在这些模块上工作。我们已经用jenkins实现了CI,它会定期和提交运行junit测试、fitnesse测试和cobertura覆盖的构建

由于我们要与这么多组件交互,对于一些特定的项目,我们已经实现了集成测试,这会带来一个Spring上下文,并运行许多模拟应用程序流重要部分的测试用例。为了简单/方便起见,它们被实现为Junit,放在src/test文件夹中,但不是单元测试

我们的问题是,一些组件构建需要很长时间才能运行,其中一个确定的问题是,长时间运行的集成测试运行了两次,一次在测试阶段,一次在cobertura阶段(cobertura对类进行指令化,然后再次运行测试)。这就引出了一个问题:是否有可能将测试排除在cobertura执行之外

在pom cobertura配置中使用exclude或ignore只适用于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在使用测试目标时重新运行它们


共 (1) 个答案

  1. # 1 楼答案

    您始终可以使用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