9

Im using unittest and it prints ".", "E" or "F" for "ok", "error" and "fail" after each test it does. How do I switch it off ? Im using Python 2.7 and these print come from the runner class which is built in. It sounds very tough to override the classes because it's all nested.

edit: I only want to take off the characters E . and F because they don't appear at the same time as some other log in my tests.

swan
  • 1,863
  • 3
  • 14
  • 8
  • Why do you want to do this? Surely it is helpful to see the progress of the tests? – Andrew Wilkinson Dec 15 '11 at 09:47
  • In my case it adds confusion as my tests are run in parallel, I print 1 log message and these test log messages arent synchronized with the corresponding "E" "F" or "." . – swan Dec 15 '11 at 17:12
  • How about running these tests in separate processes, which would separate output streams? – Janusz Lenar Feb 06 '13 at 14:03

3 Answers3

12

The output of unittest is written to the standard error stream, which you can pipe somewhere else. On a *nix box this would be possible like this:

python -m unittest some_module 2> /dev/null

On windows, this should look like this (thanks Karl Knechtel):

python -m unittest some_module 2> NUL

If you run the tests from python, you can simply replace the stderr stream like that:

import sys, os

sys.stderr = open(os.devnull, 'w')

... # do your testing here

sys.stderr = sys.__stderr__ # if you still need the stderr stream

Since you just want to turn off the updates for the ., F, E symbols, you could also create your own TestResult class by overriding the default one. In my case (Python 2.6) this would look like this:

import unittest

class MyTestResult(unittest._TextTestResult):
    def addSuccess(self, test):
        TestResult.addSuccess(self, test)
    def addError(self, test, err):
        TestResult.addError(self, test, err)
    def addFailure(self, test, err):
        TestResult.addFailure(self, test, err)

This effectively turns off the printing of the characters, but maintaining the default functionality.

Now we also need a new TestRunner class and override the _makeResult method:

class MyTestRunner(unittest.TextTestRunner):
    def _makeResult(self):
        return MyTestResult(self.stream, self.descriptions, self.verbosity)

With this runner you can now enjoy a log free testing.

Just a note: this is not possible from the command line, unfortunately.

Constantinius
  • 34,183
  • 8
  • 77
  • 85
  • `os.devnull` is a string. Did you mean `open(os.devnull, 'w')`? – jcollado Dec 15 '11 at 11:15
  • I need some output, I would like to unactivate only some trace. These characters . E and F are not written in an convenient way – swan Dec 16 '11 at 09:44
10

A bit late response, but someone may find it useful. You can turn . E and F off by setting verbosity level to 0:

testRunner = unittest.TextTestRunner( verbosity = 0 )

You will still have the final result and possible errors/exceptions at the end of tests in the stderr.

Tested in Python 2.4 and 2.7.

Karadur
  • 1,226
  • 9
  • 16
1

Depending the unittest framework you're using (standard, nose...), you have multiple way to decrease the verbosity:

python -m unittest -h
...
-q, --quiet      Minimal output
...
tito
  • 12,990
  • 1
  • 55
  • 75
  • 1
    the *".", "E" or "F"* are still printed in `-q` mode. – Constantinius Dec 15 '11 at 10:02
  • I dont run my tests via the command python -m unittest but instead I run them in parallel using this : http://code.activestate.com/recipes/391414/ – swan Dec 15 '11 at 17:15
  • In Python 3.6 the `-q` seems to suppress the letters, but I assume that is a bug that was fixed long ago. Full test failure outputs will still be printed. – Erhhung Apr 05 '17 at 00:55