使用tox进行简单的Python项目的CircleCI:如何测试多个Python环境?

3

我有一个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环境都能成功运行。
2个回答

2
如果您使用multi-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

1

虽然这需要放弃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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接