If you are using Python 2.7 or above, you can use the ability of assertRaises to be used as a context manager and do:

with self.assertRaises(TypeError):
    self.testListNone[:1]

If you are using Python 2.6, another way beside the one given until now is to use unittest2 which is a back port of unittest's new features to Python 2.6, and you can make it work using the code above.

N.B: I'm a big fan of the new feature (SkipTest, test discovery, etc.) of unittest, so I intend to use unittest2 as much as I can. I advise to do the same, because there is a lot more than what unittest come with in Python 2.6 or lower.

Answer from mouad on Stack Overflow
🌐
Python
docs.python.org › 3 › library › unittest.html
unittest — Unit testing framework
Changed in version 3.1: Added the ability to use assertRaises() as a context manager.
🌐
Gitlab
ongspxm.gitlab.io › blog › 2016 › 11 › assertraises-testing-for-errors-in-unittest
assertRaises - testing for errors in unittest
November 16, 2016 - import unittest def func(): raise Exception('lets see if this works') class ExampleTest(unittest.TestCase): def test_error(self): self.assertRaises(Exception, lambda:func()) if __name__=='__main__': unittest.main() But lambda is a whole new monster on its own. It doesn't seem to be the right tool to use just as a wrapper for the function. This is a much more appropriate and pythonic way of using assertRaises.
🌐
GeeksforGeeks
geeksforgeeks.org › python › test-if-a-function-throws-an-exception-in-python
Test if a function throws an exception in Python - GeeksforGeeks
July 15, 2025 - assertRaises(exception) Make a function call that should raise the exception with a context. The context manager will caught an exception and store it in the object in its exception attribute.
🌐
Lengrand
lengrand.fr › pythonunittest-assertraises-raises-error
Python/Unittest : assertRaises raises Error
May 16, 2026 - import dum_function as df # import function module import unittest class Test(unittest.TestCase): """ The class inherits from unittest """ def setUp(self): """ This method is called before each test """ self.false_int = "A" def tearDown(self): """ This method is called after each test """ pass #--- ## TESTS def test_square_value(self): # assertRaises(excClass, callableObj) prototype self.assertRaises(TypeError, df.square_value(self.false_int)) if name == "main": unittest.main()
🌐
Real Python
realpython.com › python-unittest
Python's unittest: Writing Unit Tests for Your Code – Real Python
April 29, 2024 - Language: Python Filename: test_game.py · import unittest from game import rock_paper_scissors class TestRockPaperScissors(unittest.TestCase): def test_out_of_range(self): with self.assertRaises(ValueError): rock_paper_scissors(42) def test_rock(self): self.assertEqual(rock_paper_scissors(0), "rock") def test_paper(self): self.assertEqual(rock_paper_scissors(1), "paper") def test_scissors(self): self.assertEqual(rock_paper_scissors(2), "scissors") if __name__ == "__main__": unittest.main() The first test provides a way to check whether the function validates the input argument, which must be a number between 0 and 2.
Find elsewhere
🌐
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › unittest.TestCase.assertRaises.html
unittest.TestCase.assertRaises — Python Standard Library
with self.assertRaises(SomeException) as cm: do_something() the_exception = cm.exception self.assertEqual(the_exception.error_code, 3)
🌐
Medium
medium.com › @rinu.gour123 › unit-testing-with-python-unittest-ad045671010
Unit Testing With Python Unittest | by Rinu Gour | Medium
October 24, 2018 - Then, we define three methods, the names for which begin with ‘test’: ... assertRaises()- Tests that Python raises an exception when we call the callable with positional/ keyword arguments we also passed to this method.
🌐
Electricmonk
electricmonk.nl › log › 2012 › 01 › 14 › python-unittest-assertraises-pitfall
Python UnitTest: AssertRaises pitfall | Electricmonk.nl weblog
assertRaises(exception, callable, *args, **kwds) Test that an exception is raised when callable is called with any positional or keyword arguments that are also passed to assertRaises().
🌐
pytest
docs.pytest.org › en › 7.1.x › how-to › assert.html
How to write and report assertions in tests — pytest documentation
pytest allows you to use the standard Python assert for verifying expectations and values in Python tests.
🌐
Python.org
discuss.python.org › ideas
Asserting exact exception instances - Ideas - Discussions on Python.org
March 9, 2025 - There are two helpers in unittest.TestCase for asserting that exceptions are raised and properties of them: assertRaises checks that an exception is raised and asserts its type assertRaisesRegex checks that an exception is raised and matches ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-unittest-asserttrue-function
Python unittest - assertTrue() function - GeeksforGeeks
July 15, 2025 - assertTrue() in Python is a unittest library function that is used in unit testing to compare test value with true. This function will take two parameters as input and return a boolean value depending upon the assert condition.
🌐
TutorialsPoint
tutorialspoint.com › unittest_framework › unittest_framework_assertion.htm
UnitTest Framework - Assertion
Python testing framework uses Python's built-in assert() function which tests a particular condition. If the assertion fails, an AssertionError will be raised. The testing framework will then identify the test as Failure.
🌐
Codereview
codereview.doctor › features › python › best-practice › testing
Python testing best practice | codereview.doctor
Code Review Doctor suggests a fix if your code does not follows Python testing best practices.
🌐
Readthedocs
pydoc-zh.readthedocs.io › en › latest › library › unittest.html
25.3. unittest — Unit testing framework — Python 2.7.6 documentation
Test that an exception is raised when callable is called with any positional or keyword arguments that are also passed to assertRaises(). The test passes if exception is raised, is an error if another exception is raised, or fails if no exception is raised.
🌐
BrowserStack
browserstack.com › home › guide › assert in python: syntax, examples, and best practices
Assert in Python: Syntax, Examples, and Best Practices | BrowserStack
August 25, 2026 - Python’s unittest framework provides dedicated assertion methods instead of relying only on the assert statement. import unittest class TestMath(unittest.TestCase): def test_multiply(self): self.assertEqual(2 * 3, 6) self.assertGreater(10, 5) self.assertIsNone(None) Output – · Methods such as assertEqual(), assertTrue(), assertIn(), and assertRaises() make the expected relationship explicit and provide failure information when a test does not pass.
🌐
Programiz
programiz.com › python-programming › assert-statement
Python Assert Statement
Python has built-in assert statement to use assertion condition in the program. assert statement has a condition or expression which is supposed to be always true.
🌐
GitHub
gist.github.com › asprenger › d59cc4697172fddf968169600421a366
Python unittest Assertions cheat sheet · GitHub
Python unittest Assertions cheat sheet. GitHub Gist: instantly share code, notes, and snippets.
🌐
Kapeli
kapeli.com › cheat_sheets › Python_unittest_Assertions.docset › Contents › Resources › Documents › index
Python unittest Assertions - Dash Cheat Sheets - Kapeli
All the assert methods except assertRaises() and assertRaisesRegexp() accept a msg argument that, if specified, is used as the error message on failure.