Reputation: 13
Hi so I am wondering if there is a way to fix the output of nosetests assertion failues. I have a simple script named "t.py":
import unittest
from nose.tools import assert_equal
class x(unittest.TestCase):
"""
Testing
"""
def test(self):
assert_equal(1, 2)
If I run it with the command "nosetests t" then I get the following.
AssertionError: 1 != 2
'1 != 2' = '%s != %s' % (safe_repr(1), safe_repr(2))
'1 != 2' = self._formatMessage('1 != 2', '1 != 2')
>> raise self.failureException('1 != 2')
Instead of the output I get when running "python -m unittest t"
AssertionError: 1 != 2
Does anyone know how I would make the nosetests output match the unittest output? This would clutter up results when running many tests quite a bit.
This is with python 2.7.3, nose==1.3.0, inside a virtualenv inside an ubuntu VM.
EDIT:
I have no problem with Nose trying to give more info. I have a growing test suite and extra info may be helpful. Its just that it's giving fairly useless info. I really don't need to see the same info multiple times. It's very difficult to see the problem if there is more than one test or especially if there is an additional message such as the following :
assert_equal(1,2, "Something is messed up before this assertion. Now here is a bunch of info to help with debugging. This is really quite a lot of info you know. Yes it is very long isn't it?")
Then I get this:
assert_equal(1,2, "Something is messed up before this assertion. Now here is a bunch of info to help with debugging. This is really quite a lot of info you know. Yes it is very long isnt it?")
AssertionError: Something is messed up before this assertion. Now here is a bunch of info to help with debugging. This is really quite a lot of info you know. Yes it is very long isn't it?
'1 != 2' = '%s != %s' % (safe_repr(1), safe_repr(2))
"Something is messed up before this assertion. Now here is a bunch of info to help with debugging. This is really quite a lot of info you know. Yes it is very long isnt it?" = self._formatMessage("Something is messed up before this assertion. Now here is a bunch of info to help with debugging. This is really quite a lot of info you know. Yes it is very long isnt it?", '1 != 2')
>> raise self.failureException("Something is messed up before this assertion. Now here is a bunch of info to help with debugging. This is really quite a lot of info you know. Yes it is very long isnt it?")
Thanks!
Upvotes: 1
Views: 758
Reputation: 304393
nosetests is attempting to be more helpful than the default python assert
. When faced with the default assert error message, you often need more information to track down the bug. You might not fully appreciate this until you've had to hunt down a few dozen of real assertion errors.
You won't need to worry about the results being cluttered, because you pick one and fix it (faster because you have more information) and the usual case is that there aren't many (or better zero)
Upvotes: 1
Reputation: 975
Did you try with self.assertEqual where self is "unittest.TestCase"?
For this
import unittest
class x(unittest.TestCase):
def test_1(self):
self.assertEqual(1,2)
I get this output
laptop:~/workspace/test$ nosetests j.py
F
======================================================================
FAIL: test_1 (j.x)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/workspace/test/j.py", line 4, in test_1
self.assertEqual(1,2)
AssertionError: 1 != 2
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
EDIT: You may also like "nosetests -q, --quiet " for less verbose
Upvotes: 1