pytest-docker 2.0.1__tar.gz → 2.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pytest-docker-2.0.1/src/pytest_docker.egg-info → pytest-docker-2.2.0}/PKG-INFO +34 -11
- {pytest-docker-2.0.1 → pytest-docker-2.2.0}/README.md +26 -9
- {pytest-docker-2.0.1 → pytest-docker-2.2.0}/setup.cfg +1 -1
- pytest-docker-2.2.0/src/pytest_docker/__init__.py +33 -0
- {pytest-docker-2.0.1 → pytest-docker-2.2.0}/src/pytest_docker/plugin.py +26 -13
- {pytest-docker-2.0.1 → pytest-docker-2.2.0/src/pytest_docker.egg-info}/PKG-INFO +34 -11
- {pytest-docker-2.0.1 → pytest-docker-2.2.0}/tests/test_docker_services.py +118 -0
- pytest-docker-2.2.0/tests/test_fixtures.py +64 -0
- pytest-docker-2.0.1/src/pytest_docker/__init__.py +0 -20
- pytest-docker-2.0.1/tests/test_fixtures.py +0 -22
- {pytest-docker-2.0.1 → pytest-docker-2.2.0}/LICENSE.txt +0 -0
- {pytest-docker-2.0.1 → pytest-docker-2.2.0}/pyproject.toml +0 -0
- {pytest-docker-2.0.1 → pytest-docker-2.2.0}/setup.py +0 -0
- {pytest-docker-2.0.1 → pytest-docker-2.2.0}/src/pytest_docker.egg-info/SOURCES.txt +0 -0
- {pytest-docker-2.0.1 → pytest-docker-2.2.0}/src/pytest_docker.egg-info/dependency_links.txt +0 -0
- {pytest-docker-2.0.1 → pytest-docker-2.2.0}/src/pytest_docker.egg-info/entry_points.txt +0 -0
- {pytest-docker-2.0.1 → pytest-docker-2.2.0}/src/pytest_docker.egg-info/requires.txt +0 -0
- {pytest-docker-2.0.1 → pytest-docker-2.2.0}/src/pytest_docker.egg-info/top_level.txt +0 -0
- {pytest-docker-2.0.1 → pytest-docker-2.2.0}/tests/test_docker_ip.py +0 -0
- {pytest-docker-2.0.1 → pytest-docker-2.2.0}/tests/test_dockercomposeexecutor.py +0 -0
- {pytest-docker-2.0.1 → pytest-docker-2.2.0}/tests/test_integration.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: pytest-docker
|
|
3
|
-
Version: 2.0
|
|
3
|
+
Version: 2.2.0
|
|
4
4
|
Summary: Simple pytest fixtures for Docker and Docker Compose based tests
|
|
5
5
|
Home-page: https://github.com/avast/pytest-docker
|
|
6
6
|
Author: Max K., Andre Caron
|
|
@@ -21,19 +21,26 @@ Classifier: Operating System :: POSIX
|
|
|
21
21
|
Classifier: Operating System :: Microsoft :: Windows
|
|
22
22
|
Requires-Python: >=3.6
|
|
23
23
|
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE.txt
|
|
25
|
+
Requires-Dist: pytest<8.0,>=4.0
|
|
26
|
+
Requires-Dist: attrs>=19.2.0
|
|
24
27
|
Provides-Extra: docker-compose-v1
|
|
28
|
+
Requires-Dist: docker-compose<2.0,>=1.27.3; extra == "docker-compose-v1"
|
|
25
29
|
Provides-Extra: tests
|
|
26
|
-
|
|
30
|
+
Requires-Dist: requests<3.0,>=2.22.0; extra == "tests"
|
|
31
|
+
Requires-Dist: pytest-pylint<1.0,>=0.14.1; extra == "tests"
|
|
32
|
+
Requires-Dist: pytest-pycodestyle<3.0,>=2.0.0; extra == "tests"
|
|
27
33
|
|
|
28
34
|
Docker-based integration tests
|
|
29
35
|
=====
|
|
36
|
+
|
|
30
37
|
[](https://pypi.org/project/pytest-docker/)
|
|
31
38
|
[](https://github.com/avast/pytest-docker/actions/workflows/tests.yaml)
|
|
32
39
|
[](https://pypi.org/project/pytest-docker/)
|
|
33
40
|
[](https://github.com/psf/black)
|
|
34
41
|
|
|
35
|
-
|
|
36
42
|
# Description
|
|
43
|
+
|
|
37
44
|
Simple [pytest](http://doc.pytest.org/) fixtures that help you write integration
|
|
38
45
|
tests with Docker and [Docker Compose](https://docs.docker.com/compose/).
|
|
39
46
|
Specify all necessary containers in a `docker-compose.yml` file and and
|
|
@@ -45,6 +52,7 @@ This package is tested with Python versions `3.6`, `3.7`, `3.8` and
|
|
|
45
52
|
`pytest-docker` was originally created by André Caron.
|
|
46
53
|
|
|
47
54
|
# Installation
|
|
55
|
+
|
|
48
56
|
Install `pytest-docker` with `pip` or add it to your test requirements.
|
|
49
57
|
|
|
50
58
|
By default, it uses the `docker compose` command, so it relies on the Compose plugin for Docker (also called Docker Compose V2).
|
|
@@ -61,6 +69,7 @@ def docker_compose_command() -> str:
|
|
|
61
69
|
```
|
|
62
70
|
|
|
63
71
|
If you want to use the pip-distributed version of `docker-compose` command, you can install it using
|
|
72
|
+
|
|
64
73
|
```
|
|
65
74
|
pip install pytest-docker[docker-compose-v1]
|
|
66
75
|
```
|
|
@@ -68,6 +77,7 @@ pip install pytest-docker[docker-compose-v1]
|
|
|
68
77
|
Another option could be usage of [`compose-switch`](https://github.com/docker/compose-switch).
|
|
69
78
|
|
|
70
79
|
# Usage
|
|
80
|
+
|
|
71
81
|
Here is an example of a test that depends on a HTTP service.
|
|
72
82
|
|
|
73
83
|
With a `docker-compose.yml` file like this (using the
|
|
@@ -135,7 +145,16 @@ def docker_compose_file(pytestconfig):
|
|
|
135
145
|
```
|
|
136
146
|
|
|
137
147
|
## Available fixtures
|
|
138
|
-
|
|
148
|
+
|
|
149
|
+
By default the scope of the fixtures are `session` but can be changed with
|
|
150
|
+
`pytest` command line option `--container-scope <scope>`:
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
pytest --container-scope <scope> <test_directory>
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
For available scopes and descriptions
|
|
157
|
+
see <https://docs.pytest.org/en/6.2.x/fixture.html#fixture-scopes>
|
|
139
158
|
|
|
140
159
|
### `docker_ip`
|
|
141
160
|
|
|
@@ -164,29 +183,33 @@ Docker Compose V1, change this fixture to return `docker-compose`.
|
|
|
164
183
|
|
|
165
184
|
### `docker_setup`
|
|
166
185
|
|
|
167
|
-
Get the docker_compose
|
|
186
|
+
Get the list of docker_compose commands to be executed for test spawn actions.
|
|
168
187
|
Override this fixture in your tests if you need to change spawn actions.
|
|
169
188
|
Returning anything that would evaluate to False will skip this command.
|
|
170
189
|
|
|
171
190
|
### `docker_cleanup`
|
|
172
191
|
|
|
173
|
-
Get the docker_compose
|
|
192
|
+
Get the list of docker_compose commands to be executed for test clean-up actions.
|
|
174
193
|
Override this fixture in your tests if you need to change clean-up actions.
|
|
175
194
|
Returning anything that would evaluate to False will skip this command.
|
|
176
195
|
|
|
177
|
-
|
|
178
196
|
# Development
|
|
197
|
+
|
|
179
198
|
Use of a virtual environment is recommended. See the
|
|
180
199
|
[venv](https://docs.python.org/3/library/venv.html) package for more
|
|
181
200
|
information.
|
|
182
201
|
|
|
183
202
|
First, install `pytest-docker` and its test dependencies:
|
|
184
203
|
|
|
185
|
-
|
|
204
|
+
```bash
|
|
205
|
+
pip install -e ".[tests]"
|
|
206
|
+
```
|
|
186
207
|
|
|
187
208
|
Run tests with
|
|
188
209
|
|
|
189
|
-
|
|
210
|
+
```bash
|
|
211
|
+
pytest -c setup.cfg
|
|
212
|
+
```
|
|
190
213
|
|
|
191
214
|
to make sure that the correct configuration is used. This is also how tests are
|
|
192
215
|
run in CI.
|
|
@@ -195,9 +218,9 @@ Use [black](https://pypi.org/project/black/) with default settings for
|
|
|
195
218
|
formatting. You can also use `pylint` with `setup.cfg` as the configuration
|
|
196
219
|
file.
|
|
197
220
|
|
|
198
|
-
|
|
199
221
|
# Contributing
|
|
200
|
-
|
|
222
|
+
|
|
223
|
+
This `pytest` plug-in and its source code are made available to you under a MIT
|
|
201
224
|
license. It is safe to use in commercial and closed-source applications. Read
|
|
202
225
|
the license for details!
|
|
203
226
|
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
Docker-based integration tests
|
|
2
2
|
=====
|
|
3
|
+
|
|
3
4
|
[](https://pypi.org/project/pytest-docker/)
|
|
4
5
|
[](https://github.com/avast/pytest-docker/actions/workflows/tests.yaml)
|
|
5
6
|
[](https://pypi.org/project/pytest-docker/)
|
|
6
7
|
[](https://github.com/psf/black)
|
|
7
8
|
|
|
8
|
-
|
|
9
9
|
# Description
|
|
10
|
+
|
|
10
11
|
Simple [pytest](http://doc.pytest.org/) fixtures that help you write integration
|
|
11
12
|
tests with Docker and [Docker Compose](https://docs.docker.com/compose/).
|
|
12
13
|
Specify all necessary containers in a `docker-compose.yml` file and and
|
|
@@ -18,6 +19,7 @@ This package is tested with Python versions `3.6`, `3.7`, `3.8` and
|
|
|
18
19
|
`pytest-docker` was originally created by André Caron.
|
|
19
20
|
|
|
20
21
|
# Installation
|
|
22
|
+
|
|
21
23
|
Install `pytest-docker` with `pip` or add it to your test requirements.
|
|
22
24
|
|
|
23
25
|
By default, it uses the `docker compose` command, so it relies on the Compose plugin for Docker (also called Docker Compose V2).
|
|
@@ -34,6 +36,7 @@ def docker_compose_command() -> str:
|
|
|
34
36
|
```
|
|
35
37
|
|
|
36
38
|
If you want to use the pip-distributed version of `docker-compose` command, you can install it using
|
|
39
|
+
|
|
37
40
|
```
|
|
38
41
|
pip install pytest-docker[docker-compose-v1]
|
|
39
42
|
```
|
|
@@ -41,6 +44,7 @@ pip install pytest-docker[docker-compose-v1]
|
|
|
41
44
|
Another option could be usage of [`compose-switch`](https://github.com/docker/compose-switch).
|
|
42
45
|
|
|
43
46
|
# Usage
|
|
47
|
+
|
|
44
48
|
Here is an example of a test that depends on a HTTP service.
|
|
45
49
|
|
|
46
50
|
With a `docker-compose.yml` file like this (using the
|
|
@@ -108,7 +112,16 @@ def docker_compose_file(pytestconfig):
|
|
|
108
112
|
```
|
|
109
113
|
|
|
110
114
|
## Available fixtures
|
|
111
|
-
|
|
115
|
+
|
|
116
|
+
By default the scope of the fixtures are `session` but can be changed with
|
|
117
|
+
`pytest` command line option `--container-scope <scope>`:
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
pytest --container-scope <scope> <test_directory>
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
For available scopes and descriptions
|
|
124
|
+
see <https://docs.pytest.org/en/6.2.x/fixture.html#fixture-scopes>
|
|
112
125
|
|
|
113
126
|
### `docker_ip`
|
|
114
127
|
|
|
@@ -137,29 +150,33 @@ Docker Compose V1, change this fixture to return `docker-compose`.
|
|
|
137
150
|
|
|
138
151
|
### `docker_setup`
|
|
139
152
|
|
|
140
|
-
Get the docker_compose
|
|
153
|
+
Get the list of docker_compose commands to be executed for test spawn actions.
|
|
141
154
|
Override this fixture in your tests if you need to change spawn actions.
|
|
142
155
|
Returning anything that would evaluate to False will skip this command.
|
|
143
156
|
|
|
144
157
|
### `docker_cleanup`
|
|
145
158
|
|
|
146
|
-
Get the docker_compose
|
|
159
|
+
Get the list of docker_compose commands to be executed for test clean-up actions.
|
|
147
160
|
Override this fixture in your tests if you need to change clean-up actions.
|
|
148
161
|
Returning anything that would evaluate to False will skip this command.
|
|
149
162
|
|
|
150
|
-
|
|
151
163
|
# Development
|
|
164
|
+
|
|
152
165
|
Use of a virtual environment is recommended. See the
|
|
153
166
|
[venv](https://docs.python.org/3/library/venv.html) package for more
|
|
154
167
|
information.
|
|
155
168
|
|
|
156
169
|
First, install `pytest-docker` and its test dependencies:
|
|
157
170
|
|
|
158
|
-
|
|
171
|
+
```bash
|
|
172
|
+
pip install -e ".[tests]"
|
|
173
|
+
```
|
|
159
174
|
|
|
160
175
|
Run tests with
|
|
161
176
|
|
|
162
|
-
|
|
177
|
+
```bash
|
|
178
|
+
pytest -c setup.cfg
|
|
179
|
+
```
|
|
163
180
|
|
|
164
181
|
to make sure that the correct configuration is used. This is also how tests are
|
|
165
182
|
run in CI.
|
|
@@ -168,9 +185,9 @@ Use [black](https://pypi.org/project/black/) with default settings for
|
|
|
168
185
|
formatting. You can also use `pylint` with `setup.cfg` as the configuration
|
|
169
186
|
file.
|
|
170
187
|
|
|
171
|
-
|
|
172
188
|
# Contributing
|
|
173
|
-
|
|
189
|
+
|
|
190
|
+
This `pytest` plug-in and its source code are made available to you under a MIT
|
|
174
191
|
license. It is safe to use in commercial and closed-source applications. Read
|
|
175
192
|
the license for details!
|
|
176
193
|
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
from .plugin import (
|
|
3
|
+
docker_compose_command,
|
|
4
|
+
docker_compose_file,
|
|
5
|
+
docker_compose_project_name,
|
|
6
|
+
docker_ip,
|
|
7
|
+
docker_setup,
|
|
8
|
+
docker_cleanup,
|
|
9
|
+
docker_services,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"docker_compose_command",
|
|
14
|
+
"docker_compose_file",
|
|
15
|
+
"docker_compose_project_name",
|
|
16
|
+
"docker_ip",
|
|
17
|
+
"docker_setup",
|
|
18
|
+
"docker_cleanup",
|
|
19
|
+
"docker_services",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def pytest_addoption(parser: pytest.Parser) -> None:
|
|
24
|
+
group = parser.getgroup("docker")
|
|
25
|
+
group.addoption(
|
|
26
|
+
"--container-scope",
|
|
27
|
+
type=str,
|
|
28
|
+
action="store",
|
|
29
|
+
default="session",
|
|
30
|
+
help="The pytest fixture scope for reusing containers between tests."
|
|
31
|
+
" For available scopes and descriptions, "
|
|
32
|
+
" see https://docs.pytest.org/en/6.2.x/fixture.html#fixture-scopes",
|
|
33
|
+
)
|
|
@@ -10,6 +10,13 @@ import attr
|
|
|
10
10
|
import pytest
|
|
11
11
|
|
|
12
12
|
|
|
13
|
+
@pytest.fixture
|
|
14
|
+
def container_scope_fixture(request):
|
|
15
|
+
return request.config.getoption("--container-scope")
|
|
16
|
+
|
|
17
|
+
def containers_scope(fixture_name, config):
|
|
18
|
+
return config.getoption("--container-scope", "session")
|
|
19
|
+
|
|
13
20
|
def execute(command, success_codes=(0,)):
|
|
14
21
|
"""Run a shell command."""
|
|
15
22
|
try:
|
|
@@ -42,7 +49,7 @@ def get_docker_ip():
|
|
|
42
49
|
return match.group(1)
|
|
43
50
|
|
|
44
51
|
|
|
45
|
-
@pytest.fixture(scope=
|
|
52
|
+
@pytest.fixture(scope=containers_scope)
|
|
46
53
|
def docker_ip():
|
|
47
54
|
"""Determine the IP address for TCP connections to Docker containers."""
|
|
48
55
|
|
|
@@ -129,7 +136,7 @@ class DockerComposeExecutor:
|
|
|
129
136
|
return execute(command)
|
|
130
137
|
|
|
131
138
|
|
|
132
|
-
@pytest.fixture(scope=
|
|
139
|
+
@pytest.fixture(scope=containers_scope)
|
|
133
140
|
def docker_compose_command():
|
|
134
141
|
"""Docker Compose command to use, it could be either `docker compose`
|
|
135
142
|
for Docker Compose V2 or `docker-compose` for Docker Compose
|
|
@@ -138,7 +145,7 @@ def docker_compose_command():
|
|
|
138
145
|
return "docker compose"
|
|
139
146
|
|
|
140
147
|
|
|
141
|
-
@pytest.fixture(scope=
|
|
148
|
+
@pytest.fixture(scope=containers_scope)
|
|
142
149
|
def docker_compose_file(pytestconfig):
|
|
143
150
|
"""Get an absolute path to the `docker-compose.yml` file. Override this
|
|
144
151
|
fixture in your tests if you need a custom location."""
|
|
@@ -146,7 +153,7 @@ def docker_compose_file(pytestconfig):
|
|
|
146
153
|
return os.path.join(str(pytestconfig.rootdir), "tests", "docker-compose.yml")
|
|
147
154
|
|
|
148
155
|
|
|
149
|
-
@pytest.fixture(scope=
|
|
156
|
+
@pytest.fixture(scope=containers_scope)
|
|
150
157
|
def docker_compose_project_name():
|
|
151
158
|
"""Generate a project name using the current process PID. Override this
|
|
152
159
|
fixture in your tests if you need a particular project name."""
|
|
@@ -155,11 +162,10 @@ def docker_compose_project_name():
|
|
|
155
162
|
|
|
156
163
|
|
|
157
164
|
def get_cleanup_command():
|
|
165
|
+
return ["down -v"]
|
|
158
166
|
|
|
159
|
-
return "down -v"
|
|
160
167
|
|
|
161
|
-
|
|
162
|
-
@pytest.fixture(scope="session")
|
|
168
|
+
@pytest.fixture(scope=containers_scope)
|
|
163
169
|
def docker_cleanup():
|
|
164
170
|
"""Get the docker_compose command to be executed for test clean-up actions.
|
|
165
171
|
Override this fixture in your tests if you need to change clean-up actions.
|
|
@@ -169,11 +175,10 @@ def docker_cleanup():
|
|
|
169
175
|
|
|
170
176
|
|
|
171
177
|
def get_setup_command():
|
|
172
|
-
|
|
173
|
-
return "up --build -d"
|
|
178
|
+
return ["up --build -d"]
|
|
174
179
|
|
|
175
180
|
|
|
176
|
-
@pytest.fixture(scope=
|
|
181
|
+
@pytest.fixture(scope=containers_scope)
|
|
177
182
|
def docker_setup():
|
|
178
183
|
"""Get the docker_compose command to be executed for test setup actions.
|
|
179
184
|
Override this fixture in your tests if you need to change setup actions.
|
|
@@ -196,7 +201,11 @@ def get_docker_services(
|
|
|
196
201
|
|
|
197
202
|
# setup containers.
|
|
198
203
|
if docker_setup:
|
|
199
|
-
|
|
204
|
+
# Maintain backwards compatibility with the string format.
|
|
205
|
+
if isinstance(docker_setup, str):
|
|
206
|
+
docker_setup = [docker_setup]
|
|
207
|
+
for command in docker_setup:
|
|
208
|
+
docker_compose.execute(command)
|
|
200
209
|
|
|
201
210
|
try:
|
|
202
211
|
# Let test(s) run.
|
|
@@ -204,10 +213,14 @@ def get_docker_services(
|
|
|
204
213
|
finally:
|
|
205
214
|
# Clean up.
|
|
206
215
|
if docker_cleanup:
|
|
207
|
-
|
|
216
|
+
# Maintain backwards compatibility with the string format.
|
|
217
|
+
if isinstance(docker_cleanup, str):
|
|
218
|
+
docker_cleanup = [docker_cleanup]
|
|
219
|
+
for command in docker_cleanup:
|
|
220
|
+
docker_compose.execute(command)
|
|
208
221
|
|
|
209
222
|
|
|
210
|
-
@pytest.fixture(scope=
|
|
223
|
+
@pytest.fixture(scope=containers_scope)
|
|
211
224
|
def docker_services(
|
|
212
225
|
docker_compose_command,
|
|
213
226
|
docker_compose_file,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: pytest-docker
|
|
3
|
-
Version: 2.0
|
|
3
|
+
Version: 2.2.0
|
|
4
4
|
Summary: Simple pytest fixtures for Docker and Docker Compose based tests
|
|
5
5
|
Home-page: https://github.com/avast/pytest-docker
|
|
6
6
|
Author: Max K., Andre Caron
|
|
@@ -21,19 +21,26 @@ Classifier: Operating System :: POSIX
|
|
|
21
21
|
Classifier: Operating System :: Microsoft :: Windows
|
|
22
22
|
Requires-Python: >=3.6
|
|
23
23
|
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE.txt
|
|
25
|
+
Requires-Dist: pytest<8.0,>=4.0
|
|
26
|
+
Requires-Dist: attrs>=19.2.0
|
|
24
27
|
Provides-Extra: docker-compose-v1
|
|
28
|
+
Requires-Dist: docker-compose<2.0,>=1.27.3; extra == "docker-compose-v1"
|
|
25
29
|
Provides-Extra: tests
|
|
26
|
-
|
|
30
|
+
Requires-Dist: requests<3.0,>=2.22.0; extra == "tests"
|
|
31
|
+
Requires-Dist: pytest-pylint<1.0,>=0.14.1; extra == "tests"
|
|
32
|
+
Requires-Dist: pytest-pycodestyle<3.0,>=2.0.0; extra == "tests"
|
|
27
33
|
|
|
28
34
|
Docker-based integration tests
|
|
29
35
|
=====
|
|
36
|
+
|
|
30
37
|
[](https://pypi.org/project/pytest-docker/)
|
|
31
38
|
[](https://github.com/avast/pytest-docker/actions/workflows/tests.yaml)
|
|
32
39
|
[](https://pypi.org/project/pytest-docker/)
|
|
33
40
|
[](https://github.com/psf/black)
|
|
34
41
|
|
|
35
|
-
|
|
36
42
|
# Description
|
|
43
|
+
|
|
37
44
|
Simple [pytest](http://doc.pytest.org/) fixtures that help you write integration
|
|
38
45
|
tests with Docker and [Docker Compose](https://docs.docker.com/compose/).
|
|
39
46
|
Specify all necessary containers in a `docker-compose.yml` file and and
|
|
@@ -45,6 +52,7 @@ This package is tested with Python versions `3.6`, `3.7`, `3.8` and
|
|
|
45
52
|
`pytest-docker` was originally created by André Caron.
|
|
46
53
|
|
|
47
54
|
# Installation
|
|
55
|
+
|
|
48
56
|
Install `pytest-docker` with `pip` or add it to your test requirements.
|
|
49
57
|
|
|
50
58
|
By default, it uses the `docker compose` command, so it relies on the Compose plugin for Docker (also called Docker Compose V2).
|
|
@@ -61,6 +69,7 @@ def docker_compose_command() -> str:
|
|
|
61
69
|
```
|
|
62
70
|
|
|
63
71
|
If you want to use the pip-distributed version of `docker-compose` command, you can install it using
|
|
72
|
+
|
|
64
73
|
```
|
|
65
74
|
pip install pytest-docker[docker-compose-v1]
|
|
66
75
|
```
|
|
@@ -68,6 +77,7 @@ pip install pytest-docker[docker-compose-v1]
|
|
|
68
77
|
Another option could be usage of [`compose-switch`](https://github.com/docker/compose-switch).
|
|
69
78
|
|
|
70
79
|
# Usage
|
|
80
|
+
|
|
71
81
|
Here is an example of a test that depends on a HTTP service.
|
|
72
82
|
|
|
73
83
|
With a `docker-compose.yml` file like this (using the
|
|
@@ -135,7 +145,16 @@ def docker_compose_file(pytestconfig):
|
|
|
135
145
|
```
|
|
136
146
|
|
|
137
147
|
## Available fixtures
|
|
138
|
-
|
|
148
|
+
|
|
149
|
+
By default the scope of the fixtures are `session` but can be changed with
|
|
150
|
+
`pytest` command line option `--container-scope <scope>`:
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
pytest --container-scope <scope> <test_directory>
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
For available scopes and descriptions
|
|
157
|
+
see <https://docs.pytest.org/en/6.2.x/fixture.html#fixture-scopes>
|
|
139
158
|
|
|
140
159
|
### `docker_ip`
|
|
141
160
|
|
|
@@ -164,29 +183,33 @@ Docker Compose V1, change this fixture to return `docker-compose`.
|
|
|
164
183
|
|
|
165
184
|
### `docker_setup`
|
|
166
185
|
|
|
167
|
-
Get the docker_compose
|
|
186
|
+
Get the list of docker_compose commands to be executed for test spawn actions.
|
|
168
187
|
Override this fixture in your tests if you need to change spawn actions.
|
|
169
188
|
Returning anything that would evaluate to False will skip this command.
|
|
170
189
|
|
|
171
190
|
### `docker_cleanup`
|
|
172
191
|
|
|
173
|
-
Get the docker_compose
|
|
192
|
+
Get the list of docker_compose commands to be executed for test clean-up actions.
|
|
174
193
|
Override this fixture in your tests if you need to change clean-up actions.
|
|
175
194
|
Returning anything that would evaluate to False will skip this command.
|
|
176
195
|
|
|
177
|
-
|
|
178
196
|
# Development
|
|
197
|
+
|
|
179
198
|
Use of a virtual environment is recommended. See the
|
|
180
199
|
[venv](https://docs.python.org/3/library/venv.html) package for more
|
|
181
200
|
information.
|
|
182
201
|
|
|
183
202
|
First, install `pytest-docker` and its test dependencies:
|
|
184
203
|
|
|
185
|
-
|
|
204
|
+
```bash
|
|
205
|
+
pip install -e ".[tests]"
|
|
206
|
+
```
|
|
186
207
|
|
|
187
208
|
Run tests with
|
|
188
209
|
|
|
189
|
-
|
|
210
|
+
```bash
|
|
211
|
+
pytest -c setup.cfg
|
|
212
|
+
```
|
|
190
213
|
|
|
191
214
|
to make sure that the correct configuration is used. This is also how tests are
|
|
192
215
|
run in CI.
|
|
@@ -195,9 +218,9 @@ Use [black](https://pypi.org/project/black/) with default settings for
|
|
|
195
218
|
formatting. You can also use `pylint` with `setup.cfg` as the configuration
|
|
196
219
|
file.
|
|
197
220
|
|
|
198
|
-
|
|
199
221
|
# Contributing
|
|
200
|
-
|
|
222
|
+
|
|
223
|
+
This `pytest` plug-in and its source code are made available to you under a MIT
|
|
201
224
|
license. It is safe to use in commercial and closed-source applications. Read
|
|
202
225
|
the license for details!
|
|
203
226
|
|
|
@@ -174,3 +174,121 @@ def test_wait_until_responsive_timeout():
|
|
|
174
174
|
)
|
|
175
175
|
assert sleep.call_args_list == [mock.call(1.0), mock.call(1.0), mock.call(1.0)]
|
|
176
176
|
assert str(exc.value) == ("Timeout reached while waiting on service!")
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def test_single_commands():
|
|
180
|
+
"""Ensures backwards compatibility with single command strings for setup and cleanup."""
|
|
181
|
+
|
|
182
|
+
with mock.patch("subprocess.check_output") as check_output:
|
|
183
|
+
check_output.returncode = 0
|
|
184
|
+
|
|
185
|
+
assert check_output.call_count == 0
|
|
186
|
+
|
|
187
|
+
# The fixture is a context-manager.
|
|
188
|
+
with get_docker_services(
|
|
189
|
+
"docker compose",
|
|
190
|
+
"docker-compose.yml",
|
|
191
|
+
docker_compose_project_name="pytest123",
|
|
192
|
+
docker_setup="up --build -d",
|
|
193
|
+
docker_cleanup="down -v",
|
|
194
|
+
) as services:
|
|
195
|
+
assert isinstance(services, Services)
|
|
196
|
+
|
|
197
|
+
assert check_output.call_count == 1
|
|
198
|
+
|
|
199
|
+
# Can request port for services.
|
|
200
|
+
port = services.port_for("hello", 80)
|
|
201
|
+
assert port == 1
|
|
202
|
+
|
|
203
|
+
assert check_output.call_count == 2
|
|
204
|
+
|
|
205
|
+
# 2nd request for same service should hit the cache.
|
|
206
|
+
port = services.port_for("hello", 80)
|
|
207
|
+
assert port == 1
|
|
208
|
+
|
|
209
|
+
assert check_output.call_count == 2
|
|
210
|
+
|
|
211
|
+
assert check_output.call_count == 3
|
|
212
|
+
|
|
213
|
+
# Both should have been called.
|
|
214
|
+
assert check_output.call_args_list == [
|
|
215
|
+
mock.call(
|
|
216
|
+
'docker compose -f "docker-compose.yml" -p "pytest123" up --build -d',
|
|
217
|
+
stderr=subprocess.STDOUT,
|
|
218
|
+
shell=True,
|
|
219
|
+
),
|
|
220
|
+
mock.call(
|
|
221
|
+
'docker compose -f "docker-compose.yml" -p "pytest123" port hello 80',
|
|
222
|
+
stderr=subprocess.STDOUT,
|
|
223
|
+
shell=True,
|
|
224
|
+
),
|
|
225
|
+
mock.call(
|
|
226
|
+
'docker compose -f "docker-compose.yml" -p "pytest123" down -v',
|
|
227
|
+
stderr=subprocess.STDOUT,
|
|
228
|
+
shell=True,
|
|
229
|
+
),
|
|
230
|
+
]
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def test_multiple_commands():
|
|
234
|
+
"""Multiple startup and cleanup commands should be executed."""
|
|
235
|
+
|
|
236
|
+
with mock.patch("subprocess.check_output") as check_output:
|
|
237
|
+
check_output.returncode = 0
|
|
238
|
+
|
|
239
|
+
assert check_output.call_count == 0
|
|
240
|
+
|
|
241
|
+
# The fixture is a context-manager.
|
|
242
|
+
with get_docker_services(
|
|
243
|
+
"docker compose",
|
|
244
|
+
"docker-compose.yml",
|
|
245
|
+
docker_compose_project_name="pytest123",
|
|
246
|
+
docker_setup=["ps", "up --build -d"],
|
|
247
|
+
docker_cleanup=["down -v", "ps"],
|
|
248
|
+
) as services:
|
|
249
|
+
assert isinstance(services, Services)
|
|
250
|
+
|
|
251
|
+
assert check_output.call_count == 2
|
|
252
|
+
|
|
253
|
+
# Can request port for services.
|
|
254
|
+
port = services.port_for("hello", 80)
|
|
255
|
+
assert port == 1
|
|
256
|
+
|
|
257
|
+
assert check_output.call_count == 3
|
|
258
|
+
|
|
259
|
+
# 2nd request for same service should hit the cache.
|
|
260
|
+
port = services.port_for("hello", 80)
|
|
261
|
+
assert port == 1
|
|
262
|
+
|
|
263
|
+
assert check_output.call_count == 3
|
|
264
|
+
|
|
265
|
+
assert check_output.call_count == 5
|
|
266
|
+
|
|
267
|
+
# Both should have been called.
|
|
268
|
+
assert check_output.call_args_list == [
|
|
269
|
+
mock.call(
|
|
270
|
+
'docker compose -f "docker-compose.yml" -p "pytest123" ps',
|
|
271
|
+
stderr=subprocess.STDOUT,
|
|
272
|
+
shell=True,
|
|
273
|
+
),
|
|
274
|
+
mock.call(
|
|
275
|
+
'docker compose -f "docker-compose.yml" -p "pytest123" up --build -d',
|
|
276
|
+
stderr=subprocess.STDOUT,
|
|
277
|
+
shell=True,
|
|
278
|
+
),
|
|
279
|
+
mock.call(
|
|
280
|
+
'docker compose -f "docker-compose.yml" -p "pytest123" port hello 80',
|
|
281
|
+
stderr=subprocess.STDOUT,
|
|
282
|
+
shell=True,
|
|
283
|
+
),
|
|
284
|
+
mock.call(
|
|
285
|
+
'docker compose -f "docker-compose.yml" -p "pytest123" down -v',
|
|
286
|
+
stderr=subprocess.STDOUT,
|
|
287
|
+
shell=True,
|
|
288
|
+
),
|
|
289
|
+
mock.call(
|
|
290
|
+
'docker compose -f "docker-compose.yml" -p "pytest123" ps',
|
|
291
|
+
stderr=subprocess.STDOUT,
|
|
292
|
+
shell=True,
|
|
293
|
+
),
|
|
294
|
+
]
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import os.path
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def test_docker_compose_file(docker_compose_file):
|
|
8
|
+
assert docker_compose_file == os.path.join(HERE, "docker-compose.yml")
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def test_docker_compose_project(docker_compose_project_name):
|
|
12
|
+
assert docker_compose_project_name == "pytest{}".format(os.getpid())
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def test_docker_cleanup(docker_cleanup):
|
|
16
|
+
assert docker_cleanup == ["down -v"]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def test_docker_setup(docker_setup):
|
|
21
|
+
assert docker_setup == ["up --build -d"]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_docker_compose_command(docker_compose_command):
|
|
26
|
+
assert docker_compose_command == "docker compose"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_default_container_scope(pytester):
|
|
30
|
+
pytester.makepyfile(
|
|
31
|
+
"""
|
|
32
|
+
import pytest
|
|
33
|
+
@pytest.fixture(scope="session")
|
|
34
|
+
def dummy(docker_cleanup):
|
|
35
|
+
return True
|
|
36
|
+
|
|
37
|
+
def test_default_container_scope(dummy):
|
|
38
|
+
assert dummy == True
|
|
39
|
+
"""
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
result = pytester.runpytest()
|
|
43
|
+
result.assert_outcomes(passed=1)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@pytest.mark.parametrize("scope", ["session", "module", "class"])
|
|
47
|
+
def test_general_container_scope(testdir, request, scope):
|
|
48
|
+
params = [f"--container-scope={scope}"]
|
|
49
|
+
assert request.config.pluginmanager.hasplugin("docker")
|
|
50
|
+
|
|
51
|
+
testdir.makepyfile(
|
|
52
|
+
f"""
|
|
53
|
+
import pytest
|
|
54
|
+
@pytest.fixture(scope="{scope}")
|
|
55
|
+
def dummy(docker_cleanup):
|
|
56
|
+
return True
|
|
57
|
+
|
|
58
|
+
def test_default_container_scope(dummy):
|
|
59
|
+
assert dummy == True
|
|
60
|
+
"""
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
result = testdir.runpytest(*params)
|
|
64
|
+
result.assert_outcomes(passed=1)
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
from .plugin import (
|
|
2
|
-
docker_cleanup,
|
|
3
|
-
docker_compose_command,
|
|
4
|
-
docker_compose_file,
|
|
5
|
-
docker_compose_project_name,
|
|
6
|
-
docker_ip,
|
|
7
|
-
docker_setup,
|
|
8
|
-
docker_cleanup,
|
|
9
|
-
docker_services,
|
|
10
|
-
)
|
|
11
|
-
|
|
12
|
-
__all__ = [
|
|
13
|
-
"docker_cleanup",
|
|
14
|
-
"docker_compose_file",
|
|
15
|
-
"docker_compose_project_name",
|
|
16
|
-
"docker_ip",
|
|
17
|
-
"docker_setup",
|
|
18
|
-
"docker_cleanup",
|
|
19
|
-
"docker_services",
|
|
20
|
-
]
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import os.path
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
def test_docker_compose_file(docker_compose_file):
|
|
8
|
-
assert docker_compose_file == os.path.join(HERE, "docker-compose.yml")
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def test_docker_compose_project(docker_compose_project_name):
|
|
12
|
-
assert docker_compose_project_name == "pytest{}".format(os.getpid())
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def test_docker_cleanup(docker_cleanup):
|
|
16
|
-
assert docker_cleanup == "down -v"
|
|
17
|
-
|
|
18
|
-
def test_docker_setup(docker_setup):
|
|
19
|
-
assert docker_setup == "up --build -d"
|
|
20
|
-
|
|
21
|
-
def test_docker_compose_comand(docker_compose_command):
|
|
22
|
-
assert docker_compose_command == "docker compose"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|