问题
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