forked from man-group/pytest-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytest_fixture_config.py
49 lines (40 loc) · 1.44 KB
/
pytest_fixture_config.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
""" Fixture configuration
"""
import functools
import pytest
class Config(object):
__slots__ = ()
def __init__(self, **kwargs):
[setattr(self, k, v) for (k, v) in kwargs.items()]
def update(self, cfg):
for k in cfg:
if k not in self.__slots__:
raise ValueError("Unknown config option: {0}".format(k))
setattr(self, k, cfg[k])
def requires_config(cfg, vars_):
""" Decorator for fixtures that will skip tests if the required config variables
are missing or undefined in the configuration
"""
def decorator(f):
# We need to specify 'request' in the args here to satisfy pytest's fixture logic
@functools.wraps(f)
def wrapper(request, *args, **kwargs):
for var in vars_:
if not getattr(cfg, var):
pytest.skip('config variable {0} missing, skipping test'.format(var))
return f(request, *args, **kwargs)
return wrapper
return decorator
def yield_requires_config(cfg, vars_):
""" As above but for py.test yield_fixtures
"""
def decorator(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
for var in vars_:
if not getattr(cfg, var):
pytest.skip('config variable {0} missing, skipping test'.format(var))
gen = f(*args, **kwargs)
yield next(gen)
return wrapper
return decorator