简体   繁体   English

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

[英]Using pytest when tests are in a separate folder

my python folder structure is as follows 我的python文件夹结构如下

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

For the life of me i cannot figure out how I am supposed to test my functions defined in imagefuncs.py in test_imagefuncs.py 对于我的生活,我无法弄清楚我应该如何测试test_imagefuncs.pyimagefuncs.py中定义的函数

I cannot import the libraries folder in test_imagefuncs.py so none of those functions are visible to my testing code. 我无法在test_imagefuncs.py中导入libraries文件夹,因此我的测试代码都看不到这些函数。

I'm using python3 and all i want to achieve is do py.test from my root repository folder and have all my tests execute without throwing import errors. 我正在使用python3,我想要实现的是从我的根存储库文件夹中执行py.test并执行所有测试而不会导致导入错误。

Is modifying the python path the only way to go about achieving this? 修改python路径是实现这个目标的唯一方法吗?

I want to achieve this without modifying the system path or python path 我想在不修改系统路径或python路径的情况下实现此目的

Is modifying the python path the only way to go about achieving this? 修改python路径是实现这个目标的唯一方法吗?

Yes. 是。 One way or other, only paths in sys.path will be used. 无论如何,只使用sys.path路径。

modify PYTHONPATH is one way to just append paths to sys.path , any other way will do the same thing as adding paths to sys.path . 修改PYTHONPATH是将路径追加到sys.path一种方法,任何其他方式都可以像添加sys.path路径一样。 The specific variation really depends on project. 具体的变化实际上取决于项目。

eg 例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM