我有一个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
使用tox在CircleCI上测试多个环境的最佳实践是什么?我应该转向在CircleCI中使用conda而不是venv吗?如果是这样,我该如何添加?还是有一种方法可以继续使用venv,也许通过修改其环境创建命令?
编辑:
我现在发现这并不特定于CircleCI,因为当在Travis CI上运行此tox时,我会得到类似的错误。另外,我确认在本地机器上使用venv创建的Python 3.7虚拟环境可以按照广告运行,py36和py37环境都能成功运行。