userver: /data/code/userver/testsuite/pytest_plugins/pytest_userver/plugins/service_runner.py Source File
Loading...
Searching...
No Matches
service_runner.py
1"""
2Helpers to make the `make start-*` commands work.
3"""
4
5# pylint: disable=no-member,missing-kwoa
6import pathlib
7
8import pytest
9
10
11class ServiceRunnerModule(pytest.Module):
13 def __init__(self, path):
14 self.__file__ = path
15
16 def __init__(self, **kwargs) -> None:
17 super().__init__(**kwargs)
18 self._module = self.FakeModule(path=str(self.path))
19
20 @property
21 def obj(self):
22 return self._module
23
24
26 @pytest.hookimpl(tryfirst=True)
27 def pytest_collection_modifyitems(self, session, config, items):
28 paths = set()
29
30 # Is there servicetest chosen
31 for item in items:
32 paths.add(pathlib.Path(item.module.__file__).parent)
33 for marker in item.own_markers:
34 if marker.name == 'servicetest':
35 return
36
37 if not paths:
38 return
39
40 tests_root = min(paths, key=lambda p: len(p.parts))
41
42 module = ServiceRunnerModule.from_parent(
43 parent=session, path=pathlib.Path(tests_root).resolve(),
44 )
45 function = pytest.Function.from_parent(
46 parent=module,
47 name=test_service_default.__name__,
48 callobj=test_service_default,
49 )
50
51 items.append(function)
52
53
54@pytest.mark.servicetest
56 service_client, service_baseurl, monitor_baseurl,
57) -> None:
58 """
59 This is default service runner testcase. Feel free to override it
60 in your own tests, e.g.:
61
62 @code
63 @pytest.mark.servicetest
64 def test_service(service_client):
65 ...
66 @endcode
67
68 @ingroup userver_testsuite
69 """
70 # TODO: use service_client.base_url() and monitor_client.base_url()
71 delimiter = '=' * 100
72 message = f'\n{delimiter}\nStarted service at {service_baseurl}'
73 if monitor_baseurl:
74 message += f', configured monitor URL is {monitor_baseurl}'
75 message += f'\n{delimiter}\n'
76 print(message)
77
78
79def pytest_configure(config):
80 if config.option.service_runner_mode:
81 runner = UserviceRunner()
82 config.pluginmanager.register(runner, 'uservice_runner')