[IPython-dev] What is the best way to write unit test for Magics Classes?

Aquil H. Abdullah aquil.abdullah@gmail....
Mon Apr 1 17:33:24 CDT 2013

So I came up with the following solution:

# --- Global IPython Shell used in the rest of the test no need to keep
ipshell = app_magics.get_ip_shell()

I could then do things like the following in my unit tests:

class TestAppMagics(object):
    def setup(self):

    def teardown(self):

    def test_simulated_positions(self):
        Test our computation of the current positions
        # NOTE: This is just a stub to show how things might work
        # hardcoded values will be replaced
        ipshell.run_cell('result = %current_simulated_positions')
        csp = ipshell.user_ns['result']
        nose.tools.eq_(csp['x'], -330, 'position x: %s dose not match
expected result -330' % csp['x'])

I looked at what was being done in test_interactiveshell.py in

I know that this doesn't give me true isolation from IPython, but it seems
to work.
Any comments?

On Sun, Mar 31, 2013 at 10:39 PM, Aquil H. Abdullah <
aquil.abdullah@gmail.com> wrote:

> Hello All,
> I am working on an application that uses IPython for a user interface.
> Basically, I need to interact with the results of a long running process.
>  I've decided to use the InteracteShellEmbed class along with a Magics
> subclass to make things a little bit easier for the users.
> I like to write unit tests as I develop my code, but it isn't quite clear
> to me how to unit tests classes that inherit from Magics and are decorated
> by line_magic or needs_local_scope.
> For example, let's say I have the following code:
> @magics_class
> class AppMagics(Magics):
>     """
>     Application magics to do stuff
>     """
>     @needs_local_scope
>     @line_magic
>     def peek_a_boo(self, line, local_ns=None):
>         """
>         In [Peek 2]: x = 10
>         In [Peek 3]: %peek_a_boo x
>         Out[Peek 3]: 420
>         """
>         var = local_ns.get(line)
>         cool_stuff = 42 * int(var)
>         return col_stuff
> In this instance I can use a a doc-test, however after reading the Dev:
> Testing page on github ,
> https://github.com/ipython/ipython/wiki/Dev%3A-Testing.  It looks like
> I'll have to make some modifications which I don't quite understand.
> Can anyone provide me with some resources for a newb to figure out how to
> right unit tests for Magics?
> --
> Aquil H. Abdullah
> aquil.abdullah@gmail.com

Aquil H. Abdullah
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/ipython-dev/attachments/20130401/7bfb9400/attachment.html 

More information about the IPython-dev mailing list