75

我想在启动时只跳过一个测试mvn install

有没有办法做到这一点 ?

4

6 回答 6

68

-Dtest您可以通过在选项前面加上!(感叹号)来指定排除模式。例如,

mvn -Dtest=\!FlakyTest* install

在这里找到它并验证它可以正常工作。例如,我可以通过使用以下方法跳过这个易碎的 Jenkins 测试:

mvn -Dtest=\!CronTabTest* package
于 2016-06-13T15:55:42.843 回答
22

使用 junit 4,@Ignore当我想这样做时,我会添加一个注释。这对你有用,除非你只想有时忽略测试,或者只在构建从 maven 运行时忽略它。如果是这种情况,那么我会问“为什么?”

测试应该是一致的,它们应该是可移植的,并且应该始终通过。如果一个特定的测试有问题,我会考虑重写它,完全删除它,或者将它移到不同的测试套件或项目中。

于 2009-05-07T16:49:24.783 回答
17

需要排除集成测试但需要包含单元测试是正常的。为了实现这一点,我建议使用后缀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 要排除一个测试,您只需在排除列表中明确命名它。简单的。

于 2013-09-26T09:25:21.250 回答
11

如果您想使用 CLI 排除一个测试,您应该使用-Dtestand-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仅运行集成测试而不运行单元测试

于 2018-10-26T12:18:11.443 回答
8

看看这个解决方案,使用@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 {}

您还可以使用 maven包含 ( groups) / exclude ( ) 那些测试,例如:excludedGroups

mvn -DexcludedGroups=com.mycompany.tests.IntegrationTests test
于 2011-12-09T07:31:49.637 回答
4

我认为如果使用此命令,这应该可以工作:

mvn archetype:create -DgroupId=test -DartifactId=test

(用于测试将 pom.xml 和 test-class 更改为以下并使用 mvn install)

pom.xml

<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");
    }
}
于 2009-05-19T07:23:06.363 回答