How do I use nosetests with a top-level directory as a package?

一世执手 提交于 2021-01-29 15:10:45

问题


This question shows a simple project:

./__init__.py
./foo.py
./tests
   ./__init__.py
   ./test_foo.py

In my case, foo.py is an executable that also contains some functions imported by other code (say bar.py at the top level).

The issue is that nose will not import packages from a top-level directory with an __init__.py.

This answer says to either 1) remove the top-level __init__.py, or 2) change all the imports (including in foo.py if it imports from the top-level directory).

I don't want to do 1, because my foo.py has functions that need to be imported elsewhere.

I don't want to do 2, because that breaks foo.py (it imports from baz.py, also at the top level).

What is the simplest thing I can do, to allow foo.py to be an executable (with a main), be importable, and be testable with nosetests?

I tried multiple different flags, things like

nosetests --exe --no-path-adjustment --no-skip

If I should restructure my project, then what is recommended? Should I put foo.py in bin? That would lead to importing functions from bin.foo, which looks odd. Should I separate functions out of foo.py into bin and lib? Seems complicated.

P.S. Why does nosetests exclude the top-level directory if it is a package? It seems odd, and clearly tons of people have problems with it (the answer I linked currently has 221 votes).

来源:https://stackoverflow.com/questions/60352884/how-do-i-use-nosetests-with-a-top-level-directory-as-a-package

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