【问题标题】:Tests do not run in the specified order in JUnit5测试在 JUnit5 中不按指定顺序运行
【发布时间】:2019-11-07 15:28:36
【问题描述】:

我是 Kotlin、Java 和 IntelliJ 的新手,过去几个小时以来我一直在努力,我需要一些帮助。

我需要按特定顺序运行测试。我知道不建议这样做,但这些是集成和工作流测试,而不是单元测试。出于某种原因,JUnit 5 完全忽略了 IntelliJ 中的测试顺序,并以随机顺序运行测试。我写了一段简单的代码来检查这一点,行为是完全随机的。

import org.junit.jupiter.api.MethodOrderer.OrderAnnotation
import org.junit.jupiter.api.Order
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestMethodOrder

@TestMethodOrder(OrderAnnotation::class)
class OrderedTests
{

    @Test
    @Order(1)
    fun `A Test`()
    {
        println("First Test")
    }

    @Test
    @Order(2)
    fun `A Second Test`()
    {
        println("Second Test")
    }

    @Test
    @Order(3)
    fun `A FFFF Test`()
    {
        println("Third Test")
    }

    @Test
    @Order(4)
    fun `1 Test`()
    {
        println("Fourth Test")
    }

    @Test
    @Order(5)
    fun `AA Test`()
    {
        println("Fifth Test")
    }

    @Test
    @Order(6)
    fun `MM Test`()
    {
        println("Sixth Test")
    }
}

这是我的 pom.xml 依赖项的样子:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>de.arvatoinfoscore.lab.ad.wf</groupId>
    <artifactId>dummytest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <version.java>11.0.4</version.java>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <junit.jupiter.version>5.5.2</junit.jupiter.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <kotlin.version>1.3.50</kotlin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test-junit5</artifactId>
            <version>${kotlin.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <jvmTarget>1.8</jvmTarget>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我使用 IntelliJ 运行我的测试,运行顺序始终相同:Fifth Test、Second Test、Third Test、Fourth Test、Sixth Test 和 First Test。

任何帮助我指出正确方向的帮助将不胜感激。 谢谢

【问题讨论】:

    标签: java maven intellij-idea kotlin junit5


    【解决方案1】:

    您需要 junit-jupiter-engine 作为依赖项。将此添加到您的 pom 中:

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

    【讨论】:

    • 感谢您的提示。我认为 junit-jupiter-engine 与 IntelliJ 捆绑在一起。但这有助于解决问题。
    • 我有那个依赖 5.8.1 并且不做排序,@TestMethodOrder(MethodOrderer.OrderAnnotation.class) 设置
    【解决方案2】:

    正如你所说的顺序应该无关紧要,看看这个https://github.com/junit-team/junit5/issues/48

    【讨论】:

      猜你喜欢
      • 2013-10-11
      • 1970-01-01
      • 2018-05-29
      • 2010-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-08
      相关资源
      最近更新 更多