til: docker_pytest-docker.md
This data as json
| path | topic | title | url | body | html | shot | created | created_utc | updated | updated_utc | shot_hash | slug |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| docker_pytest-docker.md | docker | Run pytest against a specific Python version using Docker | https://github.com/simonw/til/blob/main/docker/pytest-docker.md | For [datasette issue #1802](https://github.com/simonw/datasette/issues/1802) I needed to run my `pytest` test suite using a specific version of Python 3.7. I decided to do this using Docker, using the official [python:3.7-buster](https://hub.docker.com/_/python/tags?page=1&name=3.7-buster) image. Here's the recipe that worked for me: ```bash docker run --rm -it -v `pwd`:/code \ python:3.7-buster \ bash -c "cd /code && pip install -e '.[test]' && pytest" ``` This command runs interactively so I can see the output (the `-it` option). It mounts the current directory (with my testable application in it - I ran this in the root of a `datasette` checkout) as the `/code` volume inside the container. The `--rm` option ensures that the container used for the test will be deleted once the test has completed (not just stopped). It then runs the following using `bash -c`: cd /code && pip install -e '.[test]' && pytest This installs my project's dependencies and test dependencies and then runs `pytest`. The truncated output looks like this: ``` % docker run -it -v `pwd`:/code \ python:3.7-buster \ bash -c "cd /code && pip install -e '.[test]' && pytest" Obtaining file:///code Preparing metadata (setup.py) ... done Collecting asgiref>=3.2.10 Downloading asgiref-3.5.2-py3-none-any.whl (22 kB) ... Installing collected packages: rfc3986, mypy-extensions, iniconfig, zipp, typing-extensions, typed-ast, tomli, soupsieve, sniffio, six, PyYAML, pyparsing, pycparser, py, platformdirs, pathspec, mergedeep, MarkupSafe, itsdangerous, idna, hupper, h11, execnet, cogapp, certifi, attrs, aiofiles, python-multipart, packaging, Jinja2, janus, importlib-metadata, cffi, beautifulsoup4, asgiref, anyio, pluggy, pint, httpcore, cryptography, click, asgi-csrf, uvicorn, trustme, pytest, httpx, click-default-group-wheel, black, pytest-timeout, pytest-forked, pytest-asyncio, datasette, blacken-docs, pytest-xdist Running setup.py develop for datasette ... ========================================================= test session starts ========================================================== platform linux -- Python 3.7.13, pytest-7.1.3, pluggy-1.0.0 SQLite: 3.27.2 rootdir: /code, configfile: pytest.ini plugins: asyncio-0.19.0, anyio-3.6.1, timeout-2.1.0, xdist-2.5.0, forked-1.4.0 asyncio: mode=strict collected 1054 items tests/test_package.py .. [ 0%] tests/test_cli.py . [ 0%] tests/test_cli_serve_get.py .. [ 0%] tests/test_cli.py . [ 0%] tests/test_black.py . [ 0%] tests/test_api.py .................................................. [ 5%] ``` | <p>For <a href="https://github.com/simonw/datasette/issues/1802">datasette issue #1802</a> I needed to run my <code>pytest</code> test suite using a specific version of Python 3.7.</p> <p>I decided to do this using Docker, using the official <a href="https://hub.docker.com/_/python/tags?page=1&name=3.7-buster" rel="nofollow">python:3.7-buster</a> image.</p> <p>Here's the recipe that worked for me:</p> <div class="highlight highlight-source-shell"><pre>docker run --rm -it -v <span class="pl-s"><span class="pl-pds">`</span>pwd<span class="pl-pds">`</span></span>:/code \ python:3.7-buster \ bash -c <span class="pl-s"><span class="pl-pds">"</span>cd /code && pip install -e '.[test]' && pytest<span class="pl-pds">"</span></span></pre></div> <p>This command runs interactively so I can see the output (the <code>-it</code> option).</p> <p>It mounts the current directory (with my testable application in it - I ran this in the root of a <code>datasette</code> checkout) as the <code>/code</code> volume inside the container.</p> <p>The <code>--rm</code> option ensures that the container used for the test will be deleted once the test has completed (not just stopped).</p> <p>It then runs the following using <code>bash -c</code>:</p> <pre><code>cd /code && pip install -e '.[test]' && pytest </code></pre> <p>This installs my project's dependencies and test dependencies and then runs <code>pytest</code>.</p> <p>The truncated output looks like this:</p> <pre><code>% docker run -it -v `pwd`:/code \ python:3.7-buster \ bash -c "cd /code && pip install -e '.[test]' && pytest" Obtaining file:///code Preparing metadata (setup.py) ... done Collecting asgiref>=3.2.10 Downloading asgiref-3.5.2-py3-none-any.whl (22 kB) ... Installing collected packages: rfc3986, mypy-extensions, iniconfig, zipp, typing-extensions, typed-ast, tomli, soupsieve, sniffio, six, PyYAML, pyparsing, pycparser, py, platformdirs, pathspec, mergedeep, MarkupSafe, itsdangerous, idna, hupper, h11, execnet, cogapp, certifi, attrs, aiofiles, python-multipart, packaging, Jinja2, janus, importlib-metadata, cffi, beautifulsoup4, asgiref, anyio, pluggy, pint, httpcore, cryptography, click, asgi-csrf, uvicorn, trustme, pytest, httpx, click-default-group-wheel, black, pytest-timeout, pytest-forked, pytest-asyncio, datasette, blacken-docs, pytest-xdist Running setup.py develop for datasette ... ========================================================= test session starts ========================================================== platform linux -- Python 3.7.13, pytest-7.1.3, pluggy-1.0.0 SQLite: 3.27.2 rootdir: /code, configfile: pytest.ini plugins: asyncio-0.19.0, anyio-3.6.1, timeout-2.1.0, xdist-2.5.0, forked-1.4.0 asyncio: mode=strict collected 1054 items tests/test_package.py .. [ 0%] tests/test_cli.py . [ 0%] tests/test_cli_serve_get.py .. [ 0%] tests/test_cli.py . [ 0%] tests/test_black.py . [ 0%] tests/test_api.py .................................................. [ 5%] </code></pre> | <Binary: 67,170 bytes> | 2022-09-05T16:23:06-07:00 | 2022-09-05T23:23:06+00:00 | 2022-09-06T10:51:25-07:00 | 2022-09-06T17:51:25+00:00 | 7b5c417fbdee6e3968459b3d0db5c6a9 | pytest-docker |