TL;DR:如何設定我的 GitLab 測驗管道,以便測驗也可以在 VS Code 上本地運行?
我對 GitLab 管道非常陌生,所以如果問題是業余的,請原諒我。我有一個在線設定的 GitLab 存盤庫,并且我正在使用 VS Code 進行本地開發。我創建了一個新管道,我想確保我的所有單元測驗(用 PyTest 撰寫)在我提交時運行。
問題是,即使我在setup.py兩個地方都使用相同的檔案(顯然),我無法同時讓 VS Code 測驗和 GitLab 管道測驗作業。問題是,我正在為我的測驗進行匯入,如果我匯入喜歡
...
from external_workforce import misc_tools
# I want to test functions in this misc_tools module
...
然后它適用于 GitLab,但不適用于 VS Code,因為 VS Code 在我進行測驗發現時會出錯,即:ModuleNotFoundError: No module named 'external_workforce'. 但是,這適用于 GitLab。但是,如果我像這樣匯入(在我的test_tools.py檔案中,請參見下面的位置):
...
from hr_datapool.external_workforce import misc_tools
...
它在 VS Code 中作業,但現在 GitLab 對我說ModuleNotFoundError: No module named 'hr_datapool'.
我認為相關資訊可能如下,如果需要更多資訊,請詢問更多!
我的檔案結構是:
.
|__ requirements.txt
setup.py
hr_datapool
|__ external_workforce
|__ __init__.py
misc_tools.py
tests
|__ test_tools.py
|__ other_module
...
在我的管道編輯器(.gitlab-ci.yml檔案)中,我有:
image: python:3.9.7
variables:
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
cache:
paths:
- .cache/pip
- venv/
before_script:
- python --version # For debugging
- pip install virtualenv
- virtualenv venv
- source venv/bin/activate
- pip install -r requirements.txt
test:
script:
- pytest --pyargs hr_datapool #- python setup.py test
run:
script:
- python setup.py bdist_wheel
artifacts:
paths:
- dist/*.whl
最后,我setup.py的是:
import re
from unittest import removeResult
from setuptools import setup, find_packages
with open('requirements.txt') as f:
requirements = f.read().splitlines()
for req in ['wheel', 'bar']:
requirements.append(req)
setup(
name='hr-datapool',
version='0.1',
...
packages=find_packages(),
install_requires=requirements,
)
基本上,問題是:如何設定我的 GitLab 測驗管道,以便測驗也可以在 VS Code 上本地運行?謝謝!
更新:
添加來自 VS Code 的完整跟蹤:
> conda run -n base --no-capture-output --live-stream python ~/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir "." -s --cache-clear hr_datapool
cwd: .
[ERROR 2022-2-23 9:2:4.500]: Error discovering pytest tests:
[r [Error]: ============================= test session starts ==============================
platform darwin -- Python 3.9.7, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /Users/myuser/Documents/myfolder
plugins: anyio-2.2.0
collected 0 items / 1 error
==================================== ERRORS ====================================
_____ ERROR collecting hr_datapool/external_workforce/tests/test_tools.py ______
ImportError while importing test module '/Users/myuser/Documents/myfolder/hr_datapool/external_workforce/tests/test_tools.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../opt/anaconda3/lib/python3.9/importlib/__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
hr_datapool/external_workforce/tests/test_tools.py:2: in <module>
from external_workforce import misc_tools
E ModuleNotFoundError: No module named 'external_workforce'
=========================== short test summary info ============================
ERROR hr_datapool/external_workforce/tests/test_tools.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
===================== no tests collected, 1 error in 0.08s =====================
Traceback (most recent call last):
File "/Users/myuser/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/get_output_via_markers.py", line 26, in <module>
runpy.run_path(module, run_name="__main__")
File "/Users/myuser/opt/anaconda3/lib/python3.9/runpy.py", line 268, in run_path
return _run_module_code(code, init_globals, run_name,
File "/Users/myuser/opt/anaconda3/lib/python3.9/runpy.py", line 97, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "/Users/myuser/opt/anaconda3/lib/python3.9/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/Users/myuser/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/testing_tools/run_adapter.py", line 22, in <module>
main(tool, cmd, subargs, toolargs)
File "/Users/myuser/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/testing_tools/adapter/__main__.py", line 100, in main
parents, result = run(toolargs, **subargs)
File "/Users/myuser/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 44, in discover
raise Exception("pytest discovery failed (exit code {})".format(ec))
Exception: pytest discovery failed (exit code 2)
ERROR conda.cli.main_run:execute(33): Subprocess for 'conda run ['python', '/Users/myuser/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/get_output_via_markers.py', '/Users/A111086670/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/testing_tools/run_adapter.py', 'discover', 'pytest', '--', '--rootdir', '/Users/myuser/Documents/myfolder', '-s', '--cache-clear', 'hr_datapool']' command failed. (See above for error)
at ChildProcess.<anonymous> (/Users/myuser/.vscode/extensions/ms-python.python-2022.2.1924087327/out/client/extension.js:32:39235)
at Object.onceWrapper (events.js:422:26)
at ChildProcess.emit (events.js:315:20)
at maybeClose (internal/child_process.js:1048:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)]
uj5u.com熱心網友回復:
PYTHONPATH導致問題。
作為 external_workforce父檔案夾路徑 ->使用 GitLab 時hr_datapool的路徑。使用 VSCode 時的父檔案夾路徑。PYTHONPATH hr_datapoolPYTHONPATH
您是否在 VSCode 上的終端中運行測驗?你有沒有在 settings.json 檔案中添加這個?
"terminal.integrated.env.windows": {
"PYTHONPATH": "${workspaceFolder};"
},
然后就可以pytest在終端上執行VSCode了。但是您沒有在 GitLab 中配置它而不是添加hr-datapool(- pytest --pyargs hr_datapool或setup( name='hr-datapool',),因此您將收到錯誤訊息。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/447685.html
