如何针对不同的 Python *补丁* 版本测试库?

How to test a library against different Python *patch* versions?

我正在编写一个库,想针对不同的 Python 补丁版本进行测试,例如 3.7.1、3.7.2 等

我使用tox已经很久了,但是,根据这个,它并不真正支持这种用法。

有什么建议吗?

可能最不麻烦(但乏味)的方法是在监狱或 VM 中顺序安装不同版本的 Python,然后在其上测试您的代码。

如果您在类 UNIX 系统上从源代码安装 Python,您可以尝试使用不同的前缀并排安装它们(例如 /opt/patch1opt/patch2 等) 然后 明确地 运行 你的测试用正确的 python 就像 /opt/patch1/bin/python3。一个警告;在这种情况下,我不确定 Python 可执行文件是否会找到正确的共享库。

ms-windows 安装程序允许您选择安装位置。如果你指示它 not 将 Python 放在 PATH 中并且不设置文件关联等,那也可能有效。您还必须使用完整路径显式调用正确的 Python。

我会去 Docker 和 运行 它的容器。

$ docker run -it --rm -w /opt -v "$PWD:/opt" python:3.4.2 python <script.py>
-it   - interactive mode  
--rm  - remove container after the run
-w    - working directory inside container
-v    - map directory $PWD from host to /opt inside container
<container>   - python:3.4.2
<command>     - python script.py

您可以使用以下命令查看可用的图像:

$ curl -s https://registry.hub.docker.com/v1/repositories/python/tags | \
    jq -r .[].name | grep "^[23][.0-9]*$" | sort -V
2
2.7
2.7.7
2.7.8
2.7.9
2.7.10
2.7.11
2.7.12
2.7.13
2.7.14
2.7.15
2.7.16
3
3.2
3.2.6
3.3
3.3.5
3.3.6
3.3.7
3.4
3.4.1
3.4.2
3.4.3
3.4.4
3.4.5
3.4.6
3.4.7
3.4.8
3.4.9
3.4.10
3.5
3.5.0
3.5.1
3.5.2
3.5.3
3.5.4
3.5.5
3.5.6
3.5.7
3.6
3.6.0
3.6.1
3.6.2
3.6.3
3.6.4
3.6.5
3.6.6
3.6.7
3.6.8
3.6.9
3.7
3.7.0
3.7.1
3.7.2
3.7.3
3.7.4

grep 过滤掉 beta 和 alpha 版本,所以如果您需要它们 - 只需删除 grep。

如果您需要列表中不存在的 Python 版本,您可以使用自定义 Python.

构建 docker 图像

例如 Alpine linux(它真的很小) https://github.com/docker-library/python/blob/f82205cde8f0a5ffa276103a50d843edced67757/3.7/alpine3.10/Dockerfile

对于 3.8.1 的检查之一(假设您的 python3.8 指向 3.8.2),您可以使用 discover flag

tox --discover /path/to/python3.8.1 -e py38

如果你想定义一个始终使用 3.8.1 的环境,你可以通过定义一个新的 tox 环境并将 basepython 设置为 python3.8.1.

来实现
[testenv:py381]
basepython = python3.8.1

[testenv:py382]
basepython = python3.8.2