Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#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 Mariusz Felisiak, 3 years ago

Summary: manage.py test --pdb does not open a debugger when a subtest failsMake --pdb work with subTest().
Triage Stage: UnreviewedAccepted

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?

comment:2 by Abhyudai, 3 years ago

Owner: changed from nobody to Abhyudai
Status: newassigned

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.

Last edited 3 years ago by Abhyudai (previous) (diff)

comment:3 by Lucidiot, 3 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 Abhyudai, 3 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 Lucidiot, 3 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

comment:6 by Abhyudai, 3 years ago

Has patch: set

yes, it seems to work. thanks

pull-request

comment:7 by GitHub <noreply@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 7872971d:

Fixed #33149 -- Made test runner --pdb option work with subTest().

Thanks Lucidot for the report and Mariusz Felisiak for the initial
patch.

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In 44d11e5b:

[4.0.x] Fixed #33149 -- Made test runner --pdb option work with subTest().

Thanks Lucidot for the report and Mariusz Felisiak for the initial
patch.
Backport of 7872971dfbb818177168e64c24a933e48ce01206 from main

Note: See TracTickets for help on using tickets.
Back to Top