2014-10-29 16:05:26 +00:00
|
|
|
|
|
|
|
import glob
|
|
|
|
import os
|
|
|
|
|
2014-12-12 16:51:27 +00:00
|
|
|
import testtools
|
2014-10-30 15:11:47 +00:00
|
|
|
import yaml
|
2014-10-29 16:05:26 +00:00
|
|
|
|
2014-12-12 16:51:27 +00:00
|
|
|
from unittest import suite
|
|
|
|
|
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-12-12 14:44:33 +00:00
|
|
|
return type.__new__(cls, name, bases, d)
|
2014-10-29 16:05:26 +00:00
|
|
|
|
2014-10-30 15:11:47 +00:00
|
|
|
|
2014-12-12 16:51:27 +00:00
|
|
|
class HTTPTestCase(testtools.TestCase):
|
2014-10-29 16:05:26 +00:00
|
|
|
|
2014-12-12 16:51:27 +00:00
|
|
|
def setUp(self):
|
|
|
|
if not self.has_run:
|
|
|
|
super(HTTPTestCase, self).setUp()
|
2014-10-29 16:05:26 +00:00
|
|
|
|
2014-12-12 16:51:27 +00:00
|
|
|
def tearDown(self):
|
|
|
|
if not self.has_run:
|
|
|
|
super(HTTPTestCase, self).tearDown()
|
|
|
|
self.has_run = True
|
2014-10-29 16:05:26 +00:00
|
|
|
|
2014-12-12 16:51:27 +00:00
|
|
|
def runTest(self):
|
|
|
|
if self.has_run:
|
|
|
|
return
|
|
|
|
if self.prior and not self.prior.has_run:
|
|
|
|
self.prior.run()
|
|
|
|
self.assertTrue(self.test_data['url'])
|
2014-10-29 16:05:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def build_tests(path, loader, tests, pattern):
|
2014-12-12 16:51:27 +00:00
|
|
|
top_suite = suite.TestSuite()
|
2014-10-29 16:05:26 +00:00
|
|
|
|
|
|
|
path = '%s/*.yaml' % path
|
|
|
|
|
|
|
|
for test_file in glob.iglob(path):
|
2014-12-12 16:51:27 +00:00
|
|
|
file_suite = suite.TestSuite()
|
2014-12-12 14:44:33 +00:00
|
|
|
test_data = load_yaml(test_file)
|
|
|
|
test_base_name = os.path.splitext(os.path.basename(test_file))[0]
|
|
|
|
prior_test = None
|
|
|
|
for test in test_data:
|
|
|
|
test_name = '%s_%s' % (test_base_name,
|
|
|
|
test['name'].lower().replace(' ', '_'))
|
|
|
|
klass = Builder(test_name, (HTTPTestCase,),
|
2014-12-12 16:51:27 +00:00
|
|
|
{'test_data': test,
|
|
|
|
'prior': prior_test,
|
|
|
|
'has_run': False})
|
2014-12-12 14:44:33 +00:00
|
|
|
tests = loader.loadTestsFromTestCase(klass)
|
|
|
|
this_test = tests._tests[0]
|
|
|
|
file_suite.addTest(this_test)
|
|
|
|
prior_test = this_test
|
|
|
|
top_suite.addTest(file_suite)
|
|
|
|
|
|
|
|
return top_suite
|