-
-
Save michael-erasmus/da62afbe68c3f6ad097c30893144d74a to your computer and use it in GitHub Desktop.
An IPython %runtests magic that does some very basic test function discovery, running, and reporting within IPython.
This file contains hidden or 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 | |
import pandas as pd | |
@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 | |
#setup tables for result | |
results = [] | |
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 = [] | |
fail = {} | |
error = {} | |
t1 = time.time() | |
for name, func in tests.iteritems(): | |
print '{} ... '.format(name), | |
try: | |
func() | |
except AssertionError as e: | |
results.append({ | |
'name' : name, | |
'status' : 'fail', | |
'message' : '{}: {}'.format(name, repr(e)) | |
}) | |
except Exception as e: | |
results.append({ | |
'name' : name, | |
'status' : 'error', | |
'message' : '{}: {}'.format(name, repr(e)) | |
}) | |
else: | |
results.append({ | |
'name' : name, | |
'status' : 'success', | |
'message' : '' | |
}) | |
t2 = time.time() | |
results = pd.DataFrame(results, columns=['name', 'status', 'message']) | |
def highlight_fail_success(status): | |
return ['background-color: green;color: white' if v == 'success' else 'background-color: red' for v in status] | |
results.style.apply(highlight_fail_success) | |
print '' | |
print 'Ran {} tests in {:.3g} seconds.'.format(len(tests), t2 - t1) | |
print 'ok = {}, fail = {}, error = {}'.format( | |
len(results[results.status == 'ok']), | |
len(results[results.status == 'fail']), | |
len(results[results.status == 'error']) | |
) | |
return results.style.apply(highlight_fail_success, subset=['status']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Forked from jiffyclub/runtests.py
Tweaked presentation to present results in a formatted table, using Pandas.