diff --git a/tardis/spectrum/tests/test_spectrum_solver.py b/tardis/spectrum/tests/test_spectrum_solver.py index bb82e5d822c..941a34eefa0 100644 --- a/tardis/spectrum/tests/test_spectrum_solver.py +++ b/tardis/spectrum/tests/test_spectrum_solver.py @@ -32,9 +32,10 @@ def simulation( simulation.run_final() request.cls.regression_data = RegressionData(request) - request.cls.regression_data.sync_hdf_store(simulation) + data = request.cls.regression_data.sync_hdf_store(simulation) - return simulation + yield simulation + data.close() def get_expected_data(self, key: str): return pd.read_hdf(self.regression_data.fpath, key) diff --git a/tardis/tests/fixtures/regression_data.py b/tardis/tests/fixtures/regression_data.py index 064d33ef1f9..f25aa86d96b 100644 --- a/tardis/tests/fixtures/regression_data.py +++ b/tardis/tests/fixtures/regression_data.py @@ -24,6 +24,7 @@ def __init__(self, request) -> None: "--generate-reference" ) self.fname = f"{self.fname_prefix}.UNKNOWN_FORMAT" + self.hdf_store_object = None @property def module_name(self): @@ -161,9 +162,14 @@ def sync_hdf_store(self, tardis_module, update_fname=True): f"Skipping test to generate regression data: {self.fpath}" ) else: - return pd.HDFStore(self.fpath, mode="r") - + # since each test function has its own regression data instance + # each test function will only have one HDFStore object + self.hdf_store_object = pd.HDFStore(self.fpath, mode="r") + return self.hdf_store_object @pytest.fixture(scope="function") def regression_data(request): - return RegressionData(request) + regression_data_instance = RegressionData(request) + yield regression_data_instance + if regression_data_instance.hdf_store_object is not None and regression_data_instance.hdf_store_object.is_open: + regression_data_instance.hdf_store_object.close() diff --git a/tardis/tests/test_tardis_full.py b/tardis/tests/test_tardis_full.py index 92a6a6fba4c..80ac724a84d 100644 --- a/tardis/tests/test_tardis_full.py +++ b/tardis/tests/test_tardis_full.py @@ -55,9 +55,10 @@ def simulation( simulation.run_final() request.cls.regression_data = RegressionData(request) - request.cls.regression_data.sync_hdf_store(simulation) + data = request.cls.regression_data.sync_hdf_store(simulation) - return simulation + yield simulation + data.close() def get_expected_data(self, key: str): return pd.read_hdf(self.regression_data.fpath, key) diff --git a/tardis/tests/test_tardis_full_formal_integral.py b/tardis/tests/test_tardis_full_formal_integral.py index c981c46f097..ca3024047ff 100644 --- a/tardis/tests/test_tardis_full_formal_integral.py +++ b/tardis/tests/test_tardis_full_formal_integral.py @@ -73,7 +73,8 @@ def simulation(self, config, atomic_data_fname): def test_simulation(self, simulation, request): regression_data = RegressionData(request) container = SimulationContainer(simulation) - regression_data.sync_hdf_store(container) + data = regression_data.sync_hdf_store(container) + data.close() def test_j_blue_estimators(self, simulation, request): regression_data = RegressionData(request) diff --git a/tardis/visualization/tools/tests/test_liv_plot.py b/tardis/visualization/tools/tests/test_liv_plot.py index 5a4f56897bc..ae50f66f8ee 100644 --- a/tardis/visualization/tools/tests/test_liv_plot.py +++ b/tardis/visualization/tools/tests/test_liv_plot.py @@ -357,6 +357,7 @@ def test_generate_plot_mpl( + str(index2) ), ) + expected.close() def test_mpl_image(self, plotter_generate_plot_mpl, tmp_path, request): """ @@ -520,3 +521,4 @@ def test_generate_plot_ply( rtol=0.3, atol=3, ) + expected.close() diff --git a/tardis/visualization/tools/tests/test_sdec_plot.py b/tardis/visualization/tools/tests/test_sdec_plot.py index 1136ba7e148..2a11b00289d 100644 --- a/tardis/visualization/tools/tests/test_sdec_plot.py +++ b/tardis/visualization/tools/tests/test_sdec_plot.py @@ -231,6 +231,7 @@ def test_calculate_plotting_data( pd.testing.assert_frame_equal( plot_object, expected.get(group + attribute_name) ) + expected.close() @pytest.fixture(scope="class", params=combinations) def plotter_generate_plot_mpl(self, request, observed_spectrum, plotter): @@ -331,6 +332,7 @@ def test_generate_plot_mpl( + str(index2) ), ) + expected.close() @pytest.fixture(scope="class", params=combinations) def plotter_generate_plot_ply(self, request, observed_spectrum, plotter): @@ -416,6 +418,8 @@ def test_generate_plot_mpl( data.y, expected.get(group + "y").values.flatten() ) + expected.close() + def test_mpl_image(self, plotter_generate_plot_mpl, tmp_path, request): regression_data = RegressionData(request) fig, _ = plotter_generate_plot_mpl