测试未按JUnit5中的指定顺序运行

vivek86:

我是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运行测试,运行顺序始终相同:第五测试,第二测试,第三测试,第四测试,第六测试和第一测试。

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

迭戈·马林(Diego Marin):

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章