diff --git a/docs/posts/2024/2024-02-22-debugging-in-python.md b/docs/posts/2024/2024-02-22-debugging-in-python.md index 797b2daa..9e4bc58a 100644 --- a/docs/posts/2024/2024-02-22-debugging-in-python.md +++ b/docs/posts/2024/2024-02-22-debugging-in-python.md @@ -7,6 +7,7 @@ categories: comments: true date: created: 2024-02-22 + modified: 2024-04-27 --- # Debugging in Python @@ -29,4 +30,22 @@ Do not use: `export PYTHONBREAKPOINT='IPython.core.debugger.set_trace'`, it open ## Setting debugger in Pytest -Already mentioned in another post dedicated to [Python unittest](../2021/2021-06-12-python-unittest-cheet-sheet.md#pytest-pdb-pdbclsipythonterminaldebuggerterminalpdb). +**Conflict with pytest-cov**: If you use `pytest-cov`, you must use `pytest --no-cov` if you want to set breakpoints in your tests. Otherwise, pytest will just skip the breakpoints. This is because `pytest-cov` uses `sys.settrace()` to track code coverage, which is in conflict with breakpoints. There's no final solution for this issue, a workaround is to whether use coverage or whether use debug. In an IDE session (VSCode for e.g.), we can use `make test` to launch the test and generate coverage report with `pytest --cov`, and then use VScode's built-in debugger to set breakpoints and launch the tests in debug mode. To achieve this: + +1. `pip install pytest-cov` (must be installed because next VSCode settings depend on it). +2. Add the following to your VSCode user settings: + + ```json title="VSCode settings.json" hl_lines="9" + "python.testing.pytestArgs": [ + "tests", + "--color=yes", + // https://github.com/microsoft/vscode-python/issues/693#issuecomment-1356832568 + // must install pytest-cov as --no-cov is its param + // and if --no-cov is not specify, breakpoint doesn't work in pytest debug + // from VSCode UI to enable pytest debug from command line, need to add + // --no-cov to the Pytest args manually. + "--no-cov" + ], + ``` + +**Use IPython as debugger prompt**: [Python unittest](../2021/2021-06-12-python-unittest-cheet-sheet.md#pytest-pdb-pdbclsipythonterminaldebuggerterminalpdb).