<div>ipdb or pytest-ipdb may have a helpful example?</div><div><br></div><a href="https://github.com/gotcha/ipdb/blob/master/ipdb/__main__.py">https://github.com/gotcha/ipdb/blob/master/ipdb/__main__.py</a><div><br></div><div><a href="https://github.com/mverteuil/pytest-ipdb/blob/master/pytestipdb/ptipdb.py">https://github.com/mverteuil/pytest-ipdb/blob/master/pytestipdb/ptipdb.py</a></div><div><br></div><div>These all require nose:</div><div><br></div><div><a href="https://github.com/ipython/ipython/blob/master/IPython/core/tests/test_magic_terminal.py">https://github.com/ipython/ipython/blob/master/IPython/core/tests/test_magic_terminal.py</a></div><div><br></div><div><a href="https://github.com/ipython/ipython/blob/master/IPython/core/tests/test_magic_arguments.py">https://github.com/ipython/ipython/blob/master/IPython/core/tests/test_magic_arguments.py</a></div><div><br></div><div><a href="https://github.com/ipython/ipython/blob/master/IPython/core/tests/test_interactiveshell.py">https://github.com/ipython/ipython/blob/master/IPython/core/tests/test_interactiveshell.py</a><br><br>On Tuesday, April 10, 2018, Jacob Vanderplas <<a href="mailto:jakevdp@cs.washington.edu">jakevdp@cs.washington.edu</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255)"><span style="font-size:12.8px">I'm trying to create automated tests for some magic functions within a pytest framework</span><span style="font-size:12.8px">. I've figured out how to do part of what I need using an embedded interactive shell; briefly, I do something like this:</span></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255)"><br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255)"><div><font face="monospace, monospace">from<span> </span><span>IPython</span>.terminal.embed import InteractiveShellEmbed</font></div><div><font face="monospace, monospace">ipshell = InteractiveShellEmbed()</font></div><div><font face="monospace, monospace">out = ipshell.run_cell("%my_magic_co<wbr>mmand")</font></div><div><font face="monospace, monospace">assert out.result == expected_result</font></div></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255)"><font face="monospace, monospace"><br></font></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255)"><font face="arial, helvetica, sans-serif">The problem I encounter is that my magic function optionally uses </font><font face="monospace, monospace"><span>IPython</span>.get_ipython().use<wbr>r_ns</font><font face="arial, helvetica, sans-serif"><span> </span>to access variables in the user namespace, and<span> </span></font><font face="monospace, monospace"><span>IPython</span>.get_ipython()</font><font face="arial, helvetica, sans-serif"><span> </span>retu<wbr>rns<span> </span></font><font face="monospace, monospace">None </font><font face="arial, helvetica, sans-serif">when run within an embedded shell in this way.</font></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255)"><font face="arial, helvetica, sans-serif"><br></font></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255)"><font face="arial, helvetica, sans-serif">Is there a way to create a simple embedded shell like this one that will behave as if it is an actual<span> </span><span>IPython</span><span> </span>shell, with<span> </span></font><font face="monospace, monospace"><span>IPython</span>.get_ipython()</font><font face="arial, helvetica, sans-serif"><span> </span>def<wbr>ined?</font></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255)"><font face="arial, helvetica, sans-serif"><br></font></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255)"><font face="arial, helvetica, sans-serif">Thanks,</font></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255)"><font face="arial, helvetica, sans-serif"><br></font></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255)"><font face="arial, helvetica, sans-serif">  Jake</font></div></div>
</div>
</blockquote></div>