#33149 closed Bug (fixed)
Make --pdb work with subTest().
| Reported by: | Lucidiot | Owned by: | Abhyudai |
|---|---|---|---|
| Component: | Testing framework | Version: | 3.2 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
With a test case that contains a TestCase.subTest call, a failure or error of a subtest does not cause pdb or ipdb to open when running manage.py test --pdb.
from django.test import TestCase class TestSomething(TestCase): def test_something(self): with self.subTest(a=1): raise ZeroDivisionError("oh snap!")
λ ./manage.py test --pdb
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
...
======================================================================
ERROR: test_something (demo.app.tests.test_something.TestSomething) (a=1)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/.../tests/test_something.py", line 6, in test_something
raise ZeroDivisionError("oh snap!")
ZeroDivisionError: oh snap!
----------------------------------------------------------------------
Ran 1 tests in 0.154s
FAILED (errors=1)
Destroying test database for alias 'default'...
It seems that CPython has some special handling code for subtest results that does not use TestResult.addError or TestResult.addFailure, and the PDBDebugResult only implements those. Note that --debug-sql does work with subtests because the DebugSQLTextTestResult supports subtests.
Change History (8)
comment:1 by , 4 years ago
| Summary: | manage.py test --pdb does not open a debugger when a subtest fails → Make --pdb work with subTest(). |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:2 by , 4 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
for what it is worth, only adding the call to debug() didn't work, at least on my end. i will try to look more into this.
comment:3 by , 4 years ago
Sorry for the delay! Adding a self.debug call as in the code block above does seem to work on my machine, at least when hastily adding it by editing my site-packages (don't try this at home). I could maybe provide a patch, and Abhyudai can test it to see if something is truly missing?
comment:4 by , 4 years ago
Just to be on the same page, this is the diff that is being talked about, no?
diff --git a/django/test/runner.py b/django/test/runner.py
index 225bc19b09..9ea67ce4b6 100644
--- a/django/test/runner.py
+++ b/django/test/runner.py
@@ -275,6 +275,7 @@ failure and get a correct traceback.
self.check_picklable(test, err)
self.check_subtest_picklable(test, subtest)
self.events.append(('addSubTest', self.test_index, subtest, err))
+ self.debug(err)
super().addSubTest(test, subtest, err)
def addSuccess(self, test):
comment:5 by , 4 years ago
No, I added the method to the PDBDebugResult directly:
diff --git a/django/test/runner.py b/django/test/runner.py
index 225bc19b09..b4dd974d57 100644
--- a/django/test/runner.py
+++ b/django/test/runner.py
@@ -107,6 +107,10 @@ class PDBDebugResult(unittest.TextTestResult):
super().addFailure(test, err)
self.debug(err)
+ def addSubTest(self, test, subtest, err):
+ super().addSubTest(test, subtest, err)
+ self.debug(err)
+
def debug(self, error):
self._restoreStdout()
self.buffer = False
Thanks for the report. It seems it's enough to call
debug()🤔, e.g.def addSubTest(self, test, subtest, err): if err is not None: self.debug(err) super().addSubTest(test, subtest, err)Would you like to provide a patch?