从VisualStudio代码中的覆盖率调用Python单元测试中的触发器断点

2024-04-26 22:45:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用内置的unittest framework为我的Python 3.6模块编写单元测试。我使用Visual Studio Code(VS代码)作为我的IDE。我的测试是使用Python coverage tool调用的

coverage run --include=preprocessing/load_data.py preprocessing/test_load_data.py 
coverage report --show-missing

我从我的一个测试中得到一个错误,我怀疑它与测试环境的设置有关,因此我想在测试中触发一个断点。如果我直接从Python运行测试,我可以很容易地在VC代码的launch.json中设置一个配置,但是我看不到如何为覆盖率间接调用的代码设置配置。以下是我的尝试:

{
    "name": "Python: Use Coverage to Run Tests",
    "type": "python",
    "request": "launch",
    "module": "coverage",
    "args": [ 
        "run", "--include=preprocessing/load_data.py",
        "preprocessing/test_load_data.py"
    ],
    "console": "integratedTerminal"
}

这会运行程序,但不会触发断点

我还试图attach to a local script但添加

ptvsd.enable_attach(address=('localhost', 5678), redirect_output=True)
ptvsd.wait_for_attach()

导致覆盖率工具出错:

sys.settrace() should not be used when the debugger is being used.  
Call Location:  
    File "C:\Anaconda\envs\talia36\lib\site-packages\coverage\collector.py", line 278, in _installation_trace  
    sys.settrace(None)  

如何在VS代码下配置Python调试,以便从覆盖率和断点触发器调用单元测试


Tags: run代码pytestdataincludecoverage覆盖率