Source code for cis_interface.examples.tests
import os
import warnings
import unittest
from cis_interface import runner
from cis_interface.examples import yamls
from cis_interface.config import cis_cfg
[docs]class TestExample(unittest.TestCase):
r"""Base class for running examples."""
def __init__(self, *args, **kwargs):
self.name = None
self.language = None
self.env = {}
super(TestExample, self).__init__(*args, **kwargs)
[docs] def setup(self, *args, **kwargs):
r"""Setup to perform before test."""
cis_cfg.set('debug', 'psi', 'INFO')
cis_cfg.set('debug', 'rmq', 'INFO')
cis_cfg.set('debug', 'client', 'INFO')
[docs] def teardown(self, *args, **kwargs):
r"""Teardown to perform after test."""
pass
[docs] def shortDescription(self):
r"""Prefix first line of doc string with driver."""
out = super(TestExample, self).shortDescription()
return '%s: %s' % (self.name, out)
[docs] def setUp(self, *args, **kwargs):
r"""Redirect unittest to nose style setup."""
self.setup(*args, **kwargs)
[docs] def tearDown(self, *args, **kwargs):
r"""Redirect unittest to nose style teardown."""
self.teardown(*args, **kwargs)
@property
def yaml(self):
r"""str: The full path to the yaml file for this example."""
if self.name not in yamls:
return None
if self.language not in yamls[self.name]:
return None
return yamls[self.name][self.language]
@property
def yamldir(self):
r"""str: Full path to the directory containing the yaml file."""
if self.yaml is None:
return None
return os.path.dirname(self.yaml)
[docs] def check_result(self):
r"""This should be overridden with checks for the result."""
pass
[docs] def run_example(self):
r"""This runs an example in the correct language."""
if self.yaml is None:
if self.name is not None:
warnings.warn("Could not locate example %s in language %s." %
(self.name, self.language))
else:
os.environ.update(self.env)
cr = runner.get_runner(self.yaml, namespace=self.name)
cr.run()
self.check_result()
[docs] def test_all(self):
r"""Test the version of the example that uses all languages."""
self.language = 'all'
self.run_example()
self.language = None
[docs] def test_python(self):
r"""Test the Python version of the example."""
self.language = 'python'
self.run_example()
self.language = None
[docs] def test_c(self):
r"""Test the C version of the example."""
self.language = 'c'
self.run_example()
self.language = None
[docs] def test_cpp(self):
r"""Test the C++ version of the example."""
self.language = 'cpp'
self.run_example()
self.language = None
[docs] def test_matlab(self):
r"""Test the Matlab version of the example."""
self.language = 'matlab'
self.run_example()
self.language = None