@@ -118,53 +118,57 @@ def test_stub_data_to_file(tmp_path):
118118def test_stub_data_postprocess_logs (tmp_path ):
119119 """Tests the postprocess method of Tidy3dStubData when simulation diverged."""
120120 td .log .set_capture (True )
121-
122- # test diverged
123- sim_data = make_sim_data ()
124- sim_data = sim_data .updated_copy (diverged = True , log = "The simulation has diverged!" )
125- file_path = os .path .join (tmp_path , "test_diverged.hdf5" )
126- sim_data .to_file (file_path )
127- Tidy3dStubData .postprocess (file_path )
128-
129- # test warnings
130- sim_data = make_sim_data ()
131- sim_data = sim_data .updated_copy (log = "WARNING: messages were found in the solver log." )
132- file_path = os .path .join (tmp_path , "test_warnings.hdf5" )
133- sim_data .to_file (file_path )
134- Tidy3dStubData .postprocess (file_path )
121+ try :
122+ # test diverged
123+ sim_data = make_sim_data ()
124+ sim_data = sim_data .updated_copy (diverged = True , log = "The simulation has diverged!" )
125+ file_path = os .path .join (tmp_path , "test_diverged.hdf5" )
126+ sim_data .to_file (file_path )
127+ Tidy3dStubData .postprocess (file_path )
128+
129+ # test warnings
130+ sim_data = make_sim_data ()
131+ sim_data = sim_data .updated_copy (log = "WARNING: messages were found in the solver log." )
132+ file_path = os .path .join (tmp_path , "test_warnings.hdf5" )
133+ sim_data .to_file (file_path )
134+ Tidy3dStubData .postprocess (file_path )
135+ finally :
136+ td .log .set_capture (False )
135137
136138
137139@responses .activate
138140def test_stub_data_lazy_loading (tmp_path ):
139141 """Tests the postprocess method with lazy loading of Tidy3dStubData when simulation diverged."""
140142 td .log .set_capture (True )
141143 sim_diverged_log = "The simulation has diverged!"
142-
143- # make sim data where test diverged
144- sim_data = make_sim_data ()
145- sim_data = sim_data .updated_copy (diverged = True , log = sim_diverged_log )
146- file_path = os .path .join (tmp_path , "test_diverged.hdf5" )
147- sim_data .to_file (file_path )
148-
149- # default case with lazy=False should output a warning
150- with AssertLogLevel ("WARNING" , contains_str = sim_diverged_log ):
151- Tidy3dStubData .postprocess (file_path , lazy = False )
152-
153- # we expect no warning in lazy mode as object should not be loaded
154- with AssertLogLevel (None ):
155- sim_data = Tidy3dStubData .postprocess (file_path , lazy = True )
156-
157- sim_data_copy = sim_data .copy ()
158-
159- # variable dict should only contain metadata to load the data, not the data itself
160- assert is_lazy_object (sim_data )
161-
162- # the type should be still SimulationData despite being lazy
163- assert isinstance (sim_data , SimulationData )
164-
165- # we expect a warning from the lazy object if some field is accessed
166- with AssertLogLevel ("WARNING" , contains_str = sim_diverged_log ):
167- _ = sim_data_copy .monitor_data
144+ try :
145+ # make sim data where test diverged
146+ sim_data = make_sim_data ()
147+ sim_data = sim_data .updated_copy (diverged = True , log = sim_diverged_log )
148+ file_path = os .path .join (tmp_path , "test_diverged.hdf5" )
149+ sim_data .to_file (file_path )
150+
151+ # default case with lazy=False should output a warning
152+ with AssertLogLevel ("WARNING" , contains_str = sim_diverged_log ):
153+ Tidy3dStubData .postprocess (file_path , lazy = False )
154+
155+ # we expect no warning in lazy mode as object should not be loaded
156+ with AssertLogLevel (None ):
157+ sim_data = Tidy3dStubData .postprocess (file_path , lazy = True )
158+
159+ sim_data_copy = sim_data .copy ()
160+
161+ # variable dict should only contain metadata to load the data, not the data itself
162+ assert is_lazy_object (sim_data )
163+
164+ # the type should be still SimulationData despite being lazy
165+ assert isinstance (sim_data , SimulationData )
166+
167+ # we expect a warning from the lazy object if some field is accessed
168+ with AssertLogLevel ("WARNING" , contains_str = sim_diverged_log ):
169+ _ = sim_data_copy .monitor_data
170+ finally :
171+ td .log .set_capture (False )
168172
169173
170174@pytest .mark .parametrize (
0 commit comments