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?
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.
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.
>>> 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With