58

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
  • 8,987
  • 11
  • 54
  • 85

5 Answers5

47

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.

agentofuser
  • 8,987
  • 11
  • 54
  • 85
  • 2
    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). – ken Dec 19 '11 at 18:21
  • 2
    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 – yakxxx Oct 04 '12 at 21:29
  • Oddly this seems to work for me with `print(...)`, but the output of pretty print with `pprint.pprint(...)` does not show in the console. – Taylor D. Edmiston May 12 '16 at 00:21
  • For me adding -s worked pretty fine, as I am advised not to play with the settings.py file. – Rishik Mani Aug 15 '19 at 13:38
  • didn't work for me why are so basic things so hard in python – java-addict301 Oct 06 '22 at 21:41
  • may try "./manage.py test --" without "-s" – apet May 11 '23 at 06:15
42

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.

Daniel Holmes
  • 1,952
  • 2
  • 17
  • 28
WisZhou
  • 1,369
  • 10
  • 8
26

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.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Sourabh Sinha
  • 551
  • 1
  • 5
  • 9
6

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

oivvio
  • 3,026
  • 1
  • 34
  • 41
4

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.

John Millikin
  • 197,344
  • 39
  • 212
  • 226
  • 1
    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! – agentofuser Aug 06 '09 at 00:29
  • 1
    The best thing is to post your own answer. – Daniel Roseman Aug 06 '09 at 11:19
  • 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. – Techdragon Aug 04 '13 at 10:54