![](/img/trans.png)
[英]How to get tox to test "named" test environments with different python versions?
[英]CircleCI for simple Python project with tox: how to test multiple Python environments?
我有一个Python 项目,我使用tox
来运行基于 pytest 的测试。 我正在尝试配置项目以在 CircleCI 上构建。
tox.ini
列出了 Python 3.6 和 3.7 作为环境:
envlist = py{36,37,},coverage
我可以在使用 Python 3.7 版的 conda 虚拟环境中的本地机器上成功运行tox
。
在 CircleCI 上,我使用的是标准 Python 虚拟环境,因为这是示例(“入门”)配置中提供的内容。 当 tox 尝试创建 Python 3.6 环境时,tox 测试失败:
py36 create: /home/circleci/repo/.tox/py36
ERROR: InterpreterNotFound: python3.6
看起来,当您使用这种虚拟环境时,tox 只能找到相同版本的解释器,而如果使用 conda 虚拟环境,则只要它们是较低版本,它就会以某种方式知道如何构建环境。 至少对于我的情况(在 Python 3.7 conda 环境中运行的 tox 的 Python 3.6 和 3.7 环境),这很好用。
我目前使用的 CircleCI 配置文件如下所示:
# Python CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-python/ for more details
#
version: 2
jobs:
build:
docker:
# specify the version you desire here
# CircleCI maintains a library of pre-built images
# documented at https://circleci.com/docs/2.0/circleci-images/
- image: circleci/python:3.7
working_directory: ~/repo
steps:
- checkout
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "requirements.txt" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- run:
name: install dependencies
command: |
python3 -m venv venv
. venv/bin/activate
pip install -e .
pip install tox
- save_cache:
paths:
- ./venv
key: v1-dependencies-{{ checksum "requirements.txt" }}
# run tests with tox
- run:
name: run tests
command: |
. venv/bin/activate
tox
- store_artifacts:
path: test-reports
destination: test-reports
在 CircleCI 上使用 tox 测试多个环境的最佳实践是什么? 我应该在 CircleCI 中使用 conda 而不是 venv,如果是这样,我将如何添加它? 或者有没有办法留在 venv,也许通过修改它的环境创建命令?
我现在发现这不是 CircleCI 特有的,因为在 Travis CI 上运行这个 tox 时我遇到了类似的错误。 此外,我已经确认这在我的本地机器上使用使用venv
创建的 Python 3.7 虚拟环境所宣传的那样工作,py36 和 py37 环境都成功运行。
我已经解决了这个问题,尽管并不完全,因为它涉及放弃tox
,而是使用pytest
在单独的工作流作业中为每个 Python 版本环境运行测试。 CircleCI 配置( .circleci/config.yml
)如下所示:
version: 2
workflows:
version: 2
test:
jobs:
- test-3.6
- test-3.7
- test-3.8
jobs:
test-3.6: &test-template
docker:
- image: circleci/python:3.6
working_directory: ~/repo
steps:
- checkout
- restore_cache:
keys:
- v1-dependencies-{{ checksum "requirements.txt" }}
- v1-dependencies-
- run:
name: install dependencies
command: |
python3 -m venv venv
. venv/bin/activate
pip install -e .
pip install coverage
pip install pytest
- save_cache:
paths:
- ./venv
key: v1-dependencies-{{ checksum "requirements.txt" }}
- run:
name: run tests
command: |
. venv/bin/activate
coverage run -m pytest tests
# store artifacts (for example logs, binaries, etc)
# to be available in the web app or through the API
- store_artifacts:
path: test-reports
test-3.7:
<<: *test-template
docker:
- image: circleci/python:3.7
test-3.8:
<<: *test-template
docker:
- image: circleci/python:3.8
例如,如果您使用多 python docker 映像,它允许您仍然使用 tox 在多个不同的环境中进行测试
version: 2
jobs:
test:
docker:
- image: fkrull/multi-python
steps:
- checkout
- run:
name: Test
command: 'tox'
workflows:
version: 2
test:
jobs:
- test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.