# Testbook
> This document was created in reference to a discussion held between Rohit Sanjay and Mindtree.
## Introduction
testbook is a unit testing framework for testing code in Jupyter Notebooks.
Previous attempts at unit testing notebooks involved writing the tests in the notebook itself. However, testbook will allow for unit tests to be run against notebooks in separate test files, hence treating .ipynb files as .py files.
Here is an example of a unit test written using testbook
Consider the following code cell in a Jupyter Notebook:
```python
def func(a, b):
return a + b
```
You would write a unit test using testbook in a Python file as follows:
```python
from testbook import testbook
def test_func(tb):
with testbook('/path/to/notebook.ipynb', execute=True) as tb:
func = tb.ref("func")
assert func(1, 2) == 3
```
## Context
Jupyter notebooks are on the road to be productionizable with open source tools such as [papermill](https://github.com/nteract/papermill) which can allow for notebooks to be executed as scripts with parameters. As notebooks enter production environments, it becomes important for it to be properly unit tested. Testbook can now help you write tests for complex code written in Jupyter notebooks.
Now there are a few concerns that could be raised:
1. **Why not write the code in a Python module in the first place?**
Absolutely! Testbook is aimed at those specific users who prefer to use Jupyter notebooks.
2. **Why can't I extract the code from the notebook into a Python module?**
The issue with extracting code from notebook into a Python module (.py file) is the heavy refactoring process that is involved.