Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you suppress traces for failed test cases using Nose?

I'm writing a test suit with nose, and would like failing cases to display an output like

"FAILED: is_even(5): Not even"

instead of the default output:

======================================================================
FAIL: seed_db.test_generator(5,)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/home/apurcell/tests/prism/seed_db.py", line 59, in is_even
    nose.tools.eq_(x % 2, 0, msg="Not even")
  File "/usr/local/lib/python2.7/dist-packages/nose/tools.py", line 31, in eq_
    assert a == b, msg or "%r != %r" % (a, b)
    AssertionError: Not even

----------------------------------------------------------------------

Is there an option for nose than can do this?

like image 393
andrewpurcell Avatar asked Oct 08 '22 06:10

andrewpurcell


2 Answers

If you want to change nose behavior, you should write a plug-in (see API documentation here). In your case, it sounds like you want to change the way that errors are reported, so you'd want to provide formatError() and formatFailure(). Probably you'd want to edit the exception message (to include the line number), and limit the size of the traceback.

like image 175
dbn Avatar answered Oct 12 '22 10:10

dbn


The following has output similar to but not identical to what you want (and it also changes the successful test output, which may not be what you want). It uses tap.py to output TAP (test anything protocol) instead of the usual unittest output.

nosetests --with-tap --tap-stream testcases-rsysflow.py

The output looks like:

bjb@rhino$ nosetests testcases-rrrr.py --with-tap --tap-stream
# TAP results for TestThing
ok 1 - test_create_and_get (testcases-rrrr.TestThing)
ok 2 - test_del_one (testcases-rrrr.TestThing)
ok 3 - test_get_all (testcases-rrrr.TestThing)
not ok 4 - test_get_not_exist (testcases-rrrr.TestThing)
ok 5 - test_get_wrong_indir (testcases-rrrr.TestThing)
ok 6 - test_replace_and_get (testcases-rrrr.TestThing)
ok 7 - test_set_should_fail (testcases-rrrr.TestThing)
1..7

With Test Anything Protocol (I'm speaking of the protocol, not this specific implementation) you can output diagnostic info after the dash on error - but I don't know how to do that with this implementation.

This implementation was handy because I just had to install tap.py (pip install tap.py) and put those two command-line args on the nosetest invocation of my unittest tests, and poof - TAP formatted output. Can plug this in to Jenkins now.

like image 32
Brenda J. Butler Avatar answered Oct 12 '22 11:10

Brenda J. Butler