Skip to content

Instantly share code, notes, and snippets.

@neekey
Created June 24, 2017 00:47
Show Gist options
  • Save neekey/fb679b7f94262fc0feeb3a3b093bb392 to your computer and use it in GitHub Desktop.
Save neekey/fb679b7f94262fc0feeb3a3b093bb392 to your computer and use it in GitHub Desktop.
"""Run the unit tests."""
import unittest
import sys
def iterate_test_cases(test_suite_or_case):
"""Iterate through all of the test cases in 'test_suite_or_case'."""
try:
suite = iter(test_suite_or_case)
except TypeError:
yield test_suite_or_case
else:
for t in suite:
for subtest in iterate_test_cases(t):
yield subtest
test_cases = unittest.TestLoader().discover('tests', pattern='test_*.py')
not_skipped_cases = []
only_cases = []
for ts in iterate_test_cases(test_cases):
case_name = ts.id()
case_class_name = ts.__class__.__name__
if not (case_class_name.endswith('__skip') or case_name.endswith('__skip')):
if case_class_name.endswith('__only') or case_name.endswith('__only'):
only_cases.append(ts)
else:
not_skipped_cases.append(ts)
final_test_cases = []
if len(only_cases) > 0:
final_test_cases = only_cases
else:
final_test_cases = not_skipped_cases
test_suite = unittest.TestSuite()
test_suite.addTests(final_test_cases)
result = unittest.TextTestRunner(verbosity=2).run(test_suite)
if not result.wasSuccessful():
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment