【问题标题】:Maven Integration tests and unit tests with profile带有配置文件的 Maven 集成测试和单元测试
【发布时间】:2017-08-14 14:04:26
【问题描述】:

我想知道为什么它不能正确工作。我想要 itTest 配置文件,它将通过 Maven 开始集成(故障安全插件)和单元(surefire 插件)。 我的配置:

<plugins>
    <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skip>${unit-tests.skip}</skip>
                <excludes>
                    <exclude>**/*IT.java</exclude>
                </excludes>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                    <configuration>
                        <skip>${integration-tests.skip}</skip>
                        <includes>
                            <include>**/*IT.java</include>
                        </includes>
                    </configuration>
                </execution>
            </executions>
        </plugin>
</plugins>

简介

<profile>
        <id>itTest</id>
        <properties>
            <propFile>profiles/itTest.properties</propFile>
            <spring.profile>itTest</spring.profile>
            <integration-tests.skip>false</integration-tests.skip>
            <unit-tests.skip>false</unit-tests.skip>
        </properties>
    </profile>

结果:两个测试都被跳过...即使我将配置文件或硬编码的跳过更改为 false,它仍然不起作用。

【问题讨论】:

  • 首先,maven-surefire-plugin 中的 exclude 是多余的,因为它是 maven-surefire-plugin 中的默认值。 maven-failsafe-plugin 中的 include 也是多余的,因为它是 maven-failsave-plugin 中的默认值...
  • 你真的用mvn -P itTest激活了个人资料吗?
  • 是的,我通过 Intellij 界面来执行此操作,我在其中声明配置文件和命令,其中命令验证但如果我更改它为 true 并且单元测试也可能是另一个命令,如 install
  • 如果同时删除 excludesincludes 块会怎样?
  • 没什么变化,但我发现了有趣的事情。当我使用硬编码值运行clean install 时,例如 false/true 测试是否正在运行,但是当使用任何配置文件运行时,它们会被跳过

标签: java maven unit-testing


【解决方案1】:

与其将两个插件都作为默认插件并尝试跳过它们,我认为您可以简单地在配置文件本身中声明插件。 即

<profile>
    <id>itTest</id>
    <plugins>
      <plugin>
        <artifactId>maven-failsafe-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                </goals>
                <configuration>
                    <includes>
                        <include>**/*IT.java</include>
                    </includes>
                </configuration>
            </execution>
        </executions>
      </plugin>
    <plugins>

【讨论】:

    【解决方案2】:

    我不知道为什么您的配置并没有按照您希望的方式执行(可能与评估属性的顺序有关,因此在您重置属性时已经评估了该属性),但通常的方法是处理这个是将整个插件配置放入配置文件中。

    【讨论】:

    • 是的,它可能是解决方案,但 id 不满足我,我想要全局插件,其他配置文件可以使用它(所以我声明变量来管理安全和故障安全)
    • 你能详细说明你的想法和它应该是什么样子吗?
    • 好的,我试试。假设我有 3 个带有这些变量的配置文件(unit-test.skip,integration-test.skip),现在只有 itTest 将运行这两个测试,但将来我想灵活一些,如果我想运行单元测试我的 1 个配置文件我只需要在这个特定配置文件中将变量从 false 更改为 true。这就是为什么我不想将其拉入 itTest 配置文件。
    猜你喜欢
    • 2016-01-23
    • 2013-03-18
    • 2013-12-31
    • 1970-01-01
    • 2011-03-10
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多