【发布时间】:2021-09-26 00:06:53
【问题描述】:
我有一个 python 库(模块),其中包含多个模块和相关测试,遵循相当标准的结构
project_root
library_folder
__init__.py
module_A
__init__.py
sub_module_AA
__init__.py
file_AAA.py
file_AA.py
file_AB.py
file_AC.py
module_B
__init__.py
file_BA.py
file_BB.py
module_C
__init__.py
file_CA.py
file_CB.py
file_CC.py
...
tests
module_A
test_fileAA.py
...
module_B
test_fileBA.py
...
pyproject.toml
...
目前我正在做我称之为穴居人调试的事情,方法是将import pdb; pdb.set_trace() 放入有缺陷的文件中,然后使用pytest path/to/test_fileXY.py 调用相应的测试并等待断点。我想将此设置移动到 VSCode,以便我可以简单地点击 F5 并运行相应的测试。
我的问题是我需要调用pytest {workspaceDir}/tests/{submodule_path}/test_{filename}.py 来运行测试。我可以通过
{
"configurations": [
{
"name": "Python: Run Current File's Tests",
"type": "python",
"request": "launch",
"module": "pytest",
"console": "integratedTerminal",
"args": [
"${workspaceFolder}/tests/${relativeFileDirname}/test_${fileBasename}"
]
}
]
}
但是,这会产生{workspaceDir}/tests/library_folder/{submodule_path}/test_{filename}.py(请注意路径中的附加library_folder。如何正确执行此操作,即如何创建对活动文件的相应测试的引用?
【问题讨论】:
标签: python debugging visual-studio-code pytest