Spring 测试仅在测试套件中的所有测试都完成后才关闭上下文 运行
Spring test closes contexts only after all tests in test suite are run
假设我们有如下编写的测试(在单独的文件中):
@SpringBootTest(classes = {SomeBeansProvider.class, FunnyCatBeansProvider.class})
public class CatsTest extends AbstractTestNGSpringContextTests {(...)}
@SpringBootTest(classes = {SomeBeansProvider.class, FunnyCatBeansProvider.class})
public class CatTest2 extends AbstractTestNGSpringContextTests {(...)}
@SpringBootTest(classes = {SomeBeansProvider.class, DogBeansProvider.class})
public class DogsTest extends AbstractTestNGSpringContextTests {(...)}
我不想让事情复杂化,所以让我们现在假装所有这些测试都打印出类似 "Test #no execution completed.".
的内容
也让我们在SomeBeansProvider.class
中定义这样的方法
@EventListener(ContextClosedEvent.class)
public void onContextClose(final ContextClosedEvent contextClosedEvent) {
log.info("Context closed.");
}
如果您执行这些测试(使用 TestNG 运行程序),您将看到类似这样的控制台输出:
- 测试 1 执行完成。
- 测试 2 执行完成。
- 测试 3 执行完成。
- 上下文已关闭。
- 上下文已关闭。
由于 CatTest 和 CatTest2 具有相同的上下文,因此只会为它们创建一个上下文,并为 DogTest 创建一个额外的上下文。
问题是我希望上下文在不再需要时立即关闭。我想知道是否有可能在执行期间以某种方式自动对测试进行分组,以便同时没有多个上下文实例。所以我希望输出看起来类似于:
- 测试 1 执行完成。
- 测试 2 执行完成。
- 上下文已关闭。
- 测试 3 执行完成。
- 上下文已关闭。
这可能吗?
我们可以在测试用例中使用下面的 Spring 注释,使其在 class 级别或方法级别上下文关闭。
@DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD)
@DirtiesContext(classMode=ClassMode.AFTER_CLASS)
For your scenario you can do like this, You can use
@FixMethodOrder(MethodSorters.NAME_ASCENDING) at class level,and
annotate @DirtiesContext(methodMode=ClassMode.AFTER_EACH_TEST_METHOD)
at each test case wherever you want to close the context. and keep the
all the related test cases either first or last in class according to
the Ascending order convention so that they will run consecutively so
that they will be using the same context(without @DirtiesContext).
假设我们有如下编写的测试(在单独的文件中):
@SpringBootTest(classes = {SomeBeansProvider.class, FunnyCatBeansProvider.class})
public class CatsTest extends AbstractTestNGSpringContextTests {(...)}
@SpringBootTest(classes = {SomeBeansProvider.class, FunnyCatBeansProvider.class})
public class CatTest2 extends AbstractTestNGSpringContextTests {(...)}
@SpringBootTest(classes = {SomeBeansProvider.class, DogBeansProvider.class})
public class DogsTest extends AbstractTestNGSpringContextTests {(...)}
我不想让事情复杂化,所以让我们现在假装所有这些测试都打印出类似 "Test #no execution completed.".
的内容也让我们在SomeBeansProvider.class
中定义这样的方法@EventListener(ContextClosedEvent.class)
public void onContextClose(final ContextClosedEvent contextClosedEvent) {
log.info("Context closed.");
}
如果您执行这些测试(使用 TestNG 运行程序),您将看到类似这样的控制台输出:
- 测试 1 执行完成。
- 测试 2 执行完成。
- 测试 3 执行完成。
- 上下文已关闭。
- 上下文已关闭。
由于 CatTest 和 CatTest2 具有相同的上下文,因此只会为它们创建一个上下文,并为 DogTest 创建一个额外的上下文。
问题是我希望上下文在不再需要时立即关闭。我想知道是否有可能在执行期间以某种方式自动对测试进行分组,以便同时没有多个上下文实例。所以我希望输出看起来类似于:
- 测试 1 执行完成。
- 测试 2 执行完成。
- 上下文已关闭。
- 测试 3 执行完成。
- 上下文已关闭。
这可能吗?
我们可以在测试用例中使用下面的 Spring 注释,使其在 class 级别或方法级别上下文关闭。
@DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD)
@DirtiesContext(classMode=ClassMode.AFTER_CLASS)
For your scenario you can do like this, You can use @FixMethodOrder(MethodSorters.NAME_ASCENDING) at class level,and annotate @DirtiesContext(methodMode=ClassMode.AFTER_EACH_TEST_METHOD) at each test case wherever you want to close the context. and keep the all the related test cases either first or last in class according to the Ascending order convention so that they will run consecutively so that they will be using the same context(without @DirtiesContext).