20async def service_client(
21 service_daemon_instance,
23 service_client_options,
24 userver_service_client_options,
25 userver_client_cleanup,
26 _testsuite_client_config: client.TestsuiteClientConfig,
29 Main fixture that provides access to userver based service.
31 @snippet samples/testsuite-support/tests/test_ping.py service_client
32 @anchor service_client
33 @ingroup userver_testsuite_fixtures
35 if not _testsuite_client_config.testsuite_action_path:
36 yield _ClientDiagnose(service_baseurl, **service_client_options)
39 aiohttp_client = client.AiohttpClient(
41 **userver_service_client_options,
51 service_logs_update_position,
52 servicelogs_register_flusher,
53 _dynamic_config_defaults_storage,
56) -> Callable[[client.Client], AsyncGenerator]:
58 Contains the pre-test and post-test setup that depends
59 on @ref service_client.
61 Feel free to override, but in that case make sure to call the original
62 `userver_client_cleanup` fixture instance.
64 @ingroup userver_testsuite_fixtures
66 marker = request.node.get_closest_marker(
'suspend_periodic_tasks')
68 tasks_to_suspend = marker.args
72 @contextlib.asynccontextmanager
73 async def cleanup_manager(client: client.Client):
74 @servicelogs_register_flusher
77 await client.log_flush()
78 except aiohttp.client_exceptions.ClientError:
85 service_logs_update_position()
87 await _dynamic_config_defaults_storage.update(client, dynamic_config)
90 await client.suspend_periodic_tasks(tasks_to_suspend)
94 await client.resume_all_periodic_tasks()
96 return cleanup_manager
102 Fixture that provides access to userver based websocket service.
106 @snippet samples/websocket_service/tests/test_websocket.py Functional test
108 You can pass extra kwargs to `get`, they will be forwarded to [websockets.connect][1].
110 [1]: https://websockets.readthedocs.io/en/stable/reference/asyncio/client.html#websockets.asyncio.client.connect
112 @anchor websocket_client
113 @ingroup userver_testsuite_fixtures
117 @contextlib.asynccontextmanager
118 async def get(self, path, **kwargs):
119 update_server_state = getattr(
121 'update_server_state',
124 if update_server_state:
125 await update_server_state()
126 ws_context = websockets.connect(uri=f
'ws://localhost:{service_port}/{path}', **kwargs)
127 async with ws_context
as socket:
136 userver_monitor_client_options,
138) -> client.ClientMonitor:
140 Main fixture that provides access to userver monitor listener.
142 @snippet samples/testsuite-support/tests/test_metrics.py metrics labels
143 @ingroup userver_testsuite_fixtures
145 aiohttp_client = client.AiohttpClientMonitor(
147 **userver_monitor_client_options,
155class _ClientDiagnose(base_service_client.Client):
156 def __getattr__(self, name: str) ->
None:
157 raise AttributeError(
158 f
'"Client" object has no attribute "{name}". '
159 'Note that "service_client" fixture returned the basic '
160 '"testsuite.daemons.service_client.Client" client rather than '
161 'a "pytest_userver.client.Client" client with userver '
162 'extensions. That happened because the service '
163 'static configuration file contains no "tests-control" '
164 'component with "action" field.',
169@pytest.fixture(name='service_client_options')
170def _service_client_options(
171 service_client_options,
172 service_client_default_headers,
178 service_client_options,
180 **service_client_default_headers,
181 mockserver.trace_id_header: mockserver.trace_id,
187def userver_service_client_options(
188 service_client_options,
189 _testsuite_client_config: client.TestsuiteClientConfig,
192 service_periodic_tasks_state,
195 cache_invalidation_state,
196 userver_cache_control,
198 userver_allow_all_caches_invalidation,
201 **service_client_options,
202 config=_testsuite_client_config,
204 testpoint_control=testpoint_control,
205 periodic_tasks_state=service_periodic_tasks_state,
206 log_capture_fixture=userver_log_capture,
207 mocked_time=mocked_time,
208 cache_invalidation_state=cache_invalidation_state,
209 cache_control=userver_cache_control,
210 asyncexc_check=asyncexc_check,
211 allow_all_caches_invalidation=userver_allow_all_caches_invalidation,
216def userver_monitor_client_options(
217 service_client_options,
218 _testsuite_client_config: client.TestsuiteClientConfig,
220 return dict(**service_client_options, config=_testsuite_client_config)
226@pytest.fixture(scope='session')