5

I have a lot of unit tests in Django and if a test fails, I often need to see the logs (in the console, if possible). I can't really use the log file because it gets really messy.
What I do right now is: activate console logging in the settings.py, and only run one specific test. I was hoping that there is a more convenient way to do it. I only want to see what was being logged for the failing tests and not the complete log.

Edit: even though an answer was posted — and I have accepted it — I'm not quite content. I want to see only the logging output of failing tests. I was looking for a solution like that which PHPUnit provides. It captures the complete output (for logging + print) and only writes it to stdout if the test fails.

4
  • Are you adding some "print"s in the code? Commented Jun 29, 2014 at 13:26
  • @brunofitas No, I use python's log module. Commented Jun 29, 2014 at 13:29
  • I usually use the "print" command and it works fine when running locally. The prints and the errors are displayed on the server window and usually I don't need anything else. Commented Jun 29, 2014 at 13:35
  • @brunofitas Well, if I used prints in my code all the time, I would see the output of these prints for all unit tests as well. In addition, prints are thrown away by MOD_WSGI, so, I can't switch to print statements. I wound't also be able to capture the SQL log that way (which can also be very helpful). Commented Jun 29, 2014 at 13:45

3 Answers 3

2

Well, I just figured out how to fix it.
Apparently there is a -b option that "buffers the output" unless the test fails when running a unit tests. It is very easy to use if your tests.py file executes unittest.main(). Just type python tests.py -b and the complete output will be buffered unless the test fails.

...........F
Stdout:
OK :)
OK :)

======================================================================
FAIL: testStr (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests.py", line 93, in testStr
    self.fail()
AssertionError: None

Stdout:
OK :)
OK :)

----------------------------------------------------------------------
Ran 12 tests in 0.005s

In this case, every test printed something to stdout but only the very last one (the failing one was shown). Since the log can also be printed to the console (see @Thomas's answer), you will only see relevant logs.

Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't seem to be supported by django as of february 16: code.djangoproject.com/ticket/27430
Django has --buffer apparently as of version 3.2... If you are using the default test runner, DiscoverRunner. github.com/django/django/pull/12868/commits/…
1

Set the root logger to use the console handler only when running tests. Easiest way to detect is to see if "test" is the second argv parameter. Make sure all interesting loggers have "propagate": True so that they forward their logs to the root logger.

# settings.py
import sys

if sys.argv[1] == 'test':
    LOGGING['root'] = {
        'handlers': ['console'],
        'level': 'DEBUG',
    }

3 Comments

That actives logging for all test. But I only want to see the log for all failing tests. Using this code, I get way too much log output and I can hardly see which logs fail.
So, just rerun the failing tests and record log output.
That's additional work, whereas there's clearly an automatic solution the asker is after. (At least there should be; there are test frameworks where it's simple to achieve.) An automatic solution to only print logs for failed tests, without spending time to kick off any reruns.
0

The quick and dirty way to access your log output on the console is to monkey patch logging.. To overwrite logging.info for example:

def monkey_print(*args):
   print args
setattr(logging, 'info', monkey_print)

Then just delete it when you're done. Quick and dirty. It works with unittest too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.