Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
Run pytest against a specific Python version using Docker#
For datasette issue #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 image.
Here’s the recipe that worked for me:
1docker run --rm -it -v `pwd`:/code \2 python:3.7-buster \3 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:
1cd /code && pip install -e '.[test]' && pytestThis installs my project’s dependencies and test dependencies and then runs pytest.
The truncated output looks like this:
1% docker run -it -v `pwd`:/code \2 python:3.7-buster \3 bash -c "cd /code && pip install -e '.[test]' && pytest"4Obtaining file:///code5 Preparing metadata (setup.py) ... done6Collecting asgiref>=3.2.107 Downloading asgiref-3.5.2-py3-none-any.whl (22 kB)8...9Installing 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-xdist10 Running setup.py develop for datasette11...12========================================================= test session starts ==========================================================13platform linux -- Python 3.7.13, pytest-7.1.3, pluggy-1.0.014SQLite: 3.27.215rootdir: /code, configfile: pytest.ini16plugins: asyncio-0.19.0, anyio-3.6.1, timeout-2.1.0, xdist-2.5.0, forked-1.4.017asyncio: mode=strict18collected 1054 items19
20tests/test_package.py .. [ 0%]21tests/test_cli.py . [ 0%]22tests/test_cli_serve_get.py .. [ 0%]23tests/test_cli.py . [ 0%]24tests/test_black.py . [ 0%]25tests/test_api.py .................................................. [ 5%]