Skip to content

Instantly share code, notes, and snippets.

@barahilia
Created October 6, 2016 05:59
Show Gist options
  • Save barahilia/a1d31615854de71254b7f2eccebd774b to your computer and use it in GitHub Desktop.
Save barahilia/a1d31615854de71254b7f2eccebd774b to your computer and use it in GitHub Desktop.
from unittest import TestLoader, TextTestRunner
def discover_and_run(verbose=False, names=None):
# names is None or a list [name, ...]
# name is test_suite[.class[.name]]
verbosity = 2 if verbose else 1
loader = TestLoader()
if names:
tests = loader.loadTestsFromNames(names)
else:
tests = loader.discover('test', '*.py', '..')
testRunner = TextTestRunner(verbosity=verbosity)
run_result = testRunner.run(tests)
# Report to shell the exit status
exit(0 if run_result.wasSuccessful() else 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment