diff --git a/tardis/conftest.py b/tardis/conftest.py index f94ee8e6039..9ee1fcb954f 100644 --- a/tardis/conftest.py +++ b/tardis/conftest.py @@ -4,30 +4,15 @@ from astropy.tests.pytest_plugins import * from astropy.tests.pytest_plugins import ( - pytest_addoption as _pytest_add_option, - pytest_configure as _pytest_configure, - pytest_unconfigure as _pytest_unconfigure - ) + pytest_addoption as _pytest_add_option + ) -import yaml -import tempfile import tardis import pytest from tardis.atomic import AtomData from tardis.io.config_reader import Configuration from tardis.io.util import yaml_load_config_file -# For specifying error while exception handling -from socket import gaierror - -try: - import dokuwiki -except ImportError: - dokuwiki_available = False -else: - dokuwiki_available = True - - ### # Astropy ### @@ -86,45 +71,6 @@ def pytest_addoption(parser): help="path to configuration file for integration tests") -def pytest_configure(config): - _pytest_configure(config) - html_file = tempfile.NamedTemporaryFile(delete=False) - # Html test report will be generated at this filepath by pytest-html plugin - config.option.htmlpath = html_file.name - - -def pytest_unconfigure(config): - _pytest_unconfigure() - # Html report created by pytest-html plugin is read here, uploaded to - # dokuwiki and finally deleted. - if dokuwiki_available: - githash = tardis.__githash__ - report_content = open(config.option.htmlpath, 'rb').read() - report_content = report_content.replace("", "") - - report_content = ( - "Test executed on commit " - "[[https://www.github.com/tardis-sn/tardis/commit/{0}|{0}]]\n\n" - "{1}".format(githash, report_content) - ) - - try: - doku_conn = dokuwiki.DokuWiki( - url=integration_tests_config["dokuwiki"]["url"], - user=integration_tests_config["dokuwiki"]["username"], - password=integration_tests_config["dokuwiki"]["password"]) - except gaierror, dokuwiki.DokuWikiError: - print "Dokuwiki connection not established, report upload failed!" - else: - # Upload report on dokuwiki. Temporary link due to prototyping purposes. - doku_conn.pages.append("reports:{0}".format(githash[:7]), report_content) - print "Uploaded report on Dokuwiki." - - # Remove the local report file. Keeping the report saved on local filesystem - # is not desired, hence deleted. - os.unlink(config.option.htmlpath) - print "Deleted temporary file containing html report." - # ------------------------------------------------------------------------- # project specific fixtures # ------------------------------------------------------------------------- @@ -165,15 +111,3 @@ def included_he_atomic_data(test_data_path): def tardis_config_verysimple(): return yaml_load_config_file( 'tardis/io/tests/data/tardis_configv1_verysimple.yml') - - -@pytest.fixture(scope="session") -def integration_tests_config(request): - integration_tests_configpath = request.config.getvalue("integration-tests") - if integration_tests_configpath is None: - pytest.skip('--integration-tests was not specified') - else: - integration_tests_configpath = os.path.expandvars( - os.path.expanduser(integration_tests_configpath) - ) - return yaml.load(open(integration_tests_configpath)) diff --git a/tardis/tests/tests_slow/conftest.py b/tardis/tests/tests_slow/conftest.py index 1cbd62ef26a..3efc5c747b2 100644 --- a/tardis/tests/tests_slow/conftest.py +++ b/tardis/tests/tests_slow/conftest.py @@ -1,9 +1,87 @@ import os +import shutil +import tempfile +import yaml import numpy as np import pytest from astropy import units as u +from astropy.tests.helper import remote_data import tardis +# For specifying error while exception handling +from socket import gaierror + +try: + import dokuwiki +except ImportError: + dokuwiki_available = False +else: + dokuwiki_available = True + + +def pytest_configure(config): + integration_tests_configpath = config.getvalue("integration-tests") + if integration_tests_configpath is not None: + integration_tests_configpath = os.path.expandvars( + os.path.expanduser(integration_tests_configpath) + ) + config.option.integration_tests_config = yaml.load( + open(integration_tests_configpath)) + + # A common tempdir for storing plots / PDFs and other slow test related data + # generated during execution. + tempdir_session = tempfile.mkdtemp() + config.option.tempdir = tempdir_session + html_file = tempfile.NamedTemporaryFile(delete=False) + # Html test report will be generated at this filepath by pytest-html plugin + config.option.htmlpath = html_file.name + + +@remote_data +def pytest_unconfigure(config): + integration_tests_configpath = config.getvalue("integration-tests") + if integration_tests_configpath is not None: + # Html report created by pytest-html plugin is read here, uploaded to + # dokuwiki and finally deleted. + if dokuwiki_available: + githash = tardis.__githash__ + report_content = open(config.option.htmlpath, 'rb').read() + report_content = report_content.replace("", "") + + report_content = ( + "Test executed on commit " + "[[https://www.github.com/tardis-sn/tardis/commit/{0}|{0}]]\n\n" + "{1}".format(githash, report_content) + ) + + # These steps are already performed by `integration_tests_config` but + # all the fixtures are teared down and no longer usable, when this + # method is being called by pytest, hence they are called as functions. + integration_tests_config = config.option.integration_tests_config + try: + doku_conn = dokuwiki.DokuWiki( + url=integration_tests_config["dokuwiki"]["url"], + user=integration_tests_config["dokuwiki"]["username"], + password=integration_tests_config["dokuwiki"]["password"]) + except gaierror, dokuwiki.DokuWikiError: + print "Dokuwiki connection not established, report upload failed!" + else: + # Upload report on dokuwiki. Temporary link due to prototyping purposes. + doku_conn.pages.set("reports:{0}".format(githash[:7]), report_content) + print "Uploaded report on Dokuwiki." + + # Remove the local report file. Keeping the report saved on local filesystem + # is not desired, hence deleted. + os.unlink(config.option.htmlpath) + print "Deleted temporary file containing html report." + # Remove tempdir by recursive deletion + shutil.rmtree(config.option.tempdir) + + +@pytest.fixture(scope="session") +def integration_tests_config(request): + return request.config.option.integration_tests_config + @pytest.fixture(scope="session") def reference_datadir(integration_tests_config): @@ -17,19 +95,6 @@ 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, reference_datadir): """ diff --git a/tardis/tests/tests_slow/test_w7.py b/tardis/tests/tests_slow/test_w7.py index 48cbb8d047d..93823334d6a 100644 --- a/tardis/tests/tests_slow/test_w7.py +++ b/tardis/tests/tests_slow/test_w7.py @@ -11,6 +11,8 @@ from tardis.io.config_reader import Configuration +@pytest.mark.skipif(not pytest.config.getvalue("integration-tests"), + reason="integration tests are not included in this run") class TestW7(object): """ Slow integration test for Stratified W7 setup. @@ -19,7 +21,7 @@ class TestW7(object): @classmethod @pytest.fixture(scope="class", autouse=True) def setup(self, request, reference, data_path, atomic_data_fname, - reference_datadir, base_plot_dir): + reference_datadir): """ This method does initial setup of creating configuration and performing a single run of integration test. @@ -60,7 +62,7 @@ def setup(self, request, reference, data_path, atomic_data_fname, # 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) + self.base_plot_dir = os.path.join(request.config.option.tempdir, self.name) os.makedirs(self.base_plot_dir) def test_j_estimators(self):