Pet*_*ney 160 java testing maven-2 surefire
我有一个maven2多模块项目,在我的每个子模块中,我都有JUnit测试,这些测试都是命名的Test.java,分别Integration.java用于单元测试和集成测试.当我执行:
mvn test
*Test.java执行子模块中的所有JUnit测试.当我执行
mvn test -Dtest=**/*Integration
没有任何Integration.java测试在子模块中执行.
这些看起来像是完全相同的命令,但是带有-Dtest =/*Integration**的命令不起作用它显示在父级别运行的0个测试,没有任何测试
Kie*_*ief 232
的Maven构建的生命周期现在包括"集成测试"相对于运行集成测试,这是从在"测试"阶段运行单元测试单独运行.它在"package"之后运行,因此如果你运行"mvn verify","mvn install"或"mvn deploy",集成测试将一路运行.
默认情况下,集成测试运行在名为测试类**/IT*.java,**/*IT.java和**/*ITCase.java,但可以配置.
有关如何连接所有内容的详细信息,请参阅Failsafe插件,Failsafe使用页面(在我编写此内容时未在上一页中正确链接),还可以查看此Sonatype博客文章.
ser*_*g10 96
您可以设置Maven的Surefire来单独运行单元测试和集成测试.在标准单元测试阶段,您运行的所有模式都不符合集成测试.然后,您创建第二个仅运行集成测试的测试阶段.
这是一个例子:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>test</goal>
</goals>
<phase>integration-test</phase>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
HDa*_*ave 60
我已经完成了你想要做的事情并且效果很好.单元测试"*Tests"始终运行,"*IntegrationTests"仅在执行mvn verify或mvn安装时运行.这是我POM的片段.serg10几乎是正确的....但不完全.
<plugin>
<!-- Separates the unit tests from the integration tests. -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- Skip the default running of this plug-in (or everything is run twice...see below) -->
<skip>true</skip>
<!-- Show 100% of the lines from the stack trace (doesn't work) -->
<trimStackTrace>false</trimStackTrace>
</configuration>
<executions>
<execution>
<id>unit-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<!-- Never skip running the tests when the test phase is invoked -->
<skip>false</skip>
<includes>
<!-- Include unit tests within integration-test phase. -->
<include>**/*Tests.java</include>
</includes>
<excludes>
<!-- Exclude integration tests within (unit) test phase. -->
<exclude>**/*IntegrationTests.java</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<!-- Never skip running the tests when the integration-test phase is invoked -->
<skip>false</skip>
<includes>
<!-- Include integration tests within integration-test phase. -->
<include>**/*IntegrationTests.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
祝好运!
小智 29
您可以使用JUnit类别和Maven轻松拆分它们.
下面通过拆分单元和集成测试非常简单地展示了这一点.
public interface IntegrationTest {}
Run Code Online (Sandbox Code Playgroud)
将类别注释添加到测试类的顶部.它采用新界面的名称.
import org.junit.experimental.categories.Category;
@Category(IntegrationTest.class)
public class ExampleIntegrationTest{
@Test
public void longRunningServiceTest() throws Exception {
}
}
Run Code Online (Sandbox Code Playgroud)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.11</version>
<configuration>
<includes>
<include>**/*.class</include>
</includes>
<excludedGroups>
com.test.annotation.type.IntegrationTest
</excludedGroups>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
当你进行mvn clean测试时,只会运行未标记的单元测试.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<includes>
<include>**/*.class</include>
</includes>
<groups>
com.test.annotation.type.IntegrationTest
</groups>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
配置使用标准执行目标在构建的集成测试阶段运行failsafe插件.
您现在可以执行mvn clean install
这次以及运行的单元测试,集成测试在集成测试阶段运行.
Jam*_*ery 15
你应该尝试使用maven failsafe插件.您可以告诉它包含一组特定的测试.
Liq*_*pie 15
您应该使用maven surefire 插件来运行单元测试和maven failsafe 插件来运行集成测试。
如果您希望使用标志来切换这些测试的执行,请遵循以下步骤。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>${skipUnitTests}</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<includes>
<include>**/*IT.java</include>
</includes>
<skipTests>${skipIntegrationTests}</skipTests>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<properties>
<skipTests>false</skipTests>
<skipUnitTests>${skipTests}</skipUnitTests>
<skipIntegrationTests>${skipTests}</skipIntegrationTests>
</properties>
Run Code Online (Sandbox Code Playgroud)
因此,将根据以下标志规则跳过或切换测试:
可以通过以下标志跳过测试:
-DskipTests 跳过单元测试和集成测试-DskipUnitTests 跳过单元测试但执行集成测试-DskipIntegrationTests 跳过集成测试但执行单元测试在下面运行以仅执行单元测试
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>${skipUnitTests}</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<includes>
<include>**/*IT.java</include>
</includes>
<skipTests>${skipIntegrationTests}</skipTests>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<properties>
<skipTests>false</skipTests>
<skipUnitTests>${skipTests}</skipUnitTests>
<skipIntegrationTests>${skipTests}</skipIntegrationTests>
</properties>
Run Code Online (Sandbox Code Playgroud)
您可以执行以下命令来运行测试(单元和集成)
mvn clean test
Run Code Online (Sandbox Code Playgroud)
为了仅运行集成测试,请遵循
mvn clean verify
Run Code Online (Sandbox Code Playgroud)
或者跳过单元测试
mvn failsafe:integration-test
Run Code Online (Sandbox Code Playgroud)
此外,为了在 期间跳过集成测试mvn install,请遵循
mvn clean install -DskipUnitTests
Run Code Online (Sandbox Code Playgroud)
您可以使用跳过所有测试
mvn clean install -DskipIntegrationTests
Run Code Online (Sandbox Code Playgroud)
cle*_*tus 13
默认情况下,Maven仅运行在类名称中具有Test的测试.
重命名为IntegrationTest,它可能会工作.
或者,您可以更改Maven配置以包含该文件,但只是将测试命名为SomethingTest可能更容易也更好.
从包含和排除测试:
默认情况下,Surefire插件将自动包含具有以下通配符模式的所有测试类:
- "**/Test*.java" - 包括所有以"Test"开头的子目录和所有java文件名.
- "**/*Test.java" - 包括其所有子目录和所有以"Test"结尾的java文件名.
- "**/*TestCase.java" - 包括其所有子目录和所有以"TestCase"结尾的java文件名.
如果测试类不符合命名约定,则配置Surefire插件并指定要包含的测试.
使用Maven运行集成测试的另一种方法是使用配置文件功能:
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>integration-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
<excludes>
<exclude>**/*StagingIntegrationTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
...
Run Code Online (Sandbox Code Playgroud)
运行'mvn clean install'将运行默认构建.如上所述,集成测试将被忽略.运行'mvn clean install -P integration-tests'将包括集成测试(我也忽略了我的分段集成测试).此外,我有一个CI服务器,每晚运行我的集成测试,为此我发出命令'mvn test -P integration-tests'.