2014-10-29 16:05:26 +00:00
|
|
|
|
|
|
|
import glob
|
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
|
2014-10-30 15:11:47 +00:00
|
|
|
import yaml
|
2014-10-29 16:05:26 +00:00
|
|
|
|
2014-10-30 15:11:47 +00:00
|
|
|
|
|
|
|
def build_test_method(test):
|
2014-10-29 16:05:26 +00:00
|
|
|
def test(self):
|
2014-10-30 15:11:47 +00:00
|
|
|
self.http_test(test)
|
2014-10-29 16:05:26 +00:00
|
|
|
return test
|
|
|
|
|
|
|
|
|
2014-10-30 15:11:47 +00:00
|
|
|
def load_yaml(yaml_file):
|
|
|
|
with open(yaml_file) as source:
|
|
|
|
return yaml.safe_load(source.read())
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-29 16:05:26 +00:00
|
|
|
class Builder(type):
|
|
|
|
|
|
|
|
def __new__(cls, name, bases, d):
|
|
|
|
|
2014-10-30 15:11:47 +00:00
|
|
|
test_data = load_yaml(d['data_file'])
|
|
|
|
|
|
|
|
for test in test_data:
|
|
|
|
test_name = test['name'].lower().replace(' ', '_')
|
|
|
|
d['test_%s' % test_name] = build_test_method(test)
|
2014-10-29 16:05:26 +00:00
|
|
|
|
|
|
|
return type.__new__(cls, name, bases, d)
|
|
|
|
|
|
|
|
|
|
|
|
class HTTPTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
print 'setup called', cls.data_file
|
|
|
|
|
2014-10-30 15:11:47 +00:00
|
|
|
def http_test(self, test):
|
|
|
|
self.assertTrue(test)
|
2014-10-29 16:05:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def build_tests(path, loader, tests, pattern):
|
|
|
|
suite = unittest.TestSuite()
|
|
|
|
|
|
|
|
path = '%s/*.yaml' % path
|
|
|
|
|
|
|
|
for test_file in glob.iglob(path):
|
|
|
|
test_name = os.path.splitext(os.path.basename(test_file))[0]
|
|
|
|
klass = Builder(test_name, (HTTPTestCase,), {'data_file': test_file})
|
|
|
|
tests = loader.loadTestsFromTestCase(klass)
|
|
|
|
suite.addTests(tests)
|
|
|
|
|
|
|
|
return suite
|