19async def service_client(
20 service_daemon_instance,
22 service_client_options,
23 userver_service_client_options,
24 userver_client_cleanup,
25 _testsuite_client_config: client.TestsuiteClientConfig,
28 Main fixture that provides access to userver based service.
30 @snippet samples/testsuite-support/tests/test_ping.py service_client
31 @anchor service_client
32 @ingroup userver_testsuite_fixtures
34 if not _testsuite_client_config.testsuite_action_path:
35 yield _ClientDiagnose(service_baseurl, **service_client_options)
38 aiohttp_client = client.AiohttpClient(
40 **userver_service_client_options,
50 service_logs_update_position,
51 servicelogs_register_flusher,
52 _dynamic_config_defaults_storage,
55) -> typing.Callable[[client.Client], typing.AsyncGenerator]:
57 Contains the pre-test and post-test setup that depends
58 on @ref service_client.
60 Feel free to override, but in that case make sure to call the original
61 `userver_client_cleanup` fixture instance.
63 @ingroup userver_testsuite_fixtures
65 marker = request.node.get_closest_marker(
'suspend_periodic_tasks')
67 tasks_to_suspend = marker.args
71 @contextlib.asynccontextmanager
72 async def cleanup_manager(client: client.Client):
73 @servicelogs_register_flusher
76 await client.log_flush()
77 except aiohttp.client_exceptions.ClientError:
84 service_logs_update_position()
86 await _dynamic_config_defaults_storage.update(client, dynamic_config)
89 await client.suspend_periodic_tasks(tasks_to_suspend)
93 await client.resume_all_periodic_tasks()
95 return cleanup_manager
101 Fixture that provides access to userver based websocket service.
105 @snippet samples/websocket_service/tests/test_websocket.py Functional test
107 You can pass extra kwargs to `get`, they will be forwarded to [websockets.connect][1].
109 [1]: https://websockets.readthedocs.io/en/stable/reference/asyncio/client.html#websockets.asyncio.client.connect
111 @anchor websocket_client
112 @ingroup userver_testsuite_fixtures
116 @contextlib.asynccontextmanager
117 async def get(self, path, **kwargs):
118 update_server_state = getattr(
120 'update_server_state',
123 if update_server_state:
124 await update_server_state()
125 ws_context = websockets.connect(uri=f
'ws://localhost:{service_port}/{path}', **kwargs)
126 async with ws_context
as socket:
135 userver_monitor_client_options,
137) -> client.ClientMonitor:
139 Main fixture that provides access to userver monitor listener.
141 @snippet samples/testsuite-support/tests/test_metrics.py metrics labels
142 @ingroup userver_testsuite_fixtures
144 aiohttp_client = client.AiohttpClientMonitor(
146 **userver_monitor_client_options,
154class _ClientDiagnose(base_service_client.Client):
155 def __getattr__(self, name: str) ->
None:
156 raise AttributeError(
157 f
'"Client" object has no attribute "{name}". '
158 'Note that "service_client" fixture returned the basic '
159 '"testsuite.daemons.service_client.Client" client rather than '
160 'a "pytest_userver.client.Client" client with userver '
161 'extensions. That happened because the service '
162 'static configuration file contains no "tests-control" '
163 'component with "action" field.',
168@pytest.fixture(name='service_client_options')
169def _service_client_options(
170 service_client_options,
171 service_client_default_headers,
177 service_client_options,
179 **service_client_default_headers,
180 mockserver.trace_id_header: mockserver.trace_id,
186def userver_service_client_options(
187 service_client_options,
188 _testsuite_client_config: client.TestsuiteClientConfig,
191 service_periodic_tasks_state,
194 cache_invalidation_state,
195 userver_cache_control,
197 userver_allow_all_caches_invalidation,
200 **service_client_options,
201 config=_testsuite_client_config,
203 testpoint_control=testpoint_control,
204 periodic_tasks_state=service_periodic_tasks_state,
205 log_capture_fixture=userver_log_capture,
206 mocked_time=mocked_time,
207 cache_invalidation_state=cache_invalidation_state,
208 cache_control=userver_cache_control,
209 asyncexc_check=asyncexc_check,
210 allow_all_caches_invalidation=userver_allow_all_caches_invalidation,
215def userver_monitor_client_options(
216 service_client_options,
217 _testsuite_client_config: client.TestsuiteClientConfig,
219 return dict(**service_client_options, config=_testsuite_client_config)
225@pytest.fixture(scope='session')