Skip to content

Commit

Permalink
Convert to using pytest instead of "setup.py test"
Browse files Browse the repository at this point in the history
  • Loading branch information
markpeek committed Aug 2, 2024
1 parent 3a1ff77 commit 69c43e0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 89 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ spec:
ln -f CloudFormationResourceSpecification.json .backup/CloudFormationResourceSpecification_$${SPECVERSION}.json

test: ## run tests
@python setup.py test
@pytest tests
64 changes: 23 additions & 41 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,38 @@
import os
import re
import sys
import unittest


class TestExamples(unittest.TestCase):
maxDiff = None
def test_example(input_filename, expected_output):
saved = sys.stdout
stdout = io.StringIO()
try:
sys.stdout = stdout
with open(input_filename) as f:
code = compile(f.read(), input_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()
assert str(expected_output) == str(actual_output)

# 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):
def load_example_tests():
# 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()
example_filenames = filter(regex.search, os.listdir(examples))

for f in example_filesnames:
testname = "test_" + f[:-3]
results = []
for f in example_filenames:
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)
results.append((examples + "/" + f, expected_output))

return suite
return results


if __name__ == "__main__":
unittest.main()
def pytest_generate_tests(metafunc):
example_data = load_example_tests()
metafunc.parametrize("input_filename,expected_output", example_data)
60 changes: 24 additions & 36 deletions tests/test_examples_template_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,43 @@
from troposphere.template_generator import TemplateGenerator


class TestTemplateGenerator(unittest.TestCase):
maxDiff = None
def test_template_generator(input_filename, expected_output):
"""
Ensures that all example outputs can be loaded into the
template generator and back to JSON with no difference.
"""
# we first get both outputs as JSON strings
template = expected_output
generated = TemplateGenerator(json.loads(template)).to_json()

# those are set by create_test_class
filename = None
expected_output = None
# then we make them into a dict for comparison
template = json.loads(template)
generated = json.loads(generated)

def test_template_generator(self):
"""
Ensures that all example outputs can be loaded into the
template generator and back to JSON with no difference.
"""
# we first get both outputs as JSON strings
template = self.expected_output
generated = TemplateGenerator(json.loads(template)).to_json()
assert template == generated

# then we make them into a dict for comparison
template = json.loads(template)
generated = json.loads(generated)

self.assertDictEqual(template, generated)


def create_test_class(testname, **kwargs):
klass = type(testname, (TestTemplateGenerator,), kwargs)
return klass


def load_tests(loader, tests, pattern):
def load_example_tests():
EXCLUDE_EXAMPLES = ["OpenStack_AutoScaling.py", "OpenStack_Server.py"]
# 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()

results = []
for f in example_filesnames:
if f in EXCLUDE_EXAMPLES:
continue
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
filename = "tests/examples_output/%s.template" % f[:-3]
expected_output = open(filename).read()
results.append((filename, expected_output))

return results


def pytest_generate_tests(metafunc):
example_data = load_example_tests()
metafunc.parametrize("input_filename,expected_output", example_data)


if __name__ == "__main__":
Expand Down
11 changes: 0 additions & 11 deletions tests/test_serverless.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,17 +477,6 @@ def test_globals(self):
FunctionGlobals(MemorySize=64)

def test_api_event_auth(self):
# Verify deprecated Auth
api_event = ApiEvent(
"SomeApiEvent",
Auth=Auth(),
Path="some path",
Method="some method",
)
t = Template()
t.add_resource(api_event)
t.to_json()

# Verify ApiFunctionAuth
api_event = ApiEvent(
"SomeApiEvent",
Expand Down

0 comments on commit 69c43e0

Please sign in to comment.