Debugging Unittest Hangs Checklist | Generated by AI

Home 2025.10

Hangs in unittest are usually from deadlocks (threads/async), stuck I/O, subprocess/multiprocessing issues, or a test waiting on a network call. Here’s a tight checklist to see where it’s stuck and unstick it—works on Python 3.9–3.11.

1) Run unittest verbosely (and correctly)

# If xx.py calls unittest.main()
python xx.py -v

# Or use discovery (recommended)
python -m unittest -v
python -m unittest discover -v -s tests -p "test_*.py"

Note: python -v is interpreter import-verbose, not test-verbose. Use -m unittest -v for test names and progress.

2) Turn on faulthandler + dev mode (stack dumps on hang, stricter warnings)

# One-off
python -X faulthandler -X dev -u -m unittest -v
# Or via env
export PYTHONFAULTHANDLER=1
python -X dev -u -m unittest -v

3) Force a traceback when it seems stuck

Option A — from another terminal (Linux/macOS):

kill -SIGUSR1 <pid>  # with faulthandler enabled, dumps all thread stacks

Option B — add to your test bootstrap (top of xx.py):

import faulthandler, signal, sys
faulthandler.enable()
# Dump stacks on SIGUSR1:
faulthandler.register(signal.SIGUSR1, all_threads=True)
# Also auto-dump if it hangs > 120s:
faulthandler.dump_traceback_later(120, repeat=True)

4) Trace execution step-by-step (heavy but decisive)

python -m trace --trace xx.py
# or
python -m trace --trace -m unittest discover -v

You’ll see every line executed; stop when the output “freezes”—that’s your hang site.

5) Use the debugger right away

python -m pdb xx.py         # if xx.py calls unittest.main()
# Break on a suspected line:
# (Pdb) b mymodule.py:123
# (Pdb) c

For discovery runs, add import pdb; pdb.set_trace() at the suspected spot.

6) Common causes & quick fixes

7) Extra visibility

8) If it only hangs on one Python version


If you paste the traceback from faulthandler (or the last lines from trace --trace), I’ll point to the exact culprit and suggest a targeted fix.


Back

openai/gpt-5

Donate