【问题标题】:'python -m unittest discover' works locally but unable to discover tests on CircleCi'python -m unittest discover' 在本地工作,但无法在 CircleCi 上发现测试
【发布时间】:2019-12-30 10:50:14
【问题描述】:

我正在尝试将单元测试添加到我在 CircleCi 上的项目中。在本地,当我从终端运行“python -m unittest discover”时,它能够找到并运行所有 16 个测试。但在 CircleCi 上,它说:

Ran 0 tests in 0.000s

知道会发生什么吗? 是否缺少某些配置或环境变量?

我的项目结构:

project
├── src
│   ├── main
│       ├── python
│           ├── testing
│               ├──Test1.py
|               ├──Test2.py

config.yml

version: 2
docker:
  - image: circleci/python:3.7.5
jobs:
  build:
    steps:
      - checkout
      - restore_cache:
          key: deps1-{{ .Branch }}-{{ checksum "requirements.txt" }}
      - run:
          name: Install Python deps in a venv
          command: |
            python3 -m venv venv
            . venv/bin/activate
            pip install -r requirements/dev.txt
      - save_cache:
          key: deps1-{{ .Branch }}-{{ checksum "requirements.txt" }}
          paths:
          - "venv"
      - run:
          name: Running tests
          command: |
            cd ~/project/src/main/python/testing/
            python3 -m unittest discover

【问题讨论】:

    标签: python-3.x python-unittest circleci-2.0


    【解决方案1】:

    尝试为该特定步骤设置working_directory,而不是使用cd,例如

    - run:
        name: Running tests
        working_directory: ~/project/src/main/python/testing/
        command: |
            python3 -m unittest discover
    

    【讨论】:

      【解决方案2】:

      我终于解决了这个问题。 我的测试类以大写“T”开头,默认情况下单元测试会查找test*.py。所以我不得不添加额外的配置,如下所示:

      python -m unittest discover -p "Test*.py"
      

      在本地它能够发现测试,因为 Windows 命令行不区分大小写。 但是 CircleCi 启动的 Linux 命令行是区分大小写的,这就是为什么没有发现测试的原因。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-18
        • 2016-05-04
        • 1970-01-01
        • 1970-01-01
        • 2018-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多