Added test generator (not quite finished yet)

This commit is contained in:
Alan Saul 2013-03-11 17:05:18 +00:00
parent 9b4cb78fdb
commit c39af496a6
3 changed files with 58 additions and 26 deletions

View file

@ -4,23 +4,63 @@
import unittest
import numpy as np
import GPy
import inspect
import pkgutil
import os
class ExamplesTests(unittest.TestCase):
def test_check_model_returned(self):
pass
def _checkgrad(self, model):
self.assertTrue(model.checkgrad())
def test_model_checkgrads(self):
pass
def _model_instance(self, model):
self.assertTrue(isinstance(model, GPy.models))
def test_all_examples(self):
examples_module = __import__("GPy").examples
#Load models
"""
def model_instance_generator(model):
def check_model_returned(self):
self._model_instance(model)
return check_model_returned
#Loop through models
#for model in models:
#self.assertTrue(m.checkgrad())
def checkgrads_generator(model):
def model_checkgrads(self):
self._checkgrad(model)
return model_checkgrads
"""
def model_checkgrads(model):
assert model.checkgrad() is True
def model_instance(model):
assert model.checkgrad() is True
def test_models():
examples_path = os.path.dirname(GPy.examples.__file__)
#Load modules
for loader, module_name, is_pkg in pkgutil.iter_modules([examples_path]):
#Load examples
module_examples = loader.find_module(module_name).load_module(module_name)
functions = [ func for func in [inspect.getmembers(module_examples, predicate=inspect.isfunction)[0]] if func[0].startswith('_') is False ]
for example in functions:
print "Testing example: ", example[0]
#Generate model
model = example[1]()
print model
#Create tests for instance check
"""
test = model_instance_generator(model)
test.__name__ = 'test_instance_%s' % example[0]
setattr(ExamplesTests, test.__name__, test)
#Create tests for checkgrads check
test = checkgrads_generator(model)
test.__name__ = 'test_checkgrads_%s' % example[0]
setattr(ExamplesTests, test.__name__, test)
"""
model_checkgrads.description = 'test_checkgrads_%s' % example[0]
yield model_checkgrads, model
model_instance.description = 'test_checkgrads_%s' % example[0]
yield model_instance, model
if __name__ == "__main__":
print "Running unit tests, please be (very) patient..."
unittest.main()