项目文件夹中的__init__.py会破坏鼻子测试

Gar*_*ler 5 python testing packages nose

项目树:

.
|-- bar.py
`-- test
    |-- __init__.py
    `-- test_bar.py
Run Code Online (Sandbox Code Playgroud)

bar.py:

def dumb_true():
    return True
Run Code Online (Sandbox Code Playgroud)

测试/ test_bar.py:

import bar

def test_bar_true():
        assert bar.dumb_true()
Run Code Online (Sandbox Code Playgroud)

我可以nosetests从项目内部或其测试目录中运行.__init__.py但是,如果我在项目文件夹中添加一个空,我就不能再nosetests从test目录中运行了,这对我没有任何意义.

.
|-- bar.py
|-- __init__.py  <-- new, empty file, ruining everything
`-- test
    |-- __init__.py
    `-- test_bar.py
Run Code Online (Sandbox Code Playgroud)

任何人都可以向我解释这里发生了什么?

我已经在这个主题上广泛阅读 - 通过鼻子文档/手册页和整个互联网; 但是我觉得这一切都解决了,这看起来很混乱!

小智 3

看来你的问题在这里得到了解答。

__init__.py您的顶级目录中有一个。这使它成为一个包。如果你把它去掉,你的鼻子测试应该会起作用。

如果不删除它,则必须将导入更改为 import dir.foo,其中 dir 是目录的名称。