Skip to content

Better mechanism to define singleton containers #1441

@aguibert

Description

@aguibert

Currently singleton containers are kind of hacky, requiring manual container start in a static init block of an extended class.

Singleton containers are useful, because it allows container startup cost to be amortized across multiple test classes for a faster overall runtime.

The current approach has 2 primary limitations:

  1. requires a certain test class to be extended
  2. requires container lifecycle to be manually managed by the user

Proposed solution

Add a new SharedContainerConfiguration marker interface, with a complementary @SharedContainerConfig(Class<? extends SharedContainerConfiguration>) annotation which may be used on a test class. Shared container configuration would be processed in org.testcontainers.junit.jupiter.TestcontainersExtension.beforeAll(ExtensionContext), and would start any containers that are not already started.

Example usage:

public class MySharedContainerConfig implements SharedContainerConfiguration {
    @Container
    public static GenericContainer<?> foo = new GenericContainer<>();
}
@Testcontainers
@SharedContainerConfig(AppContainerConfig.class)
public class MyTestA {

   @Test 
   public void testA() { ... }
}

@Testcontainers
@SharedContainerConfig(AppContainerConfig.class)
public class MyTestB {

   @Test 
   public void testB() { ... }
}

The call flow would be as follows:

  1. GenericContainer foo is started
  2. TestA beforeClass (no-op in this case)
  3. testA runs
  4. TestB beforeClass (no-op in this case)
  5. testB runs

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions