Package: testcell
testcellTL;DR:%%testcellprevents your testing cells from affecting the
global namespace.The Python cell magic%%testcellexecutes a cell withoutpollutingthe notebook’s global namespace. This is useful whenever you want to
test your code without having any of the local variables escape that
cell.What’s happening under the hood is that your cell code, before being
executed, is wrapped in a temporary function that will be deleted after
execution. To give you the feeling ofseamless integrationthe last
statement is optionally returned like it happens in a normal cell.WARNING:this don’t protect you fromthe side effects of your codelike deleting a file or mutating the state of a global variable.InstallpipinstalltestcellHow to usejust import it withimport testcelland then use the%%testcellcell
magic.%%testcella="'a' is not polluting global scope"a"'a' is not polluting global scope"assert'a'notinlocals()What is happening under the hood is that%%testcellwraps your cell’s
code with a function, execute it and then deletes it. Adding theverbosekeywork will print which code will be executed.NOTE: The actual cell code is enclosed withinBEGINandENDcomment
blocks for improved readability.%%testcellverbosea="'a' is not polluting global scope"a### BEGIN
def _test_cell_():
    #| echo: false
    a = "'a' is not polluting global scope"
    return a # %%testcell
try:
    _ = _test_cell_()
finally:
    del _test_cell_
_ # This will be added to global scope
### END

"'a' is not polluting global scope"If you’re just interested in seeing what will be executed, but actually
not executing it, you ca usedryrunoption:%%testcelldryruna="'a' is not polluting global scope"a### BEGIN
def _test_cell_():
    #| echo: false
    a = "'a' is not polluting global scope"
    return a # %%testcell
try:
    _ = _test_cell_()
finally:
    del _test_cell_
if _ is not None: display(_)
### ENDIf you add a semicolon;at the end of your last statement noreturnstatement is added and nothing is displayed like a normal jupyter cell.%%testcellverbosea="'a' is not polluting global scope"a;### BEGIN
def _test_cell_():
    #| echo: false
    a = "'a' is not polluting global scope"
    a;
try:
    _ = _test_cell_()
finally:
    del _test_cell_
_ # This will be added to global scope
### ENDtestcellworks seamlessly with existingprintordisplaystatements
on last line:%%testcellverbosea="'a' is not polluting global scope"print(a)### BEGIN
def _test_cell_():
    #| echo: false
    a = "'a' is not polluting global scope"
    return print(a) # %%testcell
try:
    _ = _test_cell_()
finally:
    del _test_cell_
_ # This will be added to global scope
### END
'a' is not polluting global scopeMoreover, thanks toast, it properly deals with complex situations
like comments on the last line and multi lines statements%%testcellverbosea="'a' is not polluting global scope"(a,True)# this is a comment on last line### BEGIN
def _test_cell_():
    #| echo: false
    a = "'a' is not polluting global scope"
    return (a,
     True) # %%testcell
try:
    _ = _test_cell_()
finally:
    del _test_cell_
_ # This will be added to global scope
### END

("'a' is not polluting global scope", True)Run in isolation%%testcellnis a shortcut for%%testcell noglobalsand executes the
cell in complete isolation from the global scope. This is very useful
when you want to ensure that global variables or namespaces are not
accessible within the cell.aaa='global variable'%%testcell'aaa'inglobals()True%%testcellnoglobals'aaa'inglobals()False%%testcelln'aaa'inglobals()False%%testcellnglobals().keys()dict_keys(['__builtins__'])With%%testcellninside the cell, you’ll be able to access only to__builtins__(aka: standard python’s functions).It behaves like a
notebook-in-notebook.%%testcelldefmy_function(x):print(aaa)# global variablereturnxtry:my_function(123)exceptExceptionase:print(e)global variable%%testcellndefmy_function(x):print(aaa)# global variablereturnxtry:my_function(123)exceptExceptionase:print(e)name 'aaa' is not definedAs you can see from this last example,%%testcellnhelps you to
identify thatmy_functionrefers global variableaaa.IMPORTANT: this isjust wrapping your celland so it’s still
running on your main kernel. If you modify variables that has been
created outside of this cell (aka: if you have side effects) this will
not protect you.aaa'global variable'%%testcell# WARNING: this will alter the state of global variable:globals().update({'aaa':'modified global variable'});aaa'modified global variable'delaaaLinks:PROJECT PAGE:https://github.com/artste/testcellDOCUMENTATION:https://artste.github.io/testcellPYPI:https://pypi.org/project/testcellCOLAB DEMO:testcell_demo.ipynbKAGGLE SAMPLE NOTEBOOK:https://www.kaggle.com/artste/introducing-testcellTodo:Install as a plugin to enable it by default like other cell’s magic.