userver: /data/code/userver/testsuite/pytest_plugins/pytest_userver/plugins/testpoint.py Source File
Loading...
Searching...
No Matches
testpoint.py
1"""
2Testpoints support for the server.
3"""
4
5# pylint: disable=redefined-outer-name
6import pytest
7
8USERVER_CONFIG_HOOKS = ['userver_config_testpoint']
9
10
11class BaseError(Exception):
12 pass
13
14
20 def __init__(self) -> None:
21 self.enabled_testpoints: frozenset[str] = frozenset()
22
23
24DISABLED_ERROR = """Access to {opname!r} on unregistered testpoint {name}
25
26Use `await service_client.update_server_state()` to explicitly sync testpoints
27state.
28"""
29
30
31@pytest.fixture
32def testpoint_control():
33 return TestpointControl()
34
35
36@pytest.fixture
37def testpoint_checker_factory(testpoint_control):
38 def create_checker(name):
39 def checker(opname):
40 if name not in testpoint_control.enabled_testpoints:
42 DISABLED_ERROR.format(opname=opname, name=name),
43 )
44
45 return checker
46
47 return create_checker
48
49
50@pytest.fixture(scope='session')
51def userver_config_testpoint(mockserver_info):
52 """
53 Returns a function that adjusts the static configuration file for
54 the testsuite.
55 Sets the `tests-control.skip-unregistered-testpoints` to `True`.
56
57 @ingroup userver_testsuite_fixtures
58 """
59
60 def _patch_config(config_yaml, config_vars):
61 components = config_yaml['components_manager']['components']
62 tests_control = components.get('tests-control')
63 if tests_control:
64 tests_control['skip-unregistered-testpoints'] = True
65
66 return _patch_config