1- import datetime
1+ # pylint: disable=redefined-outer-name
2+ # pylint: disable=unused-argument
3+ # pylint: disable=unused-variable
4+ # pylint: disable=too-many-arguments
5+
26import logging
37import os
48from pathlib import Path
1519_GB : ByteSize = ByteSize (_MB * 1024 ) # in bytes
1620
1721
18- # Dictionary to store start times of tests
19- _test_start_times = {}
20-
21-
22- def _utc_now ():
23- return datetime .datetime .now (tz = datetime .timezone .utc )
24-
25-
26- def _construct_graylog_url (api_host , start_time , end_time ):
27- """
28- Construct a Graylog URL for the given time interval.
29- """
30- base_url = api_host .replace ("api." , "monitoring." , 1 ).rstrip ("/" )
31- url = f"{ base_url } /graylog/search"
32- start_time_str = start_time .strftime ("%Y-%m-%dT%H:%M:%S.%fZ" )
33- end_time_str = end_time .strftime ("%Y-%m-%dT%H:%M:%S.%fZ" )
34- query = f"from={ start_time_str } &to={ end_time_str } "
35- return f"{ url } ?{ query } "
36-
37-
38- def pytest_runtest_setup (item ):
39- """
40- Hook to capture the start time of each test.
41- """
42- _test_start_times [item .name ] = _utc_now ()
43-
44-
45- def pytest_runtest_makereport (item , call ):
46- """
47- Hook to add extra information when a test fails.
48- """
49- if call .when == "call" :
50-
51- # Check if the test failed
52- if call .excinfo is not None :
53- test_name = item .name
54- test_location = item .location
55- api_host = os .environ .get ("OSPARC_API_HOST" , "" )
56-
57- diagnostics = {
58- "test_name" : test_name ,
59- "test_location" : test_location ,
60- "api_host" : api_host ,
61- }
62-
63- # Get the start and end times of the test
64- start_time = _test_start_times .get (test_name )
65- end_time = _utc_now ()
66-
67- if start_time :
68- diagnostics ["graylog_url" ] = _construct_graylog_url (api_host , start_time , end_time )
69-
70- # Print the diagnostics
71- print (f"\n Diagnostics for { test_name } :" )
72- for key , value in diagnostics .items ():
73- print (" " , key , ":" , value )
74-
75-
76- @pytest .hookimpl (tryfirst = True )
77- def pytest_configure (config ):
78- config .pluginmanager .register (pytest_runtest_setup , "osparc_test_times_plugin" )
79- config .pluginmanager .register (pytest_runtest_makereport , "osparc_makereport_plugin" )
80-
81-
8222@pytest .fixture
83- def configuration () -> Iterable [ osparc .Configuration ] :
23+ def configuration () -> osparc .Configuration :
8424 assert (host := os .environ .get ("OSPARC_API_HOST" ))
8525 assert (username := os .environ .get ("OSPARC_API_KEY" ))
8626 assert (password := os .environ .get ("OSPARC_API_SECRET" ))
87- yield osparc .Configuration (
27+ return osparc .Configuration (
8828 host = host ,
8929 username = username ,
9030 password = password ,
@@ -93,13 +33,13 @@ def configuration() -> Iterable[osparc.Configuration]:
9333
9434@pytest .fixture
9535def api_client (configuration : osparc .Configuration ) -> Iterable [osparc .ApiClient ]:
96- with osparc .ApiClient (configuration = configuration ) as api_client :
97- yield api_client
36+ with osparc .ApiClient (configuration = configuration ) as _api_client :
37+ yield _api_client
9838
9939
10040@pytest .fixture
101- def async_client (configuration ) -> Iterable [ AsyncClient ] :
102- yield AsyncClient (
41+ def async_client (configuration : osparc . Configuration ) -> AsyncClient :
42+ return AsyncClient (
10343 base_url = configuration .host ,
10444 auth = BasicAuth (
10545 username = configuration .username , password = configuration .password
@@ -108,7 +48,7 @@ def async_client(configuration) -> Iterable[AsyncClient]:
10848
10949
11050@pytest .fixture
111- def tmp_file (tmp_path : Path , caplog ) -> Path :
51+ def tmp_file (tmp_path : Path , caplog : pytest . LogCaptureFixture ) -> Path :
11252 caplog .set_level (logging .INFO )
11353 byte_size : ByteSize = 1 * _GB
11454 tmp_file = tmp_path / "large_test_file.txt"
0 commit comments