哪个 Maven 阶段将始终在测试阶段之后执行?
Which maven phase will be always executed after test phase?
我实现了一个 Maven 插件,用于在 Maven test
阶段之前创建测试数据库(具有随机名称),并在 test
阶段完成时删除该数据库。
插件需要执行两次,在 test
阶段之前(当用于创建数据库时)和在 test
阶段之后(当用于删除该测试数据库时)。
哪个Maven生命周期阶段总是在测试阶段之后执行,test
阶段是否成功执行?
Maven lifecycle 中没有对应于预测试和 post 测试的特定阶段。这是因为单元测试不应该需要外部环境。听起来你想做的不是单元测试而是集成测试,因为它们需要设置环境。
来自docs:
test
- test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
integration-test
- process and deploy the package if necessary into an environment where integration tests can be run
还有一个pre-integration-test
、integration-test
和post-integration-test
用于设置、运行和破坏测试环境。
pre-integration-test
: perform actions required before integration tests are executed. This may involve things such as setting up the required environment.
integration-test
: process and deploy the package if necessary into an environment where integration tests can be run.
post-integration-test
: perform actions required after integration tests have been executed. This may including cleaning up the environment.
因此,使用 maven-failsafe-plugin
.
在 integration-test
阶段执行此操作会更容易、更清晰
现在,如果您真的想运行将其作为单元测试,我不会将数据库的创建/删除编写为Maven 插件。让您的应用程序在测试环境中配置时创建测试数据库会好得多。 (例如,如果您使用的是 Spring,它有很多功能。)
而且,如果你真的想 运行 作为 test
阶段的单元测试, 和 使用你的插件,你将不得不跳过maven-surefire-plugin
的默认执行,然后定义创建数据库的 Maven 插件的执行,maven-surefire-plugin
的新执行和删除数据库的 Maven 插件的执行,绑定到 test
相.
这是有效的,因为当插件绑定到同一阶段时,Maven 会按 as they are defined in the POM 的顺序调用插件。
配置如下:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<id>default-test</id>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId><!-- group id of your plugin --></groupId>
<artifactId><!-- artifact id of your plugin --></artifactId>
<version><!-- version --></version>
<executions>
<execution>
<id>create-db</id>
<phase>test</phase>
<goals>
<goal><!-- your goal --></goal>
</goals>
<!-- add configuration -->
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<id>test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId><!-- group id of your plugin --></groupId>
<artifactId><!-- artifact id of your plugin --></artifactId>
<version><!-- version --></version>
<executions>
<execution>
<id>drop-db</id>
<phase>test</phase>
<goals>
<goal><!-- your goal --></goal>
</goals>
<!-- add configuration -->
</execution>
</executions>
</plugin>
我实现了一个 Maven 插件,用于在 Maven test
阶段之前创建测试数据库(具有随机名称),并在 test
阶段完成时删除该数据库。
插件需要执行两次,在 test
阶段之前(当用于创建数据库时)和在 test
阶段之后(当用于删除该测试数据库时)。
哪个Maven生命周期阶段总是在测试阶段之后执行,test
阶段是否成功执行?
Maven lifecycle 中没有对应于预测试和 post 测试的特定阶段。这是因为单元测试不应该需要外部环境。听起来你想做的不是单元测试而是集成测试,因为它们需要设置环境。
来自docs:
test
- test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployedintegration-test
- process and deploy the package if necessary into an environment where integration tests can be run
还有一个pre-integration-test
、integration-test
和post-integration-test
用于设置、运行和破坏测试环境。
pre-integration-test
: perform actions required before integration tests are executed. This may involve things such as setting up the required environment.integration-test
: process and deploy the package if necessary into an environment where integration tests can be run.post-integration-test
: perform actions required after integration tests have been executed. This may including cleaning up the environment.
因此,使用 maven-failsafe-plugin
.
integration-test
阶段执行此操作会更容易、更清晰
现在,如果您真的想运行将其作为单元测试,我不会将数据库的创建/删除编写为Maven 插件。让您的应用程序在测试环境中配置时创建测试数据库会好得多。 (例如,如果您使用的是 Spring,它有很多功能。)
而且,如果你真的想 运行 作为 test
阶段的单元测试, 和 使用你的插件,你将不得不跳过maven-surefire-plugin
的默认执行,然后定义创建数据库的 Maven 插件的执行,maven-surefire-plugin
的新执行和删除数据库的 Maven 插件的执行,绑定到 test
相.
这是有效的,因为当插件绑定到同一阶段时,Maven 会按 as they are defined in the POM 的顺序调用插件。
配置如下:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<id>default-test</id>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId><!-- group id of your plugin --></groupId>
<artifactId><!-- artifact id of your plugin --></artifactId>
<version><!-- version --></version>
<executions>
<execution>
<id>create-db</id>
<phase>test</phase>
<goals>
<goal><!-- your goal --></goal>
</goals>
<!-- add configuration -->
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<id>test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId><!-- group id of your plugin --></groupId>
<artifactId><!-- artifact id of your plugin --></artifactId>
<version><!-- version --></version>
<executions>
<execution>
<id>drop-db</id>
<phase>test</phase>
<goals>
<goal><!-- your goal --></goal>
</goals>
<!-- add configuration -->
</execution>
</executions>
</plugin>