How do I see stdout when running Django tests?

32,537

Solution 1

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

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

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

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

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.

Share:
32,537
agentofuser
Author by

agentofuser

I'm a software engineer with 15+ years of experience. I have written front-end and back-end code used by millions of people while at Qype GmbH (acquired by Yelp). I'm currently specializing in React, IPFS, and functional programming. I have designed, implemented, and marketed products from the ground up, lead teams of engineers, and founded my own startups, one them funded in the Start-Up Chile program. I have worked remotely for many years, both on open source projects (3x participant in the Google Summer of Code program) and commercial ones. I'm a good communicator and team member. I'm self-directed and disciplined with my time. I studied Computer Engineering at Brazil's (often ranked as) #1 higher education and research institution (both in general and specifically in Computer Science): University of Campinas. I've lived for almost 2 years in the United States. I can read, write, and speak English fluently. Feel free to get in touch at [email protected]. More contact information at https://agentofuser.com.

Updated on July 09, 2022

Comments

  • agentofuser
    agentofuser almost 2 years

    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).

  • agentofuser
    agentofuser almost 15 years
    You were right. Running the django tests directly doesn't work because there's a lot of settings things up that the custom runner does, but I was able to trace the problem and fix it. Now this is my first question here, so is it ok to edit the question to include the solution like above? Thanks!
  • Daniel Roseman
    Daniel Roseman almost 15 years
    The best thing is to post your own answer.
  • ken
    ken over 12 years
    Because I am using the contrib.sites framework, I specify --settings for my tests. When I do that, -s flag functions as expected, and passing in another -- before the -s throws an OSError (No such file -s).
  • yakxxx
    yakxxx over 11 years
    Just for the record. If you have django_south installed in INSTALLED_APPS after django_nose it won't let you give options for nose (like -s). You need to put south before django_nose in INSTALLED_APPS. It was the issue with me
  • Techdragon
    Techdragon almost 11 years
    This is not always the case, I have encountered situations where you get this behaviour without nose. I don't have a solution but i think this answer is incomplete.
  • adam
    adam over 9 years
    This is the correct answer. The -- -s method causes undesired side effects.
  • Taylor D. Edmiston
    Taylor D. Edmiston almost 8 years
    Oddly this seems to work for me with print(...), but the output of pretty print with pprint.pprint(...) does not show in the console.
  • Rishik Mani
    Rishik Mani over 4 years
    For me adding -s worked pretty fine, as I am advised not to play with the settings.py file.
  • Alveona
    Alveona over 3 years
    This one is way more convenient, than playing with loggers, thanks!
  • Neil C. Obremski
    Neil C. Obremski over 2 years
    aka --verbosity (long name of the same argument)