https://github.com/absent1706/unittest_onerror
Declarative error and failure catching for Python unittest module
https://github.com/absent1706/unittest_onerror
error-handling python python-unittest unittest
Last synced: about 1 month ago
JSON representation
Declarative error and failure catching for Python unittest module
- Host: GitHub
- URL: https://github.com/absent1706/unittest_onerror
- Owner: absent1706
- License: mit
- Created: 2017-05-03T09:56:08.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-05-03T13:06:46.000Z (almost 9 years ago)
- Last Synced: 2025-09-22T23:26:20.796Z (6 months ago)
- Topics: error-handling, python, python-unittest, unittest
- Language: Python
- Size: 10.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/absent1706/unittest_onerror)
[](https://pypi.python.org/pypi/unittest_onerror)
[](https://travis-ci.org/absent1706/unittest_onerror)
# Error and fail catching for Python unittest module
**For now, only Python 2 supported!**
## Error handling
Have you ever wanted to catch unittest errors?
Well, of course you can try-catch every test,
but it's a bit tricky taking into account that some errors should not be treated as errors:
```python
import unittest
from unittest.case import _ExpectedFailure, _UnexpectedSuccess, SkipTest
class MyTest(unittest.TestCase):
def test_something(self):
try:
# your actual code
except Exception as e:
# these errors should NOT be treated as errors
silent_errors = (KeyboardInterrupt, self.failureException,
_ExpectedFailure, _UnexpectedSuccess, SkipTest)
if not isinstance(e, silent_errors):
# your code that handles error
```
With this package, just write your handler and decorate tests:
```python
def my_error_handler(testcase, exception=None):
print('Hey, test {} errored:\n{}'.format(testcase.id(), exception))
class MyTestCase(unittest.TestCase):
@on_error(my_error_handler)
def test_which_errors(self):
raise ValueError('Some unexpected error')
```

See [full example](examples/on_error.py)
### Reraise error
By default, caught error [re-raises](https://github.com/absent1706/unittest_onerror/blob/master/unittest_onerror.py#L48).
You can disable re-raising, so `unittest` runner will treat test as "OK":
```python
@on_error(my_error_handler, reraise=False)
def test_which_errors(self):
# ...
```
## Failure handling
Unlike error handling, failures can be caught easily by rewriting `unittest.TestCase.fail` method.
But we anyway added failure handling to make your life easier:
```python
def my_fail_handler(testcase, exception=None):
print('Hey, test {} failed:\n{}'.format(testcase.id(), exception))
class MyTestCase(unittest.TestCase):
@on_fail(my_fail_handler)
def test_which_fails(self):
self.assertEqual(0, 1)
```

See [full example](examples/on_fail.py)
## Real-life: decorate all tests
In real life, you have a lot of test methods, and it's tedious to add decorator before each method.
So you have quick way to decorate all test methods you want:
```python
@decorate_tests_with(on_fail(my_fail_handler))
@decorate_tests_with(on_error(my_error_handler))
class TestCase(unittest.TestCase):
def test_one(self):
# ...
def test_two(self):
# ...
```
> Note about `print()` in handlers.
>
> Standard Python unittest doesn't sort our output by tests,
> i.e. it [will print all stuff in one place which is not cool](http://www.qopy.me/nyjV1d2oS1WVsIT_Dm-AxA)
>
> So we recommend you to use `nosetests` or `pytest`,
> so [your output will be sorted by test](http://www.qopy.me/dY_60Yj1SSyzds7fJNyk3w)

See [full example](examples/real_life.py)