Skip to content

Instantly share code, notes, and snippets.

@roguh
Last active March 29, 2024 22:38
Show Gist options
  • Save roguh/eae6f03861dc9fc5b737b69b334a1737 to your computer and use it in GitHub Desktop.
Save roguh/eae6f03861dc9fc5b737b69b334a1737 to your computer and use it in GitHub Desktop.
# This demonstrates using tox to test different Python versions and different library versions on select Python versions.
# TODO: use asdf, pyenv, Dockerfile to install many py versions
# Dependencies: aiohttp
# Dev dependencies: pytest pytest-asyncio pytest-cov tox tox-pyenv
[tox]
envlist =
# The oldest Python version we support
# 3.7 is EOL and 3.8 only has 1 more year of security updates
# Test 3.8 with an old version of dependencies
py38
py38-oldaiohttp
py39
py310
py311
# The latest Python version.
# Test it with an old version of dependencies
py311-oldaiohttp
py311-olderaiohttp
# The future :)
py312
[testenv]
# Example here: https://stackoverflow.com/questions/57024579/tox-ini-environment-with-multiple-dependencies
# 'deps' can also point to any number of requirements.txt or pyproject.toml files.
# Here, we list all dependencies without using other files for the sake of simplicity.
deps:
# Install the latest version of aiohttp for all Python versions, with 2 exceptions.
aiohttp
tox-pyenv
pytest
pytest-asyncio
pytest-cov
# For the two Python versions where we'll support old libraries, install the old dependencies.
# Old aiohttp versions have several CVEs, especially <=3.8.6
# https://security.snyk.io/package/pip/aiohttp
py39-oldaiohttp: aiohttp<3.9
py311-oldaiohttp: aiohttp<3.9
py311-olderaiohttp: aiohttp<3.8
# Run unit tests with pytest.
# Pytest can also run unit tests that only use the builtin unittest module.
commands = pytest --cov-report term --cov-report html --cov=. --capture=no --durations=0 --durations-min=0.01 --log-cli-level=CRITICAL {posargs} tox_example.py
import sys
import aiohttp
import pytest
@pytest.mark.asyncio
async def test_aiohttp_version():
assert isinstance(aiohttp.__version__, str)
if sys.version_info <= (3,8) or sys.version_info >= (3, 11):
assert aiohttp.__version__ in ["3.9.3", "3.8.6", "3.7.4.post0"]
else:
assert aiohttp.__version__ == "3.9.3" or aiohttp.__version__.startswith("4.")
def main():
print("AIOHTTP version is", aiohttp.__version__)
if __name__ == "__main__":
main()
@roguh
Copy link
Author

roguh commented Feb 15, 2024

The first revision is a simple tox.ini file, the next revisions will show how to test more versions of Python and more versions of different dependencies.

@roguh
Copy link
Author

roguh commented Feb 15, 2024

The 2nd revision uses pytest to run an async unit test, and it installs an old version of aiohttp for Python versions 3.11 and 3.8. It also tests the latest aiohttp with all Python versions, from 3.8 to 3.12.

@roguh
Copy link
Author

roguh commented Mar 29, 2024

https://paiml.com/docs/home/books/testing-in-python/chapter09-testing-matrix-tox/

this might be more useful:

[tox]
envlist = py27-release_1.1, py{35,36,37,38}-release_2.x

@roguh
Copy link
Author

roguh commented Mar 29, 2024

Please verify ASDF and its plugins are installing what you expect for your own security.

asdf plugin list all | grep 'node\|py'
asdf plugin add python https://github.com/danhper/asdf-python.git
asdf plugin add nodejs https://github.com/asdf-vm/asdf-nodejs.git # https://github.com/danhper/asdf-pytho$
asdf plugin update --all
asdf list all nodejs
asdf list all python
asdf install python 3.7.17 3.8.19 ....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment