From 54d3ddad4daf5918c6e2c948938569f287d6c16a 2006-02-09 19:21:41 From: vivainio Date: 2006-02-09 19:21:41 Subject: [PATCH] -Added a unit testing framework -ipapi.py: Exposed runlines and set_custom_exc --- diff --git a/IPython/ipapi.py b/IPython/ipapi.py index 56b3f00..fa28a9d 100644 --- a/IPython/ipapi.py +++ b/IPython/ipapi.py @@ -102,6 +102,8 @@ class IPApi: self.set_hook = ip.set_hook + self.set_custom_exc = ip.set_custom_exc + self.IP = ip global _recent _recent = self @@ -155,7 +157,18 @@ class IPApi: Return a PickleShareDB object. """ return self.IP.db + def runlines(self,lines): + """ Run the specified lines in interpreter, honoring ipython directives. + + This allows %magic and !shell escape notations. + Takes either all lines in one string or list of lines. + """ + if isinstance(lines,basestring): + self.IP.runlines(lines) + else: + self.IP.runlines('\n'.join(lines)) + def launch_new_instance(user_ns = None): """ Create and start a new ipython instance. diff --git a/doc/ChangeLog b/doc/ChangeLog index d17e87a..d9adf06 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,10 @@ +2006-02-09 Ville Vainio + + * test/*: Added a unit testing framework (finally). + '%run runtests.py' to run test_*. + + * ipapi.py: Exposed runlines and set_custom_exc + 2006-02-07 Ville Vainio * iplib.py: don't split "f 1 2" to "f(1,2)" in autocall, diff --git a/test/runtests.py b/test/runtests.py new file mode 100644 index 0000000..249f810 --- /dev/null +++ b/test/runtests.py @@ -0,0 +1,31 @@ +""" Run ipython unit tests + +This should be launched from inside ipython by "%run runtests.py" +or through ipython command line "ipython runtests.py". + +""" + +from path import path +import pprint,os +import IPython.ipapi +ip = IPython.ipapi.get() + +def main(): + all = path('.').files('test_*py') + results = {} + res_exc = [None] + def exchook(self,*e): + res_exc[0] = [e] + ip.IP.set_custom_exc((Exception,), exchook) + startdir = os.getcwd() + for test in all: + print test + res_exc[0] = 'ok' + os.chdir(startdir) + ip.runlines(test.text()) + results[str(test)] = res_exc[0] + + os.chdir(startdir) + pprint.pprint(results) + +main() \ No newline at end of file diff --git a/test/test_cd.ipy b/test/test_cd.ipy new file mode 100644 index 0000000..2214c1b --- /dev/null +++ b/test/test_cd.ipy @@ -0,0 +1,6 @@ +import os +cd / +assert os.getcwd() == '/' +%cd /tmp + +assert os.getcwd() == '/tmp' diff --git a/test/test_shouldfail.ipy b/test/test_shouldfail.ipy new file mode 100644 index 0000000..128f229 --- /dev/null +++ b/test/test_shouldfail.ipy @@ -0,0 +1,3 @@ +# this will fail w/ assertionerror +cd / +assert os.getcwd() == '/does/not/exist'