An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

[![Build Status](https://travis-ci.org/absent1706/unittest_onerror.svg?branch=master)](https://travis-ci.org/absent1706/unittest_onerror)
[![PyPI version](https://img.shields.io/pypi/v/unittest_onerror.svg)](https://pypi.python.org/pypi/unittest_onerror)
[![Python versions](https://img.shields.io/pypi/pyversions/unittest_onerror.svg)](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')

```

![icon](http://i.piccy.info/i9/c7168c8821f9e7023e32fd784d0e2f54/1489489664/1113/1127895/rsz_18_256.png)
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)
```
![icon](http://i.piccy.info/i9/c7168c8821f9e7023e32fd784d0e2f54/1489489664/1113/1127895/rsz_18_256.png)
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)

![icon](http://i.piccy.info/i9/c7168c8821f9e7023e32fd784d0e2f54/1489489664/1113/1127895/rsz_18_256.png)
See [full example](examples/real_life.py)