Does assertRaises (or assert_raises) exist in nose2
Asked Answered
L

2

6

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

Lemar answered 25/2, 2014 at 17:57 Comment(2)
I guess you're not using nose2 with unittest.TestCase subclasses? Else, you could just use unittest.TestCase.assertRaises() with nose2.Corvus
Since I raised this issue, and based on the answer below (more of a jumping off point) that is what I have gone back and done. It is just not the way it was being done with the testing I was doing under nose. I was hoping to get more feedback, but I am wondering if nose2 is ready for primetimeLemar
A
8

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.

Airing answered 26/2, 2014 at 5:49 Comment(0)
H
0

nose2.tools.such.helper is no longer available.

The proper solution is to know that in nose2, all test classes must inherit from unittest.TestCase, and therefore you switch to using self.assertRaises

Hemispheroid answered 3/11, 2023 at 15:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.