-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtesting.py
63 lines (48 loc) · 1.67 KB
/
testing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import contextlib
import functools
import inspect
import pytest
def for_each(examples):
def decorator(fun):
args, vargs, kwargs, defaults = inspect.getargspec(fun)
if vargs or kwargs or defaults:
raise TypeError('\n '.join([
'Unsupported signature: '.format(fun),
'args = {}'.format(args),
'vargs = {}'.format(vargs),
'kwargs = {}'.format(kwargs),
'defaults = {}'.format(defaults),
]))
argnames = ','.join(args)
return pytest.mark.parametrize(argnames, examples)(fun)
return decorator
def for_each_kwargs(examples):
def decorator(fun):
args, vargs, kwargs, defaults = inspect.getargspec(fun)
if vargs or kwargs:
raise TypeError('\n '.join([
'Unsupported signature: '.format(fun),
'args = {}'.format(args),
'vargs = {}'.format(vargs),
'kwargs = {}'.format(kwargs),
'defaults = {}'.format(defaults),
]))
# FIXME This wrapper pollutes the test log.
@functools.wraps(fun)
def wrapped_fun(example):
example = {k: v for k, v in example.iteritems() if k in args}
return fun(**example)
return pytest.mark.parametrize('example', examples)(wrapped_fun)
return decorator
@contextlib.contextmanager
def xfail_if_not_implemented():
try:
yield
except NotImplementedError as e:
pytest.xfail(reason=str(e))
@contextlib.contextmanager
def skip_if_not_implemented():
try:
yield
except NotImplementedError as e:
pytest.skip(str(e))