Documentation
¶
Overview ¶
Package testctr provides utilities for setting up testcontainers in tests.
Usage:
1. Add a TestMain function to your test package:
func TestMain(m *testing.M) {
code := testctr.EnsureTestEnv()
if code == 0 {
code = m.Run()
}
os.Exit(code)
}
2. In your test functions, check for Docker availability:
func TestWithContainers(t *testing.T) {
testctr.SkipIfDockerNotAvailable(t)
// Your test code here
}
This approach ensures proper environment setup for testcontainers while maintaining compatibility with parallel tests.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EnsureTestEnv ¶
func EnsureTestEnv() int
EnsureTestEnv sets up the necessary environment variables for testcontainers at the process level. This should be called from TestMain before running tests. It returns an exit code that should be passed to os.Exit.
This works around a testcontainers bug where it doesn't properly detect the Docker socket when using Colima or other non-standard Docker setups.
Example usage:
func TestMain(m *testing.M) {
code := testctr.EnsureTestEnv()
if code == 0 {
code = m.Run()
}
os.Exit(code)
}
func SkipIfDockerNotAvailable ¶
SkipIfDockerNotAvailable checks if Docker is available and skips the test if not. This is safe to call from tests that use t.Parallel() as it doesn't use t.Setenv. You must ensure the environment variables are set before running the test (e.g., via EnsureTestEnv in TestMain).
Types ¶
This section is empty.