我正试图使用pytest为repo建立一个单元测试框架,当时我在一个旧分支中工作。我克隆了新分支,并将文件复制到新分支。调用pytest时,测试运行良好,但它们引用了旧项目中的文件(即使它们是作为包导入到当前项目中的,而不是通过直接的文件路径(。
这是用于运行pytest:的行/命令
(venv) C:Program FilesPython-3.8.2pycharm_projectsfnma-erm01-devl-edl-hao-repo_21.3>python -m pytest
为了解决这个问题,我在本地删除了旧的分支,现在所有的测试都不起作用,而且它不将任何项目目录识别为有效的模块。
Stacktrace:
========================================================================================================================================================================= test session starts =========================================================================================================================================================================
platform win32 -- Python 3.8.2, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: C:Program FilesPython-3.8.2pycharm_projectsERM_EDL_HAO_21.3_Branch, configfile: pytest.ini
plugins: mock-3.5.1
collecting ... sys path ['
C:\Program Files\Python-3.8.2\pycharm_projects\ERM_EDL_HAO_21.3_Branch\SourceCode\application\deploy\app\hao',
'C:\Program Files\Python-3.8.2\pycharm_projects\ERM_EDL_HAO_21.3_Branch',
'C:\Progra~1\Python-3.8.2\pycharm_projects\fnma-erm01-devl-edl-hao-repo\SourceCode\application\deploy\app\hao\lambda',
'C:\Progra~1\Python-3.8.2\pycharm_projects\fnma-erm01-devl-edl-hao-repo\SourceCode\application\deploy\app\hao\lambda\tests',
'C:\Progra~1\Python-3.8.2\pycharm_projects\fnma-erm01-devl-edl-hao-repo\SourceCode\application\deploy\app\hao\lambda\utils',
'C:\Program Files\Python-3.8.2\pycharm_projects\ERM_EDL_HAO_21.3_Branch',
'C:\Program Files\Python-3.8.2\python38.zip',
'C:\Program Files\Python-3.8.2\DLLs',
'C:\Program Files\Python-3.8.2\lib',
'C:\Program Files\Python-3.8.2',
'C:\Program Files\Python-3.8.2\pycharm_projects\ERM_EDL_HAO_21.3_Branch\venv',
'C:\Program Files\Python-3.8.2\pycharm_projects\ERM_EDL_HAO_21.3_Branch\venv\lib\site-packages',
'C:\Users\g3uj2m\AppData\Roaming\Python\Python38\site-packages',
'C:\Users\g3uj2m\AppData\Roaming\Python\Python38\site-packages\win32',
'C:\Users\g3uj2m\AppData\Roaming\Python\Python38\site-packages\win32\lib',
'C:\Users\g3uj2m\AppData\Roaming\Python\Python38\site-packages\Pythonwin',
'C:\Program Files\Python-3.8.2\lib\site-packages']
collected 0 items / 16 errors
=============================================================================================================================================================================== ERRORS ================================================================================================================================================================================
_______________________________________________________________________________________________________________________ ERROR collecting SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_amtm_upstream_notification.py _______________________________________________________________________________________________________________________
ImportError while importing test module 'C:Program FilesPython-3.8.2pycharm_projectsERM_EDL_HAO_21.3_BranchSourceCodeapplicationdeployapphaolambdatestshandlersunittest_hao_amtm_upstream_notification.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
....libimportlib__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
SourceCodeapplicationdeployapphaolambdatestshandlersunittest_hao_amtm_upstream_notification.py:8: in <module>
from hao_amtm_upstream_notification import lambda_function
E ModuleNotFoundError: No module named 'hao_amtm_upstream_notification'
# ... ^ that part more or less repeated 15 more times ...
======================================================================================================================================================================= short test summary info =======================================================================================================================================================================
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_amtm_upstream_notification.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_batchjob_status_check.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_batchjob_submit.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_capture_hedge_status.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_ccfa_upstream_notification.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_dynamodb_status_check.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_glue_function.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_irdb_upstream_notification.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_krd_upstream_notification.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_ln_pool_max.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_s3_data_availability_check.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_stepfn_execution_status.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_swap_upstream_notification.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_trigger_sns_msg.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_verify_upstream_data_availability.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_workflow_trigger.py
文件树:
pytest.ini
SOURCECODE
├───application
│ └───deploy
│ └───app
│ └───hao
│ ├───lambda
│ │ ├───hao_amtm_upstream_notification
│ │ │ lambda_function.py
│ │ │
│ │ ├───hao_batchjob_status_check
│ │ │ lambda_function.py
│ │ │
│ │ ├───hao_batchjob_submit
│ │ │ lambda_function.py
│ │ │
... etc ...
│ │ │
│ │ ├───tests
│ │ │ conftest.py
│ │ │
│ │ │ ├───handlers
│ │ │ │ unittest_hao_amtm_upstream_notification.py
│ │ │ │ unittest_hao_batchjob_status_check.py
│ │ │ │ unittest_hao_batchjob_submit.py
│ │ │ │ ... etc ...
pytest.ini文件:
# pytest.ini
[pytest]
python_files = unittest_*.py
以下是我已经尝试/检查过的内容:
- 所有必要的文件夹都已标记为";源根">
- 我已经将
__init__.py
添加到所有必要的文件夹中 - 我已检查以确保
__init__.py
未被识别为文本文件 - 我已经运行了";使缓存无效/重新启动"选项
- 我已经确保项目的解释器与当前项目和目录保持一致
它不允许我包含屏幕截图,但项目结构应该不是问题,因为它在旧分支中运行得非常好。(旧的分支结构与新的分支结构相同,因此从一个分支复制到另一个分支时不应该有参考问题(
编辑1:我使用的是pytest.ini
配置文件,其中唯一指定的是用于标识测试文件的自定义前缀。我为新项目创建了一个新的venv,并且从PyCharm中的内置终端调用pytest,所以venv是活动的。
编辑2:缩短了堆栈跟踪,使其更易于阅读,并添加了文件树和pytest.ini
文件的相关部分。
好吧,Pycharm在复制到新项目后似乎无法检测到源根的更改,因为除了项目名称之外,路径是相同的。奇怪的是,如果我创建了一个测试配置并运行Pycharm内置的pytest运行程序,它就会工作。话虽如此,我还没能让控制台使用正确的PYTHONPATH,但我找到了一个变通方法。我没有使用python -m pytest
通过控制台调用pytest,而是创建了一个强制PYTHONPATH更新的脚本。
新命令:
python run_unittests.py
新脚本:
import pytest, os, sys
project_dir = os.path.dirname(os.path.abspath(__file__))
base_test_dir = os.path.join(project_dir, 'SourceCode', 'application', 'deploy', 'app', 'hao', 'lambda')
test_dirs = [
os.path.join(base_test_dir, 'tests'),
os.path.join(base_test_dir, 'handlers'),
os.path.join(base_test_dir, 'utils'),
base_test_dir
]
for i in test_dirs:
if i not in sys.path:
sys.path.append(i)
test_args = [a for a in sys.argv if a != 'run_unittests.py']
pytest.main([base_test_dir]+test_args)
感谢@bad_code的帮助。