-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Closed
Description
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:
- requires a certain test class to be extended
- 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:
- GenericContainer foo is started
- TestA beforeClass (no-op in this case)
- testA runs
- TestB beforeClass (no-op in this case)
- testB runs
sdaschner, deen13, dbelyaev, solomkinmv, sarod and 1 more