Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is nose's assert_raises function?

Tags:

python

nose

I'm using nose 1.1.2 to write tests for a Python project. There is this assert_raises function that happens to be mentioned in the documentation but I can't find it anywhere.

It should be a shorthand for something like this:

value_error_raised = False
try:
    do_something_that_should_raise_value_error()
except ValueError:
    value_error_raised = True
assert value_error_raised

type_error_raised = False
try:
    do_something_else_that_should_raise_type_error()
except TypeError:
    type_error_raised = True
assert type_error_raised

that would become:

assert_raises(ValueError,
              do_something_that_should_raise_value_error)

assert_raises(TypeError,
              do_something_else_that_should_raise_type_error)

I already searched the source code and the only mention I found was in the tools.py module, inside the raises documentation:

If you want to test many assertions about exceptions in a single test, you may want to use assert_raises instead.

Was this function removed from nose? If so, could someone help me understand why?

like image 997
tbellardi Avatar asked May 23 '12 08:05

tbellardi


People also ask

What is nose tools?

The nose. tools module provides a number of testing aids that you may find useful, including decorators for restricting test execution time and testing for exceptions, and all of the same assertX methods found in unittest.

What is nose python?

Nose is a popular test automation framework in Python that extends unittest to make testing easier. The other advantages of using the Nose framework are the enablement of auto discovery of test cases and documentation collection.


1 Answers

>>> from nose.tools import assert_raises
>>> assert_raises
<bound method Dummy.assertRaises of <nose.tools.Dummy testMethod=nop>>
>>> import nose
>>> nose.__version__
'1.1.2'

I personally use the unittest2.TestCase classes with nosetests, and use self.assertRaises.

like image 129