Use TestCase.assertRaises
(or TestCase.failUnlessRaises
) from the unittest module, for example:
import mymod
class MyTestCase(unittest.TestCase):
def test1(self):
self.assertRaises(SomeCoolException, mymod.myfunc)
Related Contents:
- Python, mock: raise exception [closed]
- How can I write a `try`/`except` block that catches all exceptions?
- Correct way to try/except using Python requests module?
- How to properly assert that an exception gets raised in pytest?
- Where do the Python unit tests go? [closed]
- Catching an exception while using a Python ‘with’ statement
- How do I determine what type of exception occurred?
- Getting the exception value in Python
- BaseException.message deprecated in Python 2.6
- How to handle exceptions in a list comprehensions?
- Best way to assert for numpy.array equality?
- How to mock a readonly property with mock?
- Is it possible for a unit test to assert that a method calls sys.exit()?
- Django test runner not finding tests
- Create and import helper functions in tests without creating packages in test directory using py.test
- Run setUp only once for a set of automated tests
- Mock attributes in Python mock?
- Python exception chaining [duplicate]
- Continuing in Python’s unittest when an assertion fails
- Unit tests for functions in a Jupyter notebook?
- Unit testing a python app that uses the requests library
- How to properly use mock in python with unittest setUp
- How to test with Python’s unittest that a warning has been thrown?
- Mocking a global variable
- Catching DoesNotExist exception in a custom manager in Django
- Unittest tests order
- Scrapy Unit Testing
- How to change the message in a Python AssertionError?
- Is there a way to prevent a SystemExit exception raised from sys.exit() from being caught?
- patching a class yields “AttributeError: Mock object has no attribute” when accessing instance attributes
- Python mock call_args_list unpacking tuples for assertion on arguments
- Handle an exception thrown in a generator
- How do I mock the filesystem in Python unit tests?
- Why python mock patch doesn’t work?
- Is there a way to use Python unit test assertions outside of a TestCase?
- How can I fake request.POST and GET params for unit testing in Flask?
- Using Basic HTTP access authentication in Django testing framework
- Python unittest’s assertDictContainsSubset recommended alternative [duplicate]
- Pytest fixture for a class through self not as method argument
- How to create table during Django tests with managed = False
- What is the difference between mocking and monkey patching?
- How to load data from a file, for a unit test, in python?
- How do I unit test PySpark programs?
- “outsourcing” exception-handling to a decorator [closed]
- How to know time spent on each test when using unittest?
- How can I create a ramdisk in Python?
- How do I make pytest fixtures work with decorated functions?
- rethrowing python exception. Which to catch?
- Break on exception in pydev
- Does pytest parametrized test work with unittest class based tests?