集成测试的执行顺序在 Srping Boot (

问题描述

我使用 Java 11(我不是在寻找类中方法的执行顺序。

网站上没有回答我的问题的帖子


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
@SpringBoottest(webEnvironment = SpringBoottest.WebEnvironment.RANDOM_PORT)
@Suite.SuiteClasses({TestFirst.class,TestSecond.class,AdminTest.class})
public class ApplicationTests {

}

public class TestFirst extends ApplicationTests {

@Test
void run(){
}
}

public class TestSecond extends ApplicationTests {

@Test
void run(){
}

}

public class AdminTest extends ApplicationTests {

@Test
void run(){
}

}

我使用注释 @Suite.SuiteClasses

我假设测试类应该一一运行。不遵循发射顺序。每个类都位于自己的目录中。

对于 Spring,它就像一个单独的集成测试。

如何让类按照我定义的顺序执行?

也许还有另一种方法

解决方法

故意未定义测试的运行顺序。每个测试都应该设置它需要的资源并在之后进行清理。单个@Tests 的运行顺序应该无关紧要。

您可以使用@Before 和@After 注释为每个测试进行一些设置和清理。还有@BeforeAll 和@AfterAll 用于在类级别设置/清理(它们在类中的所有测试之前/之后执行)。

原则是单元测试是完全独立的,可以独立于任何其他测试运行。您应该能够选择一个测试并运行它。