Skip to content

Instantly share code, notes, and snippets.

@wware
Created January 22, 2020 17:59
Show Gist options
  • Save wware/74032b45ed08027e28b6a84656ccd90c to your computer and use it in GitHub Desktop.
Save wware/74032b45ed08027e28b6a84656ccd90c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
To run these tests remotely with Ansible, add command line arguments:
./ansible_unit_tests.py -i inventory.yaml hostname
./ansible_unit_tests.py -i inventory.yaml group
To convert docstrings into a Markdown file, type:
./ansible_unit_tests.py markdown
"""
import os
import sys
import types
import unittest
class Foo(unittest.TestCase):
def test_1(self):
"""
This test always passes.
"""
self.assertTrue(True)
def test_2(self):
"""
This test always fails.
"""
self.assertTrue(False)
if __name__ == "__main__":
if len(sys.argv) == 1:
unittest.main()
elif sys.argv[1] == 'markdown':
print "# " + __file__
print __doc__
for testcase in filter(lambda x: isinstance(x, type) and issubclass(x, unittest.TestCase),
globals().values()):
print "## " + testcase.__name__ + "\n"
for m in filter(lambda x: x.startswith("test") and
isinstance(getattr(testcase, x, None),
types.MethodType), dir(testcase)):
m = getattr(testcase, m)
print "### " + m.__name__
print "\n".join([line[8:] for line in m.__doc__.split("\n")])
else:
os.system(
'ansible -m script -a "' + __file__ + ' executable=python" '
+ ' '.join(sys.argv[1:])
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment