Created
February 3, 2011 08:24
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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