Created
August 31, 2017 12:24
-
-
Save kadirmalak/b1b76443f1a478ab342f71b8c6dfc8d7 to your computer and use it in GitHub Desktop.
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
import unittest | |
import inspect | |
import re | |
def run_tests(clazz): | |
def is_test_method(name): | |
return re.search('^test_*', name) is not None | |
funcs = inspect.getmembers(clazz, predicate=inspect.isfunction) | |
tests = [f for f in funcs if is_test_method(f[0])] | |
problems = [] | |
for test in tests: | |
instance = clazz(methodName=test[0]) | |
results = instance.run() | |
if len(results.errors) == 0 and len(results.failures) == 0: | |
print("%s passed..." % test[0]) | |
else: | |
print("%s failed..." % test[0]) | |
problems.extend([f[1] for f in results.failures]) | |
problems.extend([e[1] for e in results.errors]) | |
if len(problems) > 0: | |
print() | |
for problem in problems: | |
print(problem) | |
raise Exception('%d test(s) failed...' % len(problems)) | |
class SomeTests(unittest.TestCase): | |
def __init__(self, *args, **kwargs): | |
super(SomeTests, self).__init__(*args, **kwargs) | |
def test1(self): | |
self.assertEqual('foo', 'foo') | |
def test2(self): | |
self.assertEqual('foo', 'bar') | |
run_tests(SomeTests) | |
#test1 passed... | |
#test2 failed... | |
#Traceback (most recent call last): | |
# File "<ipython-input-8-c2e141ec5648>", line 10, in test2 | |
# self.assertEqual('foo', 'bar') | |
#AssertionError: 'foo' != 'bar' | |
#- foo | |
#+ bar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment