An IPython %runtests magic that does some very basic test function discovery, running, and reporting within IPython.
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
from IPython.core.magic import register_line_magic | |
@register_line_magic | |
def runtests(line): | |
""" | |
The %runtests magic searches your IPython namespace for functions | |
with names that begin with 'test'. It will attempt to run these | |
functions (calling them with no arguments), and report whether they | |
pass, fail (raise an AssertionError), or error (raise any other | |
kind of error). | |
For tests that fail or error %runtests will show the exception raised | |
but not the traceback, so write informative messages! | |
""" | |
import collections | |
import time | |
ip = get_ipython() | |
tests = {} | |
# collect tests | |
# search will only find functions that start with 'test' | |
for k, v in ip.user_ns.iteritems(): | |
if k.startswith('test') and isinstance(v, collections.Callable): | |
tests[k] = v | |
print 'Collected {} tests.\n'.format(len(tests)) | |
# run tests | |
ok = 0 | |
fail = {} | |
error = {} | |
t1 = time.time() | |
for name, func in tests.iteritems(): | |
print '{} ... '.format(name), | |
try: | |
func() | |
except AssertionError as e: | |
print 'fail' | |
fail[name] = e | |
except Exception as e: | |
print 'error' | |
error[name] = e | |
else: | |
print 'ok' | |
ok += 1 | |
t2 = time.time() | |
# print info on any failures | |
if fail: | |
print '' | |
print 'Failures' | |
print '========' | |
for name, e in fail.iteritems(): | |
print '{}: {}'.format(name, repr(e)) | |
# print info on any errors | |
if error: | |
print '' | |
print 'Errors' | |
print '======' | |
for name, e in error.iteritems(): | |
print '{}: {}'.format(name, repr(e)) | |
print '' | |
print 'Ran {} tests in {:.3g} seconds.'.format(len(tests), t2 - t1) | |
print 'ok = {}, fail = {}, error = {}'.format(ok, len(fail), len(error)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment