我對 Pytest 有這個問題,其中單元測驗從 PyCharm 運行良好,但當我通過管道“python -m pytest”運行時卻沒有。
下面是我的專案結構:
Common
|_____configuration.py
|
Services
|
|-----ServiceA
| |
| |___src
| | |___utils
| | |__ __init__.py
| | |__ helper1.py
| |__ helper2.py
| |___Test
| |___utils
| |__ __init.py
| |__ test1.py
|
|-----ServiceB
|
|
在 helper1 中,我的代碼為
from Common import configuration
當我通過 Pycharm 運行時,測驗運行得非常好,因為它決議了所有路徑,但是當我通過管道運行它時,通過 cmd 運行時出現以下錯誤,
ModuleNotFoundError: No module named 'Common'
任何人都可以幫助如何解決這個問題。TIA
uj5u.com熱心網友回復:
在pytest >= 7.0.0中,您可以使用 Pytest 的pythonpath配置選項注冊額外的路徑。路徑值應該相對于rootdir。所以它可能是Common或../Common或者甚至帶有更多父級別的前綴。
如果您使用pyproject.toml:
[tool.pytest.ini_options]
pythonpath = ["Common"]
如果您使用pytest.ini代替:
[pytest]
pythonpath = Common
uj5u.com熱心網友回復:
在 test init .py 中添加了 common 的路徑并解決了問題
sys.path.append('..\..\Common')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/528206.html
上一篇:Vitest模擬模塊僅在一項測驗中起作用,而在其他測驗中使用實際功能
下一篇:反應測驗庫用戶事件鍵盤不起作用
