【问题标题】:Maven and JUnit 5: run a single test in a classMaven 和 JUnit 5:在一个类中运行单个测试
【发布时间】:2018-02-05 18:37:47
【问题描述】:

我一直在尝试使用 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

【问题讨论】:

标签: maven junit5


【解决方案1】:

你可以使用这种格式:

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>

【讨论】:

  • 这适用于 JUnit 5 吗?这个问题与 JUnit 4 无关。
  • 是的,但它需要平台 1.0.3 / Jupiter 5.0.3。
  • 就像@MarcPhilipp 所说,这适用于 JUnit 5。
猜你喜欢
  • 1970-01-01
  • 2011-01-30
  • 2020-12-31
  • 2019-04-25
  • 1970-01-01
  • 2019-11-03
  • 2013-01-10
  • 2010-10-13
相关资源
最近更新 更多