簡體   English   中英

當測試位於單獨的文件夾中時使用pytest

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

我的python文件夾結構如下

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

對於我的生活,我無法弄清楚我應該如何測試test_imagefuncs.pyimagefuncs.py中定義的函數

我無法在test_imagefuncs.py中導入libraries文件夾,因此我的測試代碼都看不到這些函數。

我正在使用python3,我想要實現的是從我的根存儲庫文件夾中執行py.test並執行所有測試而不會導致導入錯誤。

修改python路徑是實現這個目標的唯一方法嗎?

我想在不修改系統路徑或python路徑的情況下實現此目的

修改python路徑是實現這個目標的唯一方法嗎?

是。 無論如何,只使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM