Recursive unittest discovery with python3 and without __init__.py files

久未见 提交于 2020-05-10 07:15:23

问题


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.


回答1:


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.




回答2:


Use pytest. It does discover tests in folders.

In your case you just need to install in via pip (pip install pytest) and then run in your root folder pytest tests.



来源:https://stackoverflow.com/questions/46976256/recursive-unittest-discovery-with-python3-and-without-init-py-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!