How do I see stdout when running Django tests?

PythonDjangoDebuggingTestingStdout

Python Problem Overview


When I run tests with ./manage.py test, whatever I send to the standard output through print doesn't show. When tests fail, I see an "stdout" block per failed test, so I guess Django traps it (but doesn't show it when tests pass).

Python Solutions


Solution 1 - Python

Checked TEST_RUNNER in settings.py, it's using a project-specific runner that calls out to Nose. Nose has the -s option to stop it from capturing stdout, but if I run:

./manage.py test -s

manage.py captures it first and throws a "no such option" error. The help for manage.py doesn't mention this, but I found that if I run:

./manage.py test -- -s

it ignores the -s and lets me capture it on the custom runner's side, passing it to Nose without a problem.

Solution 2 - Python

Yeah, this issue is caused by NoseTestSuiteRunner. Adding -- -s is tricky and not the best solution. Try to add the following lines in the settings.py:

NOSE_ARGS = ['--nocapture',
             '--nologcapture',]

This solved my problems.

Solution 3 - Python

There are several levels of verbosity available which affects the amount of details we see: You can try:

python manage.py test -v 2

Other levels available are:

  • -v 0 : Least amount of details

  • -v 1: Default

  • -v 2 : More details e.g. print statements included.

Solution 4 - Python

Using current versions of all the relevant packages (Django==1.11.2, django-nose==1.4.5 and nose==1.3.7) it is sufficient to add the --nocapture flag when running your tests. Thus a simple

./manage.py test --nocapture

will suffice.

Granted of course that you have

TEST_RUNNER = "django_nose.NoseTestSuiteRunner"

in your settings.py

Solution 5 - Python

You probably have some intermediate test runner, such as Nose, intercepting and storing stdout. Try either running the Django tests directly, or write to stderr instead.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionagentofuserView Question on Stackoverflow
Solution 1 - PythonagentofuserView Answer on Stackoverflow
Solution 2 - PythonWisZhouView Answer on Stackoverflow
Solution 3 - PythonSourabh SinhaView Answer on Stackoverflow
Solution 4 - PythonoivvioView Answer on Stackoverflow
Solution 5 - PythonJohn MillikinView Answer on Stackoverflow