gpt4 book ai didi

python - 当测试在单独的文件夹中时使用 pytest

转载 作者:行者123 更新时间:2023-11-28 18:08:54 24 4
gpt4 key购买 nike

我的python文件夹结构如下

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

对于我的生活,我无法弄清楚我应该如何在 test_imagefuncs.py

中测试 imagefuncs.py 中定义的函数

我无法导入 test_imagefuncs.py 中的库文件夹,因此我的测试代码看不到这些函数。

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

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

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

最佳答案

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

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

修改 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

关于python - 当测试在单独的文件夹中时使用 pytest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51915625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com