userver: /data/code/service_template/third_party/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,
44 path=(pathlib.Path(tests_root) / '__service__').resolve(),
45 )
46 function = pytest.Function.from_parent(
47 parent=module,
48 name=test_service_default.__name__,
49 callobj=test_service_default,
50 )
51
52 items.append(function)
53
54
55@pytest.mark.servicetest
57 service_client, service_baseurl, monitor_baseurl,
58) -> None:
59 """
60 This is default service runner testcase. Feel free to override it
61 in your own tests, e.g.:
62
63 @code
64 @pytest.mark.servicetest
65 def test_service(service_client):
66 ...
67 @endcode
68
69 @ingroup userver_testsuite
70 """
71 # TODO: use service_client.base_url() and monitor_client.base_url()
72 delimiter = '=' * 100
73 message = f'\n{delimiter}\nStarted service at {service_baseurl}'
74 if monitor_baseurl:
75 message += f', configured monitor URL is {monitor_baseurl}'
76 message += f'\n{delimiter}\n'
77 print(message)
78
79
80def pytest_configure(config):
81 if config.option.service_runner_mode:
82 runner = UserviceRunner()
83 config.pluginmanager.register(runner, 'uservice_runner')