我想在启动mvn install
时跳过一次测试。
有没有办法做到这一点 ?
使用junit 4时,我想添加一个@Ignore
注释。这对你有用,除非你有时只想忽略测试,或者只在构建从maven运行时忽略它。如果是这种情况,那么我会问“为什么?”
测试应该是一致的,它们应该是可移植的,并且它们应该总是通过。如果特定测试有问题,我会考虑重新编写,完全删除它,或将其移动到不同的测试套件或项目。
通常需要排除集成测试,但需要包含单元测试。为了实现这一点,我建议使用postfix IntegrationTest(例如AbcIntegrationTest.java)命名所有集成测试。
然后在你的maven构建中提出以下内容:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
</excludes>
</configuration>
</plugin>
使用此构建时,将排除所有集成测试,但运行所有其他测试(例如单元测试)。完美:-)
有关排除和测试运行期间测试的更多信息,请阅读
http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html
PS要排除单个测试,您只需在排除列表中明确命名。简单。
使用this solution注释查看@Category
public class AccountTest {
@Test
@Category(IntegrationTests.class)
public void thisTestWillTakeSomeTime() {
...
}
@Test
@Category(IntegrationTests.class)
public void thisTestWillTakeEvenLonger() {
...
}
@Test
public void thisOneIsRealFast() {
...
}
}
然后使用测试套件运行:
@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@SuiteClasses( { AccountTest.class, ClientTest.class })
public class LongRunningTestSuite {}
您还可以包含(groups
)/ exclude(excludedGroups
)那些使用maven的测试例如:
mvn -DexcludedGroups=com.mycompany.tests.IntegrationTests test
我认为如果使用此命令,这应该工作:
mvn archetype:create -DgroupId=test -DartifactId=test
(用于测试将pom.xml和test-class更改为以下内容并使用mvn install)
pom.hml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>test</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>
test/AppTest.java
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
</dependencies>
测试类:
package test;
import org.junit.Test;
import static org.junit.Assert.fail;
public class AppTest
{
@Test
public void test_it() {
fail("not implemented");
}
}
如果要使用CLI排除单个测试,则应使用-Dtest
和-Dit.test
标志。
小心重置默认值。当您使用!
表示法时,所有默认值都将被删除,您应该将它们放回原处。对于surefire
执行的正常测试,你应该添加*Test, Test*, *Tests, *TestCase
,而对于failsafe
执行的集成测试,你应该添加IT*, *IT, *ITCase
。
你可以在这里找到更多信息https://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html(正常测试)
在这里https://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html(集成测试)
-Dit.test='!ITsometestIT, IT*, *IT, *ITCase'
一个完整的mvn
命令可能是这个:
mvn -e -B -Dtest='!unitTestABC, *Test, Test*, *Tests, *TestCase' -Dit.test='!ITintegrationTestABCIT, IT*, *IT, *ITCase' -DfailIfNoTests=false clean install
记得使用'
而不是"
。当使用双引号时,!
将对其中的任何bash
进行评估。
还要记住,当mvn test
时,不会运行集成测试。使用mvn verify
只会运行集成测试,而不是单元测试