Is there a way to have the Python debugger always pause before exiting?

Question:

I can set breakpoints, but often I just want to look at some variables after I run a test.

Is there a way to debug a pytest test in vscode and just have it always pause before exiting? Without me setting break points or writing pause statements?

Asked By: red888

||

Answers:

You can set specific final actions from within pytest tearDown methods:

pytest_runtest_teardown(item, nextitem)

pytest_runtest_makereport(item, call)

pytest_runtest_logreport(report)

pytest_exception_interact(call, report)

(More info on that here: https://docs.pytest.org/en/latest/reference/reference.html)

These methods can exist in a conftest.py file.

Eg:

def pytest_runtest_teardown(item):
    ...
    # Do something after every test that runs.

This existing Stack Overflow solution can give you a good start on possible implementations: https://stackoverflow.com/a/40446593/7058266

From there, you can have your tests do whatever you want… print variables, use breakpoints, etc.

Answered By: Michael Mintz