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.

如果并行测试执行失败,并出现异常,指出当前测试的 ApplicationContext 已不再活动,这通常意味着 ApplicationContext 已在另一个线程中从 ContextCache 中删除。

If parallel test execution fails with an exception stating that the ApplicationContext for the current test is no longer active, this typically means that the ApplicationContext was removed from the ContextCache in a different thread.

这可能是由于使用 @DirtiesContextContextCache 中的自动驱逐。如果 @DirtiesContext 是罪魁祸首,则您需要找到一种方法来避免使用 @DirtiesContext 或将此类测试排除在并行执行之外。如果 ContextCache 的最大大小已被超出,您可以增加缓存的最大大小。请参阅有关 context caching 的讨论以了解详情。

This may be due to the use of @DirtiesContext or due to automatic eviction from the ContextCache. If @DirtiesContext is the culprit, you either need to find a way to avoid using @DirtiesContext or exclude such tests from parallel execution. If the maximum size of the ContextCache has been exceeded, you can increase the maximum size of the cache. See the discussion on context caching for details.

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.