Skip to content

Instantly share code, notes, and snippets.

@grrussel
Created February 3, 2011 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grrussel/809201 to your computer and use it in GitHub Desktop.
Save grrussel/809201 to your computer and use it in GitHub Desktop.
Simple python 2.x unit test system which dynamically builds list of tests to run
# Improvement over https://gist.github.com/803854
import optparse
def test_function1():
# Do some test code
# return True on Pass, False on Fail
return True
def test_function2_EXPECTED_FAIL():
# Do some test (more) code
# return True on Pass, False on Fail
return False
def test_functionN():
# Do some test (more) code
# return True on Pass, False on Fail
return False
def main():
parser = optparse.OptionParser()
parser.add_option('-e', '--exit-on-error', dest='exit_on_error',
default=False, action="store_true",
help='Stop running tests on first failure occurring')
parser.add_option('-v', '--verbose', dest='verbose',
default=False, action="store_true",
help='Print each tests name before running')
(opts, args) = parser.parse_args()
tests = []
potential_tests = globals()
# Build list of "test" functions
# here, all functions in the module
for candidate in potential_tests:
# Avoid invoking main, recursively...
if candidate == "main":
continue
elif callable(potential_tests[candidate]):
tests.append(potential_tests[candidate])
failed = []
for t in tests:
if opts.verbose:
print t.__name__
if not t():
if t.__name__.endswith("_EXPECTED_FAIL"):
continue
print t.__name__, "Failed"
failed.append(t.__name__)
if opts.exit_on_error:
return 1
for f in failed :
print "FAIL:",f
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment