pytest建议包含一个额外的目录来分隔项目中的源代码:
my_package
??? src # <-- no __init__.py on this layer
? ??? my_package
? ??? __init__.py
? ??? util_module
? ??? __init__.py
? ??? utils.py
??? tests
??? __init__.py
??? test_util_module
??? __init__.py
??? test_utils.py
Run Code Online (Sandbox Code Playgroud)
可悲的是,他们没有说1关于测试代码中的导入应该如何在这种情况下工作,这对于我的IDE在这个天真的例子2中工作得很好,但是导致pytest出现以下错误:
my_package $ pytest
====================== test session starts ======================
platform linux -- Python 3.6.4, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
rootdir: /home/user/workspace/my_package, inifile:
collected 0 items / 1 errors
============================ ERRORS =============================
___ ERROR collecting tests/test_util_module/test_utils.py ___
ImportError while importing test …Run Code Online (Sandbox Code Playgroud)