From 958985d2c82909d1446463a7fc98962f2257f670 2011-06-22 22:50:32 From: Thomas Kluyver Date: 2011-06-22 22:50:32 Subject: [PATCH] Add testing function check_pairs to check input/output pairs against a function, and produce useful failure messages. --- diff --git a/IPython/testing/tools.py b/IPython/testing/tools.py index eea9634..0de7f4f 100644 --- a/IPython/testing/tools.py +++ b/IPython/testing/tools.py @@ -281,3 +281,29 @@ class TempFileMixin(object): # delete it. I have no clue why pass +pair_fail_msg = ("Testing function {0}\n\n" + "In:\n" + " {1!r}\n" + "Expected:\n" + " {2!r}\n" + "Got:\n" + " {3!r}\n") +def check_pairs(func, pairs): + """Utility function for the common case of checking a function with a + sequence of input/output pairs. + + Parameters + ---------- + func : callable + The function to be tested. Should accept a single argument. + pairs : iterable + A list of (input, expected_output) tuples. + + Returns + ------- + None. Raises an AssertionError if any output does not match the expected + value. + """ + for inp, expected in pairs: + out = func(inp) + assert out == expected, pair_fail_msg.format(func.func_name, inp, expected, out)