Parallel Test Execution
Spring Framework 5.0 在使用 Spring TestContext 框架时引入了对在单个 JVM 中并行执行测试的基本支持。通常,这意味着大多数测试类或测试方法可以在没有任何测试代码或配置更改的情况下并行运行。
Spring Framework 5.0 introduced basic support for executing tests in parallel within a single JVM when using the Spring TestContext Framework. In general, this means that most test classes or test methods can be run in parallel without any changes to test code or configuration.
有关如何设置并行测试执行的详细信息,请参阅你的测试框架、构建工具或 IDE 的文档。 |
For details on how to set up parallel test execution, see the documentation for your testing framework, build tool, or IDE. |
请记住,在你的测试套件中引入并发性可能会导致意外的副作用、奇怪的运行时行为以及中断或看似随机失败的测试。因此,Spring 团队针对何时不并行运行测试提供了以下一般准则。
Keep in mind that the introduction of concurrency into your test suite can result in unexpected side effects, strange runtime behavior, and tests that fail intermittently or seemingly randomly. The Spring Team therefore provides the following general guidelines for when not to run tests in parallel.
如果测试满足以下条件,则不要并行运行测试:
Do not run tests in parallel if the tests:
-
Use Spring Framework’s
@DirtiesContext
support. -
Use Spring Boot’s
@MockBean
or@SpyBean
support. -
Use JUnit 4’s
@FixMethodOrder
support or any testing framework feature that is designed to ensure that test methods run in a particular order. Note, however, that this does not apply if entire test classes are run in parallel. -
Change the state of shared services or systems such as a database, message broker, filesystem, and others. This applies to both embedded and external systems.
如果并行测试执行失败,并出现异常,指出当前测试的 If parallel test execution fails with an exception stating that the 这可能是由于使用 This may be due to the use of |
Spring TestContext 框架中的并行测试执行仅当底层 TestContext
实现提供复制构造函数时才可行,如 TestContext
的 javadoc 中所阐述的那样。Spring 中使用的 DefaultTestContext
提供了这样的构造函数。但是,如果您使用提供自定义 TestContext
实现的第三方库,则需要验证它是否适合并行测试执行。
Parallel test execution in the Spring TestContext Framework is only possible if
the underlying TestContext
implementation provides a copy constructor, as explained in
the javadoc for TestContext
. The
DefaultTestContext
used in Spring provides such a constructor. However, if you use a
third-party library that provides a custom TestContext
implementation, you need to
verify that it is suitable for parallel test execution.