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

一笑奈何 提交于 2020-06-17 00:04:30

问题


I'm writing a library and want to test against different Python patch versions, like 3.7.1, 3.7.2, etc

I've been using tox for a long time, however, according to this answer, it doesn't really support this kind of usage.

Any suggestions?


回答1:


For a one of check against 3.8.1 (assuming your python3.8 points to 3.8.2) you can use the discover flag

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

If you want to define an environment that always uses 3.8.1 you can do that by defining a new tox environment and setting basepython as python3.8.1.

[testenv:py381]
basepython = python3.8.1

[testenv:py382]
basepython = python3.8.2



回答2:


Probably the least troublesome (but tedious) way is to install different versions of Python sequentially in a jail or VM and then test your code on it.

If you install Python from source on a UNIX-like system, you could try installing them side by side using different prefixes (eg /opt/patch1, opt/patch2 etc.) And then expliticly run your test with the correct python like /opt/patch1/bin/python3. One caveat; I'm not sure if the Python executable would find the correct shared library in this case.

The ms-windows installer lets you pick an install location. If you instruct it not to put Python in the PATH and not set up file associations and the like, that might also work. You would also have to explicitly invoke the correct Python with the full path.




回答3:


I would go Docker and run it container.

$ 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

You can see what images are available with the command:

$ 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 filters out beta and alpha versions, so if you need them - just remove the grep.

If you need Python version that is not present in the list, you can build docker image with custom Python.

take eg Alpine linux (it's really small) https://github.com/docker-library/python/blob/f82205cde8f0a5ffa276103a50d843edced67757/3.7/alpine3.10/Dockerfile



来源:https://stackoverflow.com/questions/57245532/how-to-test-a-library-against-different-python-patch-versions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!