[IPython-dev] What is the best way to write unit test for Magics Classes?
Aquil H. Abdullah
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:
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
On Sun, Mar 31, 2013 at 10:39 PM, Aquil H. Abdullah <
> 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:
> class AppMagics(Magics):
> Application magics to do stuff
> 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 H. Abdullah
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the IPython-dev