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 py.path
9import pytest
10
11
12class ServiceRunnerModule(pytest.Module):
14 def __init__(self, fspath):
15 self.__file__ = fspath
16
17 def __init__(self, **kwargs) -> None:
18 super().__init__(**kwargs)
19 self._module = self.FakeModule(str(self.fspath))
20
21 @property
22 def obj(self):
23 return self._module
24
25
27 @pytest.hookimpl(tryfirst=True)
28 def pytest_collection_modifyitems(self, session, config, items):
29 paths = set()
30
31 # Is there servicetest chosen
32 for item in items:
33 paths.add(pathlib.Path(item.module.__file__).parent)
34 for marker in item.own_markers:
35 if marker.name == 'servicetest':
36 return
37
38 if not paths:
39 return
40
41 tests_root = min(paths, key=lambda p: len(p.parts))
42
43 module = ServiceRunnerModule.from_parent(
44 parent=session, fspath=py.path.local(tests_root / '__service__'),
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')