16

我有一些依赖于测试数据的集成测试。此测试数据在阶段创建pre-integration-test并在阶段删除post-integration-test

-DskipITs我的问题是,如果我在 Maven 命令行上使用,这些阶段仍然会执行。

有什么方法-DskipITs可以跳过预集成测试和集成测试后阶段?

这是 pom 中的插件定义:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<version>1.5</version>

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
</dependencies>

<configuration>
    <driver>com.mysql.jdbc.Driver</driver>
    <url>${database.url}</url>
    <username>${database.user}</username>
    <password>${database.pw}</password>
</configuration>

<executions>
    <execution>
        <id>create-integration-test-data</id>
        <phase>pre-integration-test</phase>
        <goals>
            <goal>execute</goal>
        </goals>
        <configuration>
            <orderFile>descending</orderFile>
            <fileset>
                <basedir>${basedir}/src/test/resources/sql</basedir>
                <includes>
                    <include>AdministrationTestTeardown.sql</include>
                    <include>AdministrationTestSetup.sql</include>
                </includes>
            </fileset>
        </configuration>
    </execution>

    <execution>
        <id>remove-data-after-test</id>
        <phase>post-integration-test</phase>
        <goals>
            <goal>execute</goal>
        </goals>
        <configuration>
            <fileset>
                <basedir>${basedir}/src/test/resources/sql</basedir>
                <includes>
                    <include>AdministrationTestTeardown.sql</include>
                </includes>
            </fileset>
        </configuration>
    </execution>
</executions>
</plugin>
4

2 回答 2

13

更好的方法是使用skipITs属性来跳过集成前和集成后阶段的执行。

在你的情况下,它看起来像:

<executions>
    <execution>
        <id>create-integration-test-data</id>
        <phase>pre-integration-test</phase>
        <goals>
            <goal>execute</goal>
        </goals>
        <configuration>
            <skip>${skipITs}</skip>
            <orderFile>descending</orderFile>
            <fileset>
                <basedir>${basedir}/src/test/resources/sql</basedir>
                <includes>
                    <include>AdministrationTestTeardown.sql</include>
                    <include>AdministrationTestSetup.sql</include>
                </includes>
            </fileset>
        </configuration>
    </execution>

    <execution>
        <id>remove-data-after-test</id>
        <phase>post-integration-test</phase>
        <goals>
            <goal>execute</goal>
        </goals>
        <configuration>
            <skip>${skipITs}</skip>
            <fileset>
                <basedir>${basedir}/src/test/resources/sql</basedir>
                <includes>
                    <include>AdministrationTestTeardown.sql</include>
                </includes>
            </fileset>
        </configuration>
    </execution>
</executions>

因此,每当您使用-DskipITs运行 mvn 命令时,它将跳过集成测试以及此插件的执行。

于 2015-09-27T06:48:41.657 回答
7

您可以使用由 Maven 插件的存在skipITs<skip>选项激活的配置文件来完全跳过插件的执行。有几种方法可以做到这一点。


设置属性以供稍后检查

您可以在配置文件中设置 Maven 属性,然后使用该属性跳过操作。

<profiles>
    <profile>
        <id>skip-integration-test-data-creation</id>
        <activation>
            <property>
                <name>skipITs</name>
            </property>
        </activation>
        <properties>
            <skip-integration-test-data-creation>true</skip-integration-test-data-creation>
        </properties>
    </profile>
</profiles>

-

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>sql-maven-plugin</artifactId>
    ...
    <skip>${skip-integration-test-data-creation}</skip>
    ...
</plugin>

将插件放入自己的配置文件中

另一种解决方案是将所有<plugin>不应运行的部分放入单独的配置文件中,并在该配置文件-DskipITs处于活动状态时禁用该配置文件。

<profiles>
    <profile>
        <id>skip-integration-test-data-creation</id>
        <activation>
            <property>
                <!-- Disable profile if skipITs is set -->
                <name>!skipITs</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
<!-- Here: All plugins that should not run if skipITs is set -->
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
于 2015-07-15T13:18:56.010 回答