userver: en/testsuite/environment/pytest_plugin.py Source File
Loading...
Searching...
No Matches
pytest_plugin.py
1import pathlib
2
3import pytest
4
5from . import control, utils
6
7
8class Hookspec:
9 def pytest_service_register(self, register_service):
10 """Register testsuite environment service."""
11
12
13class TestsuiteEnvironmentPlugin:
15
16 def __init__(self):
17 self._env = None
18
19 def ensure_started(self, service_name, **kwargs):
20 if self._env:
21 self._env.ensure_started(service_name, **kwargs)
22
23 def pytest_sessionstart(self, session):
24 start_environment = session.config.option.start_environment
25 if start_environment == 'no':
26 return
27 if hasattr(session.config, 'workerinput'):
28 worker_id = session.config.workerinput['workerid']
29 elif session.config.pluginmanager.getplugin('dsession'):
30 return
31 else:
32 worker_id = control.DEFAULT_WORKER_ID
33
34 config = control.load_environment_config(
35 env_dir=session.config.option.env_dir,
36 worker_id=worker_id,
37 reuse_services=start_environment == 'auto',
38 verbose=session.config.option.verbose,
39 )
40 self._env = control.TestsuiteEnvironment(config)
41
42 def register_service(service_name, factory=None):
43 def decorator(factory):
44 self._env.register_service(service_name, factory)
45
46 if factory is None:
47 return decorator
48 return decorator(factory)
49
50 session.config.pluginmanager.hook.pytest_service_register(
51 register_service=register_service,
52 )
53
54 def pytest_sessionfinish(self, session):
55 if self._env:
56 self._env.close()
57
58 def pytest_addhooks(self, pluginmanager):
59 pluginmanager.add_hookspecs(Hookspec)
60
61 def pytest_report_header(self, config):
62 headers = []
63 if self._env:
64 if self._env.config.reuse_services:
65 kind = 'auto'
66 else:
67 kind = 'new'
68 headers.append(
69 f'testsuite env: {kind}, dir: {self._env.config.env_dir}',
70 )
71 else:
72 headers.append('testsuite env: external')
73 return headers
74
75
76def pytest_addoption(parser):
77 group = parser.getgroup('env', 'Testsuite environment')
78 group.addoption(
79 '--env-dir',
80 default=None,
81 type=pathlib.Path,
82 help='Path environment data directry.',
83 )
84 group.addoption(
85 '--start-environment',
86 choices=['yes', 'no', 'auto'],
87 default='yes',
88 )
89 group.addoption(
90 '--no-env',
91 action='store_const',
92 const='no',
93 dest='start_environment',
94 help='Use existing environment, do not start new',
95 )
96 group.addoption(
97 '--auto-env',
98 action='store_const',
99 const='auto',
100 dest='start_environment',
101 help='If present use existing environment, otherwise start new',
102 )
103 group.addoption(
104 '--start-env',
105 action='store_const',
106 const='yes',
107 dest='start_environment',
108 help='Start new environment',
109 )
110
111
112def pytest_configure(config):
113 utils.ensure_non_root_user()
114 config.pluginmanager.register(
115 TestsuiteEnvironmentPlugin(),
116 'testsuite_environment',
117 )
118
119
120@pytest.fixture(scope='session')
121def ensure_service_started(pytestconfig):
122 env: TestsuiteEnvironmentPlugin
123 env = pytestconfig.pluginmanager.get_plugin('testsuite_environment')
124 return env.ensure_started