{"id":936,"hash":"029ef9c8e3ce50d1c8e98a4ccf20d48a9933b6afb5aa73090e811a6bdde4b01b","pattern":"How do I properly assert that an exception gets raised in pytest?","full_message":"Code:\n\n# coding=utf-8\nimport pytest\n\ndef whatever():\n    return 9/0\n\ndef test_whatever():\n    try:\n        whatever()\n    except ZeroDivisionError as exc:\n        pytest.fail(exc, pytrace=True)\n\nOutput:\n\n================================ test session starts =================================\nplatform linux2 -- Python 2.7.3 -- py-1.4.20 -- pytest-2.5.2\nplugins: django, cov\ncollected 1 items \n\npytest_test.py F\n\n====================================== FAILURES ======================================\n___________________________________ test_whatever ____________________________________\n\n    def test_whatever():\n        try:\n            whatever()\n        except ZeroDivisionError as exc:\n>           pytest.fail(exc, pytrace=True)\nE           Failed: integer division or modulo by zero\n\npytest_test.py:12: Failed\n============================== 1 failed in 1.16 seconds ==============================\n\nHow do I make pytest print traceback, so that I would see where in the whatever function that an exception was raised?","ecosystem":"pypi","package_name":"unit-testing","package_version":null,"solution":"pytest.raises(Exception) is what you need.\n\nCode\n\nimport pytest\n\ndef test_passes():\n    with pytest.raises(Exception) as e_info:\n        x = 1 / 0\n\ndef test_passes_without_info():\n    with pytest.raises(Exception):\n        x = 1 / 0\n\ndef test_fails():\n    with pytest.raises(Exception) as e_info:\n        x = 1 / 1\n\ndef test_fails_without_info():\n    with pytest.raises(Exception):\n        x = 1 / 1\n\n# Don't do this. Assertions are caught as exceptions.\ndef test_passes_but_should_not():\n    try:\n        x = 1 / 1\n        assert False\n    except Exception:\n        assert True\n\n# Even if the appropriate exception is caught, it is bad style,\n# because the test result is less informative\n# than it would be with pytest.raises(e)\n# (it just says pass or fail.)\n\ndef test_passes_but_bad_style():\n    try:\n        x = 1 / 0\n        assert False\n    except ZeroDivisionError:\n        assert True\n\ndef test_fails_but_bad_style():\n    try:\n        x = 1 / 1\n        assert False\n    except ZeroDivisionError:\n        assert True\n\nOutput\n\n============================================================================================= test session starts ==============================================================================================\nplatform linux2 -- Python 2.7.6 -- py-1.4.26 -- pytest-2.6.4\ncollected 7 items \n\ntest.py ..FF..F\n\n=================================================================================================== FAILURES ===================================================================================================\n__________________________________________________________________________________________________ test_fails __________________________________________________________________________________________________\n\n    def test_fails():\n        with pytest.raises(Exception) as e_info:\n>           x = 1 / 1\nE           Failed: DID NOT RAISE\n\ntest.py:13: Failed\n___________________________________________________________________________________________ test_fails_without_info ____________________________________________________________________________________________\n\n    def test_fails_without_info():\n        with pytest.raises(Exception):\n>           x = 1 / 1\nE           Failed: DID NOT RAISE\n\ntest.py:17: Failed\n___________________________________________________________________________________________ test_fails_but_bad_style ___________________________________________________________________________________________\n\n    def test_fails_but_bad_style():\n        try:\n            x = 1 / 1\n>           assert False\nE           assert False\n\ntest.py:43: AssertionError\n====================================================================================== 3 failed, 4 passed in 0.02 seconds ======================================================================================\n\nNote that e_info saves the exception object so you can extract details from it. For example, if you want to check the exception call stack or another nested exception inside.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/23337471/how-do-i-properly-assert-that-an-exception-gets-raised-in-pytest","votes":924,"created_at":"2026-04-19T04:52:04.233981+00:00","updated_at":"2026-04-19T04:52:04.233981+00:00"}