Skip to content

Commit

Permalink
Merge pull request #653 from karandesai-96/atom-data-and-report-local
Browse files Browse the repository at this point in the history
Facility to provide per-setup atom data as well as save report locally.
  • Loading branch information
wkerzendorf authored Aug 22, 2016
2 parents bea4d64 + c5a3c7a commit 3c90f9d
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 85 deletions.
24 changes: 21 additions & 3 deletions tardis/tests/integration_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from tardis import __githash__ as tardis_githash
from tardis.tests.integration_tests.report import DokuReport
from tardis.tests.integration_tests.plot_helpers import PlotUploader
from tardis.tests.integration_tests.plot_helpers import LocalPlotSaver, RemotePlotSaver


def pytest_configure(config):
Expand All @@ -24,7 +24,7 @@ def pytest_configure(config):
# prevent opening dokupath on slave nodes (xdist)
if not hasattr(config, 'slaveinput'):
config.dokureport = DokuReport(
config.integration_tests_config['dokuwiki'])
config.integration_tests_config['report'])
config.pluginmanager.register(config.dokureport)


Expand Down Expand Up @@ -59,7 +59,15 @@ def pytest_runtest_makereport(item, call):

@pytest.fixture(scope="function")
def plot_object(request):
return PlotUploader(request)
integration_tests_config = request.config.integration_tests_config
report_save_mode = integration_tests_config['report']['save_mode']

if report_save_mode == "remote":
return RemotePlotSaver(request, request.config.dokureport.dokuwiki_url)
else:
return LocalPlotSaver(request, os.path.join(
request.config.dokureport.report_dirpath, "assets")
)


@pytest.fixture(scope="class", params=[
Expand All @@ -80,6 +88,16 @@ def data_path(request):
),
'setup_name': hdf_filename[:-3]
}

# For providing atom data per individual setup. Atom data can be fetched
# from a local directory or a remote url.
if integration_tests_config['atom_data']['fetch'] == "remote":
path['atom_data_url'] = integration_tests_config['atom_data']['url']
elif integration_tests_config['atom_data']['fetch'] == "local":
path['atom_data_dirpath'] = os.path.expandvars(os.path.expanduser(
integration_tests_config['atom_data']['dirpath']
))

if (request.config.getoption("--generate-reference") and not
os.path.exists(path['gen_ref_dirpath'])):
os.makedirs(path['gen_ref_dirpath'])
Expand Down
126 changes: 89 additions & 37 deletions tardis/tests/integration_tests/plot_helpers.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
import os
import tempfile

from pytest_html import extras
import tardis
from tardis import __githash__ as tardis_githash


thumbnail_html = """
<div class="image" style="float: left">
<a href="#">
<img src= "{dokuwiki_url}lib/exe/fetch.php?media=reports:{githash}:{name}.png" />
</a>
</div>
"""


class PlotUploader(object):
def __init__(self, request):
"""A helper class to collect plots from integration tests and upload
them to DokuWiki.
class BasePlotSaver(object):
def __init__(self, request, dokuwiki_url=None, assets_dirpath=None):
"""Base Class for RemotePlotSaver and LocalPlotSaver classes.
These help in uploading plots to DokuWiki instance or saving them
locally in a directory.
Parameters
----------
request : _pytest.python.RequestObject
dokuwiki_url : str
assets_dirpath : str
"""
self.request = request
self._plots = list()
self.plot_html = list()
self.dokuwiki_url = self.request.config.dokureport.dokuwiki_url
self.dokuwiki_url = dokuwiki_url
self.assets_dirpath = assets_dirpath

def add(self, plot, name):
"""Accept a plot figure and add it to ``self._plots``.
Expand All @@ -39,8 +34,52 @@ def add(self, plot, name):
"""
self._plots.append((plot, name))

def save(self, plot, filepath, report):
"""Mark pass / fail and save a plot with ``name`` to ``filepath``.
Parameters
----------
plot : matplotlib.pyplot.figure
filepath : str
report : _pytest.runner.TestReport
"""
axes = plot.axes[0]

if report.passed:
axes.text(0.8, 0.8, 'passed', transform=axes.transAxes,
bbox={'facecolor': 'green', 'alpha': 0.5, 'pad': 10})
else:
axes.text(0.8, 0.8, 'failed', transform=axes.transAxes,
bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 10})

plot.savefig(filepath)

def get_extras(self):
"""Return ``self.plot_html`` which is further added into html report.
Returns
-------
list
List of strings containing raw html snippet to embed images.
"""
return self.plot_html


thumbnail_html_remote = """
<div class="image" style="float: left">
<a href="#">
<img src= "{dokuwiki_url}lib/exe/fetch.php?media=reports:{githash}:{name}.png" />
</a>
</div>
"""


class RemotePlotSaver(BasePlotSaver):
def __init__(self, request, dokuwiki_url):
super(RemotePlotSaver, self).__init__(request, dokuwiki_url=dokuwiki_url)

def upload(self, report):
"""Upload the content in self._plots to dokuwiki.
"""Upload content of ``self._plots`` to ``self.dokuwiki_url``.
Parameters
----------
Expand All @@ -50,37 +89,50 @@ def upload(self, report):

for plot, name in self._plots:
plot_file = tempfile.NamedTemporaryFile(suffix=".png")
axes = plot.axes[0]

if report.passed:
axes.text(0.8, 0.8, 'passed', transform=axes.transAxes,
bbox={'facecolor': 'green', 'alpha': 0.5, 'pad': 10})
else:
axes.text(0.8, 0.8, 'failed', transform=axes.transAxes,
bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 10})

plot.savefig(plot_file.name)
self.save(plot, plot_file.name, report)

self.request.config.dokureport.doku_conn.medias.add(
"reports:{0}:{1}.png".format(tardis.__githash__[0:7], name),
"reports:{0}:{1}.png".format(tardis_githash[:7], name),
plot_file.name
)

self.plot_html.append(extras.html(
thumbnail_html.format(
thumbnail_html_remote.format(
dokuwiki_url=self.dokuwiki_url,
githash=tardis.__githash__[0:7],
githash=tardis_githash[:7],
name=name)
)
)
plot_file.close()

def get_extras(self):
"""Return ``self.plot_html`` which is further added into html report.

Returns
-------
list
List of strings containing raw html snippet to embed images.
thumbnail_html_local = """
<div class="image" style="float: left">
<a href="#">
<img src= "assets/{name}.png" />
</a>
</div>
"""


class LocalPlotSaver(BasePlotSaver):
def __init__(self, request, assets_dirpath):
super(LocalPlotSaver, self).__init__(request, assets_dirpath=assets_dirpath)

def upload(self, report):
"""Save content of ``self._plots`` to ``self.assets_dirpath``.
Parameters
----------
report : _pytest.runner.TestReport
"""
return self.plot_html

for plot, name in self._plots:
self.save(plot, os.path.join(
self.assets_dirpath, "{0}.png".format(name)), report
)

self.plot_html.append(extras.html(
thumbnail_html_local.format(name=name))
)
Loading

0 comments on commit 3c90f9d

Please sign in to comment.