【问题标题】:Launch test file (pytest) as debug for active file in VSCode启动测试文件(pytest)作为 VSCode 中活动文件的调试
【发布时间】: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


    【解决方案1】:

    python 扩展内置了这个功能:

    【讨论】:

    • 我知道这个功能,它也很接近,但对我不起作用。 Python: Debug All Tests 选项对我不起作用,因为我的测试套件太大,导致调试速度非常慢。通过 CodeLense 运行单个测试是可行的,但位于测试上方意味着我需要每次都去挖掘它们并切换选项卡。我目前正在通过侧边栏菜单尝试它,但是我可以进行一些小改动并按下“我是否在脚上开枪?(运行本地测试)”按钮的设置似乎更加优雅。
    猜你喜欢
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 2022-11-24
    • 2019-04-20
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多