22async def service_client(
23 service_daemon_instance,
25 service_client_options,
26 userver_service_client_options,
27 userver_client_cleanup,
28 _testsuite_client_config: client.TestsuiteClientConfig,
31 Main fixture that provides access to userver based service.
33 @snippet samples/testsuite-support/tests/test_ping.py service_client
34 @anchor service_client
35 @ingroup userver_testsuite_fixtures
37 if not _testsuite_client_config.testsuite_action_path:
38 yield _ClientDiagnose(service_baseurl, **service_client_options)
41 aiohttp_client = client.AiohttpClient(
43 **userver_service_client_options,
53 service_logs_update_position,
54 servicelogs_register_flusher,
55 _dynamic_config_defaults_storage,
58) -> Callable[[client.Client], AsyncGenerator]:
60 Contains the pre-test and post-test setup that depends
61 on @ref service_client.
63 Feel free to override, but in that case make sure to call the original
64 `userver_client_cleanup` fixture instance.
66 @ingroup userver_testsuite_fixtures
68 marker = request.node.get_closest_marker(
'suspend_periodic_tasks')
70 warnings.warn(userver_warnings.WARN_PERIODIC_DEPRECATION, DeprecationWarning)
71 tasks_to_suspend = marker.args
75 @contextlib.asynccontextmanager
76 async def cleanup_manager(client: client.Client):
77 @servicelogs_register_flusher
80 await client.log_flush()
81 except aiohttp.client_exceptions.ClientError:
88 service_logs_update_position()
90 await _dynamic_config_defaults_storage.update(client, dynamic_config)
93 await client.suspend_periodic_tasks(tasks_to_suspend)
97 await client.resume_all_periodic_tasks()
99 return cleanup_manager
105 Fixture that provides access to userver based websocket service.
109 @snippet samples/websocket_service/tests/test_websocket.py Functional test
111 You can pass extra kwargs to `get`, they will be forwarded to [websockets.connect][1].
113 [1]: https://websockets.readthedocs.io/en/stable/reference/asyncio/client.html#websockets.asyncio.client.connect
115 @anchor websocket_client
116 @ingroup userver_testsuite_fixtures
120 @contextlib.asynccontextmanager
121 async def get(self, path, **kwargs):
122 update_server_state = getattr(
124 'update_server_state',
127 if update_server_state:
128 await update_server_state()
129 ws_context = websockets.connect(uri=f
'ws://localhost:{service_port}/{path}', **kwargs)
130 async with ws_context
as socket:
139 userver_monitor_client_options,
141) -> client.ClientMonitor:
143 Main fixture that provides access to userver monitor listener.
145 @snippet samples/testsuite-support/tests/test_metrics.py metrics labels
146 @ingroup userver_testsuite_fixtures
148 aiohttp_client = client.AiohttpClientMonitor(
150 **userver_monitor_client_options,
158class _ClientDiagnose(base_service_client.Client):
159 def __getattr__(self, name: str) ->
None:
160 raise AttributeError(
161 f
'"Client" object has no attribute "{name}". '
162 'Note that "service_client" fixture returned the basic '
163 '"testsuite.daemons.service_client.Client" client rather than '
164 'a "pytest_userver.client.Client" client with userver '
165 'extensions. That happened because the service '
166 'static configuration file contains no "tests-control" '
167 'component with "action" field.',
172@pytest.fixture(name='service_client_options')
173def _service_client_options(
174 service_client_options,
175 service_client_default_headers,
181 service_client_options,
183 **service_client_default_headers,
184 mockserver.trace_id_header: mockserver.trace_id,
190def userver_service_client_options(
191 service_client_options,
192 _testsuite_client_config: client.TestsuiteClientConfig,
195 service_periodic_tasks_state,
198 cache_invalidation_state,
199 userver_cache_control,
201 userver_allow_all_caches_invalidation,
204 **service_client_options,
205 config=_testsuite_client_config,
207 testpoint_control=testpoint_control,
208 periodic_tasks_state=service_periodic_tasks_state,
209 log_capture_fixture=userver_log_capture,
210 mocked_time=mocked_time,
211 cache_invalidation_state=cache_invalidation_state,
212 cache_control=userver_cache_control,
213 asyncexc_check=asyncexc_check,
214 allow_all_caches_invalidation=userver_allow_all_caches_invalidation,
219def userver_monitor_client_options(
220 service_client_options,
221 _testsuite_client_config: client.TestsuiteClientConfig,
223 return dict(**service_client_options, config=_testsuite_client_config)
229@pytest.fixture(scope='session')