使用 python3 和不使用 __init__.py 文件的递归单元测试发现 [英] Recursive unittest discovery with python3 and without __init__.py files
问题描述
我有以下目录结构的项目:
<预><代码>.├──需求.txt├── main.py├── 测试├──单位│ └── test_thing1.py│ └── test_thing2.py└── 整合└── test_integration_thing1.py└── test_integration_thing2.py我想用一个命令运行所有测试.如果我执行 python -m unittest discovery
,则不会执行任何测试.
我发现这个问题建议添加一个__init__.py
文件从 unit
和 integration
文件夹中制作包.该解决方案有效,并且所有测试都以这种方式运行.
但由于我使用的是 python3 和 __init__.py
文件,隐式命名空间包,我想知道是否有办法在没有那些 __init__.py
文件的情况下使其工作.
我查看了它,并认为它很可能是一个错误.
所以我创建了一个python错误报告和一个PR 为我解决了这个问题.
克隆它,看看它是否也适合你,修复后运行发现的最佳方式是 python -m unittest discover -s tests
.否则,它将查找例如位于顶级目录下方的虚拟环境.
I have project with the following directory structure:
.
├── requirements.txt
├── main.py
├── tests
├── unit
│ └── test_thing1.py
│ └── test_thing2.py
└── integration
└── test_integration_thing1.py
└── test_integration_thing2.py
I want to run all tests with one command. If I do python -m unittest discover
, no tests are executed.
I found this question that suggest adding a __init__.py
file to make packages out of the unit
and integration
folders. The solution works, and all tests are running this way.
But since I'm using python3 and __init__.py
files are not required with implicit namespace packages, I was wondering if there was a way to make this works without those __init__.py
files.
I looked into it, and think its most likely a bug.
So I created a python bug report and a PR which fixes the issue for me.
Clone it and see if it works for you as well, with the fix the best way of running discover was python -m unittest discover -s tests
. Otherwise it will look in for example virtual environments that are below the top dir.
这篇关于使用 python3 和不使用 __init__.py 文件的递归单元测试发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!