{"id":44601320,"url":"https://github.com/absent1706/unittest_onerror","last_synced_at":"2026-02-14T09:41:16.893Z","repository":{"id":62586432,"uuid":"90134388","full_name":"absent1706/unittest_onerror","owner":"absent1706","description":"Declarative error and failure catching for Python unittest module","archived":false,"fork":false,"pushed_at":"2017-05-03T13:06:46.000Z","size":11,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-22T23:26:20.796Z","etag":null,"topics":["error-handling","python","python-unittest","unittest"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/absent1706.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-03T09:56:08.000Z","updated_at":"2017-05-04T19:56:53.000Z","dependencies_parsed_at":"2022-11-03T20:21:00.375Z","dependency_job_id":null,"html_url":"https://github.com/absent1706/unittest_onerror","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/absent1706/unittest_onerror","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/absent1706%2Funittest_onerror","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/absent1706%2Funittest_onerror/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/absent1706%2Funittest_onerror/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/absent1706%2Funittest_onerror/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/absent1706","download_url":"https://codeload.github.com/absent1706/unittest_onerror/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/absent1706%2Funittest_onerror/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29442128,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-14T07:24:13.446Z","status":"ssl_error","status_checked_at":"2026-02-14T07:23:58.969Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["error-handling","python","python-unittest","unittest"],"created_at":"2026-02-14T09:41:16.284Z","updated_at":"2026-02-14T09:41:16.884Z","avatar_url":"https://github.com/absent1706.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/absent1706/unittest_onerror.svg?branch=master)](https://travis-ci.org/absent1706/unittest_onerror)\n[![PyPI version](https://img.shields.io/pypi/v/unittest_onerror.svg)](https://pypi.python.org/pypi/unittest_onerror)\n[![Python versions](https://img.shields.io/pypi/pyversions/unittest_onerror.svg)](https://travis-ci.org/absent1706/unittest_onerror)\n\n# Error and fail catching for Python unittest module\n**For now, only Python 2 supported!**\n\n## Error handling\nHave you ever wanted to catch unittest errors?\nWell, of course you can try-catch every test, \n but it's a bit tricky taking into account that some errors should not be treated as errors:\n\n```python\nimport unittest\nfrom unittest.case import _ExpectedFailure, _UnexpectedSuccess, SkipTest\n\nclass MyTest(unittest.TestCase):\n    def test_something(self):\n        try:\n            # your actual code\n        except Exception as e:\n            # these errors should NOT be treated as errors\n            silent_errors = (KeyboardInterrupt, self.failureException,\n                             _ExpectedFailure, _UnexpectedSuccess, SkipTest)\n            if not isinstance(e, silent_errors):\n                # your code that handles error\n```\n\nWith this package, just write your handler and decorate tests:\n\n```python\ndef my_error_handler(testcase, exception=None):\n    print('Hey, test {} errored:\\n{}'.format(testcase.id(), exception))\n\n\nclass MyTestCase(unittest.TestCase):\n    @on_error(my_error_handler)\n    def test_which_errors(self):\n        raise ValueError('Some unexpected error')\n\n```\n\n![icon](http://i.piccy.info/i9/c7168c8821f9e7023e32fd784d0e2f54/1489489664/1113/1127895/rsz_18_256.png)\nSee [full example](examples/on_error.py)\n\n\n### Reraise error\nBy default, caught error [re-raises](https://github.com/absent1706/unittest_onerror/blob/master/unittest_onerror.py#L48).\n\nYou can disable re-raising, so `unittest` runner will treat test as \"OK\":\n```python\n    @on_error(my_error_handler, reraise=False)\n    def test_which_errors(self):\n        # ...\n```\n\n## Failure handling\nUnlike error handling, failures can be caught easily by rewriting `unittest.TestCase.fail` method.\nBut we anyway added failure handling to make your life easier:\n \n```python\ndef my_fail_handler(testcase, exception=None):\n    print('Hey, test {} failed:\\n{}'.format(testcase.id(), exception))\n\n\nclass MyTestCase(unittest.TestCase):\n    @on_fail(my_fail_handler)\n    def test_which_fails(self):\n        self.assertEqual(0, 1)\n```\n![icon](http://i.piccy.info/i9/c7168c8821f9e7023e32fd784d0e2f54/1489489664/1113/1127895/rsz_18_256.png)\nSee [full example](examples/on_fail.py)\n\n\n## Real-life: decorate all tests\nIn real life, you have a lot of test methods, and it's tedious to add decorator before each method.\nSo you have quick way to decorate all test methods you want:\n         \n```python\n@decorate_tests_with(on_fail(my_fail_handler))\n@decorate_tests_with(on_error(my_error_handler))\nclass TestCase(unittest.TestCase):\n    def test_one(self):\n        # ...\n         \n    def test_two(self):\n        # ...   \n\n```                                                       \n\n\u003e Note about `print()` in handlers.\n\u003e\n\u003e Standard Python unittest doesn't sort our output by tests,\n\u003e  i.e. it [will print all stuff in one place which is not cool](http://www.qopy.me/nyjV1d2oS1WVsIT_Dm-AxA)\n\u003e \n\u003e So we recommend you to use `nosetests` or `pytest`,\n\u003e  so [your output will be sorted by test](http://www.qopy.me/dY_60Yj1SSyzds7fJNyk3w)\n \n![icon](http://i.piccy.info/i9/c7168c8821f9e7023e32fd784d0e2f54/1489489664/1113/1127895/rsz_18_256.png)\nSee [full example](examples/real_life.py)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabsent1706%2Funittest_onerror","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fabsent1706%2Funittest_onerror","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabsent1706%2Funittest_onerror/lists"}