Test & Code in Python

Topics include automated testing, testing strategy, software engineering practices, packaging, Python, pytest, data science, TDD, continuous integration, and software methodologies. Also anything I think helps make the daily life of a software developer more fun and rewarding. Hosted by Brian Okken.



episode 164: Debugging Python Test Failures with pytest

An overview of the pytest flags that help with debugging.
From Chapter 13, Debugging Test Failures, of Python Testing with pytest, 2nd edition.

pytest includes quite a few command-line flags that are useful for debugging.

We talk about thes flags in this episode.

Flags for selecting which tests to run, in which order, and when to stop:

  • -lf / --last-failed: Runs just the tests that failed last.
  • -ff / --failed-failed: Runs all the tests, starting with the last failed.
  • -x / --exitfirst: Stops the tests session afterEd: after?Author: yep the first failure.
  • --maxfail=num: Stops the tests after num failures.
  • -nf / --new-first: Runs all the tests, ordered by file modification time.
  • --sw / --stepwise: Stops the tests at the first failure. Starts the tests at the last failure next time.
  • --sw-skip / --stepwise-skip: Same as --sw, but skips the first failure.

Flags to control pytest output:

  • -v / --verbose Displays all the test names, passing or failing.
  • --tb=[auto/long/short/line/native/no] Controls the traceback style.
  • -l / --showlocals Displays local variables alongside the stacktrace.

Flags to start a command-line debugger:

  • --pdb Starts an interactive debugging session at the point of failure.
  • --trace Starts the pdb source-code debugger immediately when running each test.
  • --pdbcls Uses alternatives to pdb, such as IPython’s debugger with –-pdbcls=IPython.terminal.debugger:TerminalPdb.

This list is also found in Chapter 13 of Python Testing with pytest, 2nd edition.
The chapter is "Debugging Test Failures" and covers way more than just debug flags, while walking through debugging 2 test failures.

Sponsored By:

  • PyCharm Professional: Try PyCharm Pro for 4 months and learn how PyCharm will save you time. Promo Code: TESTANDCODE22

Support Test & Code in Python


  • Python Testing with pytest — The fastest way to get up to speed on pytest.
  • all pytest flags in pytest 6.2.x


 2021-09-14  13m