使用pytest来测试时,测试文件位于不同文件夹中。

2024-06-28 10:51:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我的python文件夹结构如下

repository
  -libraries
     -image
        -imagefuncs.py
     -videos
        -videofuncs.py
     -text
        -textfuncs.py
  -docs
  -tests
     -test_imagefuncs.py

在我的一生中,我不知道该如何测试我在test_imagefuncs.pyimagefuncs.py中定义的函数

我无法在测试中导入库文件夹_imagefuncs.py所以这些函数对我的测试代码都不可见。在

我用的是python3,我只想实现它py.测试从我的根存储库文件夹中执行所有测试,而不会引发导入错误。在

修改python路径是实现这一目标的唯一方法吗?在

我希望在不修改系统路径或python路径的情况下实现这一点


Tags: 函数textpytestimage路径文件夹docs
1条回答
网友
1楼 · 发布于 2024-06-28 10:51:02

Is modifying the python path the only way to go about achieving this?

是的。不管怎样,只使用sys.path中的路径。在

modify PYTHONPATH是将路径附加到sys.path的一种方法,任何其他方法都将与向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

相关问题 更多 >