Skip to content

Commit

Permalink
Shift slow test related pytest hooks in lower level conftest.py (#578)
Browse files Browse the repository at this point in the history
* Move slow test related methods to lower level conftest.py

- They conflicted with astropy's test plugins which
  were imported as *.

* Use session wide temp director to store plots.

* Use remote_data marker on pytest_unconfigure.

* Shift unpacking of integration tests config in pytest_configure.

* Cosmetic change - remove unused imports from conftest.py

* Add a check in pytest_unconfigure for integration tests.
  • Loading branch information
Karan Desai authored and wkerzendorf committed Jun 8, 2016
1 parent 8ab523b commit fc0d4e9
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 83 deletions.
70 changes: 2 additions & 68 deletions tardis/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
###
Expand Down Expand Up @@ -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("<!DOCTYPE html>", "")

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
# -------------------------------------------------------------------------
Expand Down Expand Up @@ -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))
91 changes: 78 additions & 13 deletions tardis/tests/tests_slow/conftest.py
Original file line number Diff line number Diff line change
@@ -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("<!DOCTYPE html>", "")

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):
Expand All @@ -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):
"""
Expand Down
6 changes: 4 additions & 2 deletions tardis/tests/tests_slow/test_w7.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit fc0d4e9

Please sign in to comment.