Opened 8 years ago

Closed 8 years ago

#27008 closed New feature (fixed)

Add test --debug option

Reported by: Chris Jerdonek Owned by: Chris Jerdonek
Component: Testing framework Version: 1.10
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


This ticket is to allow DEBUG to be enabled when running tests (or when running a subset of tests if passing test labels on the command-line).

Enabling DEBUG is an easy way to help troubleshoot test failures in certain situations (e.g. if your code does extra checks or shows more info when DEBUG is turned on).

Currently, Django's DiscoverRunner hardcodes settings.DEBUG = False inside setup_test_environment() (here is a direct link to the code):

def setup_test_environment(self, **kwargs):
    settings.DEBUG = False

One option would be to add a test option like --debug to Django's test management command.

Change History (6)

comment:1 by Tim Graham, 8 years ago

I'm not sure about this. While I've had this need from time to time, I've found it easy enough to solve by temporarily adding an with with self.settings(DEBUG=True): context manager to the test method in question. Do you have a use case that involves debugging more than a couple tests?

Setting settings.DEBUG = True in setup_test_environment() results in 70 failures/errors in Django's test suite. Adding an easy way to run the test suite with that option leaves me wondering whether or not it would become expected that everyone should aim to have their test suite passing with both values of DEBUG.

comment:2 by Chris Jerdonek, 8 years ago

One use case is the following. Say you have some views that return JSON. And say they're set up so that if DEBUG is enabled and there is a server-side error, the JSON includes detailed exception info (analogous to the HTML debug view). Otherwise, only a failure status code is returned. And further say you have tests that involve calling these views and checking the response.

If there is a server-side error when calling these views during a test, the absence of any exception info can make the test hard to troubleshoot. I found that having to edit the code manually each time there was such an error to change the settings for that test was unnecessarily time-consuming.

Another use case is if your code includes additional asserts when running in DEBUG mode.

Regarding an expectation that tests pass in DEBUG, I think you could simply document that it not be necessary. Or perhaps only allow the --debug option when test labels are also passed to the command (i.e. disallow the option when running all tests). In my experience, I've only had a need to use it when troubleshooting individual tests.

comment:3 by Tim Graham, 8 years ago

Summary: allow DEBUG to be enabled when running testsAdd test --debug option
Triage Stage: UnreviewedAccepted

Okay. Hopefully this won't open a can of worms. ;-)

comment:4 by Chris Jerdonek, 8 years ago

Owner: changed from nobody to Chris Jerdonek
Status: newassigned

comment:5 by Chris Jerdonek, 8 years ago

Has patch: set

I added a pull request for this here.

comment:6 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 5890b16:

Fixed #27008 -- Added --debug-mode option to DiscoverRunner.

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