当我使用PyCharm启动测试时,完全没有导入问题,但如果我在终端中运行pytest,则会抱怨找不到模块。具体地说,是在
项目文件夹结构:
我必须使用
另一方面,我可以按照pytest网站的integration advices使用
tests/tools
下的那些模块。项目文件夹结构:
src/
app/
tests/
end_to_end/ # End to end/Acceptance tests
tools/ # Helper modules used by some tests
unit # Unit tests
setup.py
setup.py
的相关部分:
setup(...
packages=find_packages('src'),
package_dir={'': 'src'},
install_requires=['requests'],
test_suite='tests',
tests_require=[
'flexmock',
'pytest',
'wsgi_intercept'
])
无论虚拟环境是否激活,执行py.test tests/end_to_end
时会失败的导入行。从app
包中导入的模块是可以正常工作的:
from tests.tools.foomodule import Foo # Fails
from app.barmodule import Bar # Doesn't fail
我必须使用
pip install -e packagename
安装我的软件包,以便在从命令行运行测试时对pytest可用。我想我将不得不将tools
文件夹作为一个包添加到我的setup.py
中,但这让我感觉很丑陋,因为这些模块仅用于辅助测试过程,并不打算分发。另一方面,我可以按照pytest网站的integration advices使用
python setup.py test
来启动我的测试。