ams*_*ams 5 java testing junit integration-testing testcontainers
使用测试容器时,正常行为是在测试完成时由于通过或失败而关闭容器。
有没有办法配置测试容器,以便在测试失败时保留数据库容器以帮助调试?
rie*_*pil 20
是的,您可以使用 Testcontainers 的重用功能(处于 alpha 状态)来在测试后不关闭容器。
为此,您需要 Testcontainers >= 1.12.3 并使用属性文件选择加入~/.testcontainers.properties
testcontainers.reuse.enable=true
Run Code Online (Sandbox Code Playgroud)
接下来,声明要重用的容器:
static PostgreSQLContainer postgreSQLContainer = (PostgreSQLContainer) new PostgreSQLContainer()
.withDatabaseName("test")
.withUsername("duke")
.withPassword("s3cret")
.withReuse(true);
Run Code Online (Sandbox Code Playgroud)
并确保不使用 JUnit 4 或 JUnit 5 注释来管理容器的生命周期。而是使用单例容器或@BeforeEach自己在内部启动它们:
静态最终 PostgreSQLContainer postgreSQLContainer;
static {
postgreSQLContainer = (PostgreSQLContainer) new PostgreSQLContainer()
.withDatabaseName("test")
.withUsername("duke")
.withPassword("s3cret")
.withReuse(true);
postgreSQLContainer.start();
}
Run Code Online (Sandbox Code Playgroud)
此功能旨在加速后续测试,因为容器仍将启动并运行,但我想这也适合您的用例。
您可以在此处找到详细指南。
| 归档时间: |
|
| 查看次数: |
846 次 |
| 最近记录: |