Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 346
Douglas Kastle Avatar asked Feb 25 '14 17:02

Douglas Kastle


Video Answer


1 Answers

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.

like image 54
roippi Avatar answered Sep 16 '22 15:09

roippi