在python中测试非导出方法

Pab*_*dez 5 python testing directory-structure

我的代码是这样组织的:

app/sampling
??? __init__.py
??? filters.py
??? test
    ??? filters_test.py
Run Code Online (Sandbox Code Playgroud)

filters.py有一些__init__.py下划线开头的导出函数(包含在中)和一些未导出的函数。

开启filters_test.py我可以轻松测试导出的功能,我可以像这样访问:

from app.sampling import exported_function
Run Code Online (Sandbox Code Playgroud)

(请注意,“ app”是我的PYTHONPATH的一部分)

但是,如果我尝试导入这样的私有函数:

from ..filters import _private_function
Run Code Online (Sandbox Code Playgroud)

这似乎可行,但随后在运行时:

SystemError: Parent module '' not loaded, cannot perform relative import

附加条款:

  • 我正在用鼻子进行测试
  • 我想尽可能保留文件夹结构

Krz*_*ski 3

从 app.sampling.filters 导入 _private_function