testcell prevents your testing cells from affecting the global namespace
Project description
testcell
One good thing about working in Jupyter notebooks is that they make it easy to quickly test a bit of code by evaluating it in notebook cell. But one bad thing is that the definitions resulting from that evaluation hang around afterwards, when all you wanted was just to test that one bit of code.
%%testcell is a simple simple solution to that problem. It lets you
execute notebook cells in isolation. Test code, try snippets, and
experiment freely: no variables, functions, classes, or imports are left
behind. This helps to keep your namespace clean, so that leftover
symbols do not confuse later work.
WARNING: this doesn’t protect you from the side effects of your code like deleting a file or mutating the state of a global variable.
Install
Lightweight and reliable: %%testcell depends only on IPython and works
in all major notebook environments including Jupyter, Colab, Kaggle,
Modal, Solveit, and the IPython console.
pip install testcell
Quick Start
First import testcell:
import testcell
Then use it:
%%testcell
temp_var = "This won't pollute namespace"
temp_var
"This won't pollute namespace"
# temp_var doesn't exist — it was only defined inside the test cell
temp_var # NameError: name 'temp_var' is not defined
How it works
Import testcell and use the %%testcell magic at the top of any cell.
Under the hood, your code is wrapped in a temporary function that
executes and then deletes itself.
Use verbose to see the generated wrapper code:
%%testcell verbose
result = "isolated execution"
result
### BEGIN
def _test_cell_():
#| echo: false
result = "isolated execution"
return result # %%testcell
try:
_ = _test_cell_()
finally:
del _test_cell_
_ # This will be added to global scope
### END
'isolated execution'
Suppressing output
Like normal Jupyter cells, add a semicolon ; to the last statement to
suppress display:
%%testcell
calculation = 42 * 2
calculation;
No output is displayed, and calculation still doesn’t leak to globals.
Skip execution
Skip cells without deleting code using the skip command.
IMPORTANT: This is especially useful in notebook environments like Colab, Kaggle, and Modal where you can’t use Jupyter’s “Raw cell” type to disable execution.
%%testcell skip
raise ValueError('This will not execute')
<testcell.MessageBox object>
To skip all %%testcell cells at once (useful for production runs),
use: testcell.global_skip = True
Visual marking
Use banner to display a colored indicator at the top of cell output,
making test cells instantly recognizable:
%%testcell banner
"clearly marked"
<testcell.MessageBox object>
'clearly marked'
The banner adapts to your environment. In HTML-based notebooks like Jupyter, it displays as a full-width colored box. In console environments like IPython, it appears as text with an emoji.
Colors and emojis are fully customizable through testcell.params.
IMPORTANT: To enable banners for all %%testcell cells, use:
testcell.global_use_banner = True
Run in complete isolation
%%testcelln is a shortcut for %%testcell noglobals and executes
cells with zero access to your notebook’s global scope. Only
Python’s __builtins__ are available.
This is powerful for: - Detecting hidden dependencies: catch when your code accidentally relies on global variables - Testing portability: verify functions work standalone - Clean slate execution: run code exactly as it would in a fresh Python session
my_global = "I'm in the global scope"
%%testcell
'my_global' in globals()
True # my_global is available
%%testcelln
'my_global' in globals()
False # my_global is NOT available
%%testcelln
globals().keys()
dict_keys(['__builtins__'])
Explicit dependencies
The (inputs)->(outputs) syntax gives you precise control: you can pass
any symbol (variables, functions, classes) into the isolated context and
save only chosen ones back to globals.
This forces explicit dependency declaration, giving you full control over what enters and exits the cell. It prevents accidental reliance on symbols from the main context that would hurt you when exporting the code.
data = [1, 2, 3, 4, 5]
%%testcelln (data)->(calculate_stats)
# Only 'data' is available, only 'calculate_stats' is saved
def calculate_stats(values):
return {
'mean': sum(values) / len(values),
'min': min(values),
'max': max(values)
}
# Test it works
print(calculate_stats(data))
{'mean': 3.0, 'min': 1, 'max': 5}
calculate_stats now exists in globals. No test code or
intermediate variables leaked.
calculate_stats([10, 20, 30])
{'mean': 20.0, 'min': 10, 'max': 30}
Advanced parsing
Thanks to Python’s ast module, %%testcell correctly handles complex
code patterns including comments on the last line and multi-line
statements:
%%testcell verbose
result = "complex parsing"
(result,
True)
# comment on last line
### BEGIN
def _test_cell_():
#| echo: false
result = "complex parsing"
return (result,
True) # %%testcell
try:
_ = _test_cell_()
finally:
del _test_cell_
_ # This will be added to global scope
### END
('complex parsing', True)
Links:
- PROJECT PAGE: https://github.com/artste/testcell
- DOCUMENTATION: https://artste.github.io/testcell
- PYPI: https://pypi.org/project/testcell
- DETAILED DEMO: https://github.com/artste/testcell/blob/main/demo/testcell_demo.ipynb
- USE CASE ZOO: https://github.com/artste/testcell/blob/main/demo/testcell_zoo.ipynb
- LAUNCHING BLOG: Introducing
%%testcell - COLAB DEMO: testcell_demo.ipynb
- KAGGLE SAMPLE NOTEBOOK: https://www.kaggle.com/artste/introducing-testcell
Todo:
- Install as a plugin to enable it by default like other cell’s magic.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file testcell-0.0.8.tar.gz.
File metadata
- Download URL: testcell-0.0.8.tar.gz
- Upload date:
- Size: 17.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af0351a9d5759588484bd31ad4d5a5d48e4930653d0684f708b483f215fa0c82
|
|
| MD5 |
c7092ef7650b1b23ba3ed6230dd62bb3
|
|
| BLAKE2b-256 |
213d354194c468752082e6b73efc2655a3583c5bb22095388160256ff072332c
|
File details
Details for the file testcell-0.0.8-py3-none-any.whl.
File metadata
- Download URL: testcell-0.0.8-py3-none-any.whl
- Upload date:
- Size: 14.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b5685f37fff0ef17341147ad0a9b3a05407ad19bec6d6b400157da5a8b6701df
|
|
| MD5 |
199262b097ba37332955dd96f9e92fd3
|
|
| BLAKE2b-256 |
31c7a245f7bf0d7f2f3aecaa53943fa302515444136dc5a08e19af5dd419e953
|