【问题标题】:CircleCI for simple Python project with tox: how to test multiple Python environments?带有tox的简单Python项目的CircleCI:如何测试多个Python环境?
【发布时间】:2020-01-29 17:07:35
【问题描述】:

我有一个Python project,我使用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 上运行此毒物时遇到了类似的错误。此外,我已经确认这可以使用在我的本地计算机上使用venv 创建的 Python 3.7 虚拟环境进行宣传,py36 和 py37 环境都可以成功运行。

【问题讨论】:

    标签: python circleci tox


    【解决方案1】:

    如果您使用multi-python docker image,它允许您仍然使用 tox 在多个不同环境中进行测试,例如

    version: 2
    
    jobs:
        test:
            docker:
                - image: fkrull/multi-python
            steps:
                - checkout
                - run:
                    name: Test
                    command: 'tox'
    
    workflows:
        version: 2
        test:
            jobs:
                - test
    

    【讨论】:

      【解决方案2】:

      虽然不完全,但我已经解决了这个问题,因为它涉及放弃 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-03
        • 2018-10-29
        • 1970-01-01
        相关资源
        最近更新 更多