Search code examples
pythonpython-2.7python-3.xnosenosetests

Does assertRaises (or assert_raises) exist in nose2


I am trying to write some nose tests for a python project. It has been a while (a year or so) since I last wrote some nostests and it looks like nose2 is the suggested module to use for this now.

I want to write a test to check that an exception is raised when the wrong value is sent into a def function. I know in nose is was used like this:

from nose.tools import assert_raises

def add(x, y):
    return x + y

assert_raises(TypeError, add, 2, "0")

I just can't find an equivalent use example for nose2, none of these imports work (there is a suggestion that nose2 is more like unittest than nose, which seems to use assertRaises):

from nose2 import assert_raises
from nose2 import assertRaises
from nose2.tools import assert_raises
from nose2.tools import assertRaises

A search of the nose2 documentation website has no mention of an assert_raises or assertRaises


Solution

  • Looks like you can find it in nose2.tools.such.helper. And no, I couldn't find it in the docs either.

    Note there is both Helper and helper; the latter is just a singleton instance of the former. Just to dispel any confusion, this is all they're doing under the hood:

    class Helper(unittest.TestCase):
    
        def runTest(self):
            pass
    
    
    helper = Helper()
    

    i.e. it's just exposing the unittest assert* methods through a dummy TestCase instance.