Last active
August 29, 2015 14:02
-
-
Save svvitale/b7b6f4abba4a6a5106fd to your computer and use it in GitHub Desktop.
A unittest/nose decorator for grouping tests together with an expected failure rate. A use case would be if you've written two tests, one of which should always pass. This decorator will skip failures until the expected failure count is exceeded or the grouping completes without reaching the expected failure count.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import nose | |
import functools | |
_either_or_map = dict() | |
def either_or(key='default', expected_failures=1): | |
def either_or_decorator(test): | |
global _either_or_map | |
if not key in _either_or_map: | |
_either_or_map[key] = { | |
'failed': 0, | |
'total': 0, | |
'run': 0 | |
} | |
metrics = _either_or_map[key] | |
metrics['total'] += 1 | |
@functools.wraps(test) | |
def inner(*args, **kwargs): | |
try: | |
metrics['run'] += 1 | |
test(*args, **kwargs) | |
except Exception: | |
metrics['failed'] += 1 | |
raise nose.SkipTest | |
finally: | |
if metrics['run'] == metrics['total']: | |
if metrics['failed'] > expected_failures: | |
raise Exception("More failures than expected in either_or set '%s'" % (key,)) | |
elif metrics['failed'] < expected_failures: | |
raise Exception("Fewer failures than expected in either_or set '%s'" % (key,)) | |
return inner | |
return either_or_decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment