Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure testcontainers to leave database container running if a test fails?

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?

like image 402
ams Avatar asked Jul 04 '20 05:07

ams


People also ask

What is RYUK in Testcontainers?

Moby Ryuk. This project helps you to remove containers/networks/volumes/images by given filter after specified delay.

How do I disable RYUK?

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 .

What is test container in spring boot?

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.


1 Answers

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.

like image 169
rieckpil Avatar answered Sep 24 '22 02:09

rieckpil