forked from cloudtools/troposphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_examples.py
57 lines (44 loc) · 1.51 KB
/
test_examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import io
import os
import re
import sys
import unittest
class TestExamples(unittest.TestCase):
maxDiff = None
# those are set by create_test_class
filename = None
expected_output = None
def test_example(self):
saved = sys.stdout
stdout = io.StringIO()
try:
sys.stdout = stdout
with open(self.filename) as f:
code = compile(f.read(), self.filename, "exec")
exec(code, {"__name__": "__main__"})
finally:
sys.stdout = saved
# rewind fake stdout so we can read it
stdout.seek(0)
actual_output = stdout.read()
self.assertEqual(str(self.expected_output), str(actual_output))
def create_test_class(testname, **kwargs):
klass = type(testname, (TestExamples,), kwargs)
return klass
def load_tests(loader, tests, pattern):
# Filter out all *.py files from the examples directory
examples = "examples"
regex = re.compile(r".py$", re.I)
example_filesnames = filter(regex.search, os.listdir(examples))
suite = unittest.TestSuite()
for f in example_filesnames:
testname = "test_" + f[:-3]
expected_output = open("tests/examples_output/%s.template" % f[:-3]).read()
test_class = create_test_class(
testname, filename=examples + "/" + f, expected_output=expected_output
)
tests = loader.loadTestsFromTestCase(test_class)
suite.addTests(tests)
return suite
if __name__ == "__main__":
unittest.main()