Maven 和 JUnit 5 : run a single test in a class

标签 maven junit5

我一直在尝试使用 Maven(版本 3.3.9)和 JUnit 5(不是 4)在一个类中使用以下命令运行单个测试,但没有成功:

mvn -Dtest=EmitRulesTest#cr_filter_contact_points_for_C4C_output test

此命令执行所有测试。

尝试此命令实际上会执行类中的所有测试:

mvn test -Dtest=EmitRulesTest

这是我的 JUnit 5 Maven 配置:

<dependencies>
    ...
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-params</artifactId>
        <version>5.0.0-RC2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        ...    
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <configuration>
                <systemPropertiesFile>${basedir}/src/test/resources/definitions/system.properties</systemPropertiesFile>
                <includes>
                    <include>**/*Test.java</include>
                </includes>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.0.0-RC2</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.0.0-RC2</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

更多引用:Running a Single Test using Maven

最佳答案

您可以使用以下格式:

mvn test -Dtest=TestClass#testMethod

您可以在这里找到更多信息:http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html

这是我的 POM.xml 的摘录

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>${junit.jupiter.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>${junit.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>${junit.jupiter.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>${junit.vintage.version}</version>
    <scope>test</scope>
</dependency>

...

<properties>
    <junit.version>4.12</junit.version>
    <junit.jupiter.version>5.1.0</junit.jupiter.version>
    <junit.vintage.version>5.1.0</junit.vintage.version>
    <junit.platform.version>1.1.0</junit.platform.version>
</properties>

关于Maven 和 JUnit 5 : run a single test in a class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45919894/

相关文章:

Maven 检查存储库中更新的依赖项

maven - 如何将 Maven 项目所需的所有 jar 提取到文件夹中?

java - 具有不同数组参数的构造函数 java

junit5 - PiTest "changed conditional boundary mutation survived"无故?

java - Maven Surefire 与 JUnit 5 隐藏堆栈跟踪

maven - 如何配置 Maven 插件来运行 spock 和 junit5 测试

csv - 如何使用 JUnit 5 的 @CsvFileSource 忽略行

maven - 为什么Gradle不为每个项目创建本地存储库并下载相同的依赖项

java - Eclipse:找不到 javax.xml.datatype.XMLGregorianCalendar - Java 11

java - 多个 Maven 模块来分离测试、API 和实现