When using Test Containers the normal behavior is that it will shutdown the container when the test is finished due to pass or failure.
Is there a way to configure test containers so that if a test fails the database container is kept around to help with debugging?
Moby Ryuk. This project helps you to remove containers/networks/volumes/images by given filter after specified delay.
Disabling Ryuk Ryuk must be started as a privileged container. If your environment already implements automatic cleanup of containers after the execution, but does not allow starting privileged containers, you can turn off the Ryuk container by setting TESTCONTAINERS_RYUK_DISABLED environment variable to true .
Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
Yes, you can use the reuse feature (in alpha state) of Testcontainers to not shutdown the container after the test.
For this to work you need Testcontainers >= 1.12.3 and opt-in with a properties file ~/.testcontainers.properties
testcontainers.reuse.enable=true
Next, declare your container to be reused:
static PostgreSQLContainer postgreSQLContainer = (PostgreSQLContainer) new PostgreSQLContainer()
.withDatabaseName("test")
.withUsername("duke")
.withPassword("s3cret")
.withReuse(true);
and make sure to not use a JUnit 4 or JUnit 5 annotation to manage the lifecycle of your container. Rather use the singleton containers or start them inside @BeforeEach
for yourself:
static final PostgreSQLContainer postgreSQLContainer;
static {
postgreSQLContainer = (PostgreSQLContainer) new PostgreSQLContainer()
.withDatabaseName("test")
.withUsername("duke")
.withPassword("s3cret")
.withReuse(true);
postgreSQLContainer.start();
}
This feature is rather intended to speed up subsequent tests as the containers will be still up- and running but I guess this also fits your use case.
You can find a detailed guide here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With