【问题标题】:Recursive unittest discovery with python3 and without __init__.py files使用 python3 和没有 __init__.py 文件的递归单元测试发现
【发布时间】:2018-04-09 03:08:33
【问题描述】:

我的项目具有以下目录结构:

.
├── requirements.txt
├── main.py
├── tests
    ├── unit
    │   └── test_thing1.py
    │   └── test_thing2.py
    └── integration
        └── test_integration_thing1.py
        └── test_integration_thing2.py

我想用一个命令运行所有测试。如果我执行python -m unittest discover,则不会执行任何测试。

我发现this question 建议添加一个__init__.py 文件以从unitintegration 文件夹中制作包。该解决方案有效,并且所有测试都以这种方式运行。

但由于我使用的是 python3,而 implicit namespace packages 不需要 __init__.py 文件,我想知道是否有办法在没有这些 __init__.py 文件的情况下使其工作。

【问题讨论】:

  • 似乎与bug#17457 相关,已在 python 3.4 中修复。你用的是哪个版本的python?

标签: python python-3.x unit-testing python-unittest


【解决方案1】:

请注意,命名空间包是常规包。 您不能期望命名空间包与常规包相同。

也阅读这篇文章。 https://dev.to/methane/don-t-omit-init-py-3hga

【讨论】:

    【解决方案2】:

    我研究了一下,认为它很可能是一个错误。

    所以我创建了一个 python bug report 和一个 PR 来解决我的问题。

    克隆它,看看它是否也适合你,修复后运行发现的最佳方式是python -m unittest discover -s tests。否则,它将查找例如位于顶级目录之下的虚拟环境。

    【讨论】:

    【解决方案3】:

    使用 pytest。它确实会在文件夹中发现测试。

    在您的情况下,您只需要通过 pip (pip install pytest) 安装,然后在您的根文件夹 pytest tests 中运行。

    【讨论】:

      猜你喜欢
      • 2014-02-27
      • 2015-06-25
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      • 2020-09-05
      • 2018-04-26
      • 2020-08-29
      相关资源
      最近更新 更多