Skip to content

Instantly share code, notes, and snippets.

@michael-erasmus
Forked from jiffyclub/runtests.py
Last active April 13, 2016 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michael-erasmus/da62afbe68c3f6ad097c30893144d74a to your computer and use it in GitHub Desktop.
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.
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'])
@michael-erasmus
Copy link
Author

Forked from jiffyclub/runtests.py

Tweaked presentation to present results in a formatted table, using Pandas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment