Python 当测试位于单独的文件夹中时使用pytest
我的python文件夹结构如下Python 当测试位于单独的文件夹中时使用pytest,python,python-3.x,unit-testing,pytest,Python,Python 3.x,Unit Testing,Pytest,我的python文件夹结构如下 repository -libraries -image -imagefuncs.py -videos -videofuncs.py -text -textfuncs.py -docs -tests -test_imagefuncs.py 就我的一生而言,我无法理解如何测试test_imagefuncs.pyintest_imagefuncs.py 我无法导入te
repository
-libraries
-image
-imagefuncs.py
-videos
-videofuncs.py
-text
-textfuncs.py
-docs
-tests
-test_imagefuncs.py
就我的一生而言,我无法理解如何测试test_imagefuncs.py
intest_imagefuncs.py
我无法导入test_imagefuncs.py中的libraries文件夹,因此这些函数对我的测试代码都不可见
我使用的是python3,我想要实现的就是从我的根存储库文件夹中执行py.test,并让我的所有测试在不抛出导入错误的情况下执行
修改python路径是实现这一目标的唯一途径吗
我希望在不修改系统路径或python路径的情况下实现这一点
修改python路径是实现这一目标的唯一途径吗
对。无论如何,只能使用sys.path
中的路径
修改PYTHONPATH
是只将路径附加到sys.path
的一种方法,任何其他方法都将执行与将路径添加到sys.path
相同的操作。具体的变化实际上取决于项目
e、 g
编写
setup.py
并安装模块进行测试。当设置一个新项目时,我通常会遵循,或者您可以查看cookiecutter项目。在存储库中添加一个空的conftest.py
的可能重复项,这应该已经足以调整sys.path
。查看链接问题中的答案以了解更多详细信息。
export PYTHONPATH='/opt/mybuild'
[tmp]$ python3.6 -m site
sys.path = [
'/tmp',
'/opt/mybuild',
'/usr/lib64/python36.zip',
'/usr/lib64/python3.6',
'/usr/lib64/python3.6/lib-dynload',
'/home/joe/.local/lib/python3.6/site-packages',
'/usr/lib64/python3.6/site-packages',
'/usr/lib/python3.6/site-packages',
]
USER_BASE: '/home/joe/.local' (exists)
USER_SITE: '/home/joe/.local/lib/python3.6/site-packages' (exists)
ENABLE_USER_SITE: True