userver: en/testsuite/plugins/object_hook.py Source File
Loading...
Searching...
No Matches
object_hook.py
1import pytest
2
3
4class Hookspec:
5 def pytest_register_object_hooks(self):
6 pass
7
8
9class ObjectHooksPlugin:
10 def __init__(self):
11 self._object_hooks = {}
12
13 @property
14 def object_hooks(self):
15 return self._object_hooks
16
17 def pytest_sessionstart(self, session):
18 hooks = session.config.pluginmanager.hook.pytest_register_object_hooks()
19 for hook in hooks:
20 self._object_hooks.update(hook)
21
22 def pytest_addhooks(self, pluginmanager):
23 pluginmanager.add_hookspecs(Hookspec)
24
25
26def pytest_configure(config):
27 config.pluginmanager.register(ObjectHooksPlugin(), 'object_hook_params')
28
29
30@pytest.fixture(scope='session')
31def _base_object_hook(request, pytestconfig, match_operator):
32 hooks = {'$match': match_operator}
33
34 plugin = pytestconfig.pluginmanager.get_plugin('object_hook_params')
35 hooks.update(plugin.object_hooks)
36
37 def _wrapper():
38 return hooks
39
40 return _wrapper
41
42
43@pytest.fixture
44def object_hook(request, _base_object_hook):
45 hooks = {}
46 for name, hook in _base_object_hook().items():
47 if isinstance(hook, dict) and '$fixture' in hook:
48 hook = request.getfixturevalue(hook['$fixture'])
49 hooks[name] = hook
50 return hooks