简体   繁体   English

测试在 JUnit5 中不按指定顺序运行

[英]Tests do not run in the specified order in JUnit5

I am new to Kotlin, Java and IntelliJ and I have been pulling my hair since the past few hours and i need some help.我是 Kotlin、Java 和 IntelliJ 的新手,过去几个小时以来我一直在拉头发,我需要一些帮助。

I need my tests to run in a specific order.我需要我的测试以特定的顺序运行。 I know that it is not recommended, but these are integration and workflow tests and not unit tests.我知道不建议这样做,但这些是集成和工作流测试,而不是单元测试。 For some reason, JUnit 5 totally ignored the test order in IntelliJ and runs the tests in a random order.由于某种原因,JUnit 5 完全忽略了 IntelliJ 中的测试顺序,并以随机顺序运行测试。 I wrote a simple piece of code to check this and the behaviour is totally random.我写了一段简单的代码来检查这一点,行为是完全随机的。

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")
    }
}

Here is how my pom.xml dependencies look like:这是我的 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>

I am using IntelliJ to run my tests and the run order is always the same: Fifth Test, Second Test, Third Test, Fourth Test, Sixth Test and First Test.我正在使用 IntelliJ 运行我的测试,运行顺序始终相同:第五次测试、第二次测试、第三次测试、第四次测试、第六次测试和第一次测试。

Any help to point me in the right direction will be greatly appreciated.任何帮助我指出正确方向的帮助将不胜感激。 Thanks谢谢

You need junit-jupiter-engine as a dependency.您需要junit-jupiter-engine作为依赖项。 Add this to your pom:将此添加到您的 pom 中:

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

As you said order should be irrelevant, take a look a this https://github.com/junit-team/junit5/issues/48正如你所说的顺序应该无关紧要,看看这个https://github.com/junit-team/junit5/issues/48

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM