How do I see stdout when running Django tests?
Asked Answered
M

5

61

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

Millman answered 5/8, 2009 at 23:34 Comment(2)
You should mark you own answer as correct since it is.Iridectomy
my Django ver 1.11 printss everything, without any -s or --no-inputFurl
M
48

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.

Millman answered 6/8, 2009 at 15:9 Comment(6)
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).Tybi
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 meCorinnecorinth
Oddly this seems to work for me with print(...), but the output of pretty print with pprint.pprint(...) does not show in the console.Afar
For me adding -s worked pretty fine, as I am advised not to play with the settings.py file.Matrass
didn't work for me why are so basic things so hard in pythonDissection
may try "./manage.py test --" without "-s"Capone
H
43

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.

Habituate answered 1/4, 2014 at 5:48 Comment(1)
This is the correct answer. The -- -s method causes undesired side effects.Marzi
Y
30

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.

Youngs answered 12/10, 2020 at 12:49 Comment(2)
This one is way more convenient, than playing with loggers, thanks!Magee
aka --verbosity (long name of the same argument)Castleman
B
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

Binder answered 11/10, 2017 at 11:47 Comment(0)
D
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.

Doridoria answered 5/8, 2009 at 23:51 Comment(3)
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!Millman
The best thing is to post your own answer.Margarettemargarida
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.Iridium

© 2022 - 2024 — McMap. All rights reserved.