From fa303e68b6a9c981631c4b08033a234a0f2c97aa Mon Sep 17 00:00:00 2001 From: Karan Desai Date: Tue, 24 May 2016 18:00:10 +0530 Subject: [PATCH] Add fixture for base directory of saving plots. - Use this fixture in TestW7, by creating per-setup subdirectories (currently only W7, would be enhanced by parametrization later). - Directories are named as per git commit hashes. --- tardis/tests/tests_slow/conftest.py | 14 ++++++++++++++ tardis/tests/tests_slow/test_w7.py | 10 ++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/tardis/tests/tests_slow/conftest.py b/tardis/tests/tests_slow/conftest.py index 428bd9d2ba5..1a92225a1c8 100644 --- a/tardis/tests/tests_slow/conftest.py +++ b/tardis/tests/tests_slow/conftest.py @@ -2,6 +2,7 @@ import numpy as np import pytest from astropy import units as u +import tardis @pytest.fixture(scope="session") @@ -18,6 +19,19 @@ def data_path(): return os.path.join(os.path.dirname(os.path.realpath(__file__)), "w7") +@pytest.fixture(scope="session") +def base_plot_dir(): + githash_short = tardis.__githash__[0:7] + base_plot_dir = os.path.join("/tmp", "plots", githash_short) + + # Remove plots generated from previous runs on same githash. + if os.path.exists(base_plot_dir): + os.rmdir(base_plot_dir) + + os.makedirs(base_plot_dir) + return base_plot_dir + + @pytest.fixture(scope="session") def reference(request, slow_tests_datadir): """ diff --git a/tardis/tests/tests_slow/test_w7.py b/tardis/tests/tests_slow/test_w7.py index 981e4e9995b..870edfdcfdf 100644 --- a/tardis/tests/tests_slow/test_w7.py +++ b/tardis/tests/tests_slow/test_w7.py @@ -20,7 +20,7 @@ class TestW7(object): @classmethod @pytest.fixture(scope="class", autouse=True) def setup(self, request, reference, data_path, atomic_data_fname, - slow_tests_datadir): + slow_tests_datadir, base_plot_dir): """ This method does initial setup of creating configuration and performing a single run of integration test. @@ -58,6 +58,12 @@ def setup(self, request, reference, data_path, atomic_data_fname, # Get the reference data through the fixture. self.reference = reference + # Form a base directory to save plots for `W7` setup. + # TODO: Rough prototyping, parametrize this as more setups are added. + self.name = "w7" + self.base_plot_dir = os.path.join(base_plot_dir, self.name) + os.makedirs(self.base_plot_dir) + def test_j_estimators(self): assert_allclose( self.reference['j_estimators'], @@ -152,7 +158,7 @@ def plot_spectrum(self, passed): # Figure is saved in `tmp` directory right now, till a suitable way of # saving them is decided. - plt.savefig(os.path.join(self.plot_savedir, "spectrum.png")) + plt.savefig(os.path.join(self.base_plot_dir, "spectrum.png")) def test_montecarlo_properties(self): assert_quantity_allclose(