Providers API Reference¶
django_testcontainers_plus.providers.base.ContainerProvider
¶
Bases: ABC
Base class for all container providers.
Each provider is responsible for: 1. Detecting if the service is needed from Django settings 2. Creating and configuring the container 3. Providing settings updates with container connection info
Source code in src/django_testcontainers_plus/providers/base.py
name
abstractmethod
property
¶
Unique identifier for this provider.
can_auto_detect(settings, context=None)
abstractmethod
¶
Check if this service is needed based on Django settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
Any
|
Django settings module |
required |
context
|
dict[str, Any] | None
|
Optional dict with pre-test-setup values that Django's test framework may have overwritten. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if this service should be automatically started |
Source code in src/django_testcontainers_plus/providers/base.py
get_container(config)
abstractmethod
¶
Create and configure the container.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict[str, Any]
|
Configuration dict from TESTCONTAINERS setting |
required |
Returns:
| Type | Description |
|---|---|
DockerContainer
|
Configured testcontainer instance |
Source code in src/django_testcontainers_plus/providers/base.py
update_settings(container, settings, config)
abstractmethod
¶
Generate settings updates with container connection info.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
container
|
DockerContainer
|
Running container instance |
required |
settings
|
Any
|
Django settings module |
required |
config
|
dict[str, Any]
|
Configuration dict from TESTCONTAINERS setting |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict of settings updates to apply |
Source code in src/django_testcontainers_plus/providers/base.py
get_default_config()
¶
Get default configuration for this provider.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Default configuration dict |
django_testcontainers_plus.manager.ContainerManager
¶
Manages lifecycle of test containers.
Source code in src/django_testcontainers_plus/manager.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
__init__(settings, context=None)
¶
Initialize container manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
Any
|
Django settings module |
required |
context
|
dict[str, Any] | None
|
Optional dict with pre-test-setup values that Django's test framework may have overwritten. Passed to providers during auto-detection. |
None
|
Source code in src/django_testcontainers_plus/manager.py
get_testcontainers_config()
¶
Get TESTCONTAINERS configuration from settings.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Configuration dict, empty if not defined |
detect_needed_containers()
¶
Detect which containers are needed based on settings.
Returns:
| Type | Description |
|---|---|
list[ContainerProvider]
|
List of providers that should be started |
Raises:
| Type | Description |
|---|---|
MissingDependencyError
|
If a needed provider is unavailable |
Source code in src/django_testcontainers_plus/manager.py
start_containers()
¶
Start all needed containers.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict of settings updates to apply |
Source code in src/django_testcontainers_plus/manager.py
stop_containers()
¶
Stop and remove all active containers.
django_testcontainers_plus.runner.TestcontainersRunner
¶
Bases: DiscoverRunner
Django test runner that automatically manages testcontainers.
This runner extends Django's DiscoverRunner to automatically: 1. Detect needed containers from settings 2. Start containers before test databases are set up 3. Update database settings with container connection info 4. Clean up containers after tests complete
Usage
settings.py¶
TEST_RUNNER = 'django_testcontainers_plus.runner.TestcontainersRunner'
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'test', } }
Source code in src/django_testcontainers_plus/runner.py
__init__(*args, **kwargs)
¶
Initialize the test runner.
setup_test_environment(**kwargs)
¶
Set up test environment and start containers.
Source code in src/django_testcontainers_plus/runner.py
teardown_test_environment(**kwargs)
¶
Tear down test environment and stop containers.
Source code in src/django_testcontainers_plus/runner.py
django_testcontainers_plus.exceptions.MissingDependencyError
¶
Bases: DjangoTestcontainersError
Source code in src/django_testcontainers_plus/exceptions.py
__init__(provider_name, extra_name, detected_in=None, original_error=None)
¶
Initialize the error with helpful context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider_name
|
str
|
Name of the provider (e.g., "MySQL", "Redis") |
required |
extra_name
|
str
|
Name of the pip extra to install (e.g., "mysql", "redis") |
required |
detected_in
|
str | None
|
Where the need was detected (e.g., "DATABASES['default']") |
None
|
original_error
|
Exception | None
|
The original ImportError that triggered this |
None
|