Skip to content

Commit

Permalink
python debug with pytest-cov
Browse files Browse the repository at this point in the history
  • Loading branch information
copdips committed Apr 27, 2024
1 parent 3b75ba7 commit cd77685
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion docs/posts/2024/2024-02-22-debugging-in-python.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ categories:
comments: true
date:
created: 2024-02-22
modified: 2024-04-27
---

# Debugging in Python
Expand All @@ -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).

0 comments on commit cd77685

Please sign in to comment.