Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当测试位于单独的文件夹中时使用pytest_Python_Python 3.x_Unit Testing_Pytest - Fatal编程技术网

Python 当测试位于单独的文件夹中时使用pytest

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

我的python文件夹结构如下

repository
  -libraries
     -image
        -imagefuncs.py
     -videos
        -videofuncs.py
     -text
        -textfuncs.py
  -docs
  -tests
     -test_imagefuncs.py
就我的一生而言,我无法理解如何测试
test_imagefuncs.py
in
test_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