lewagon / nbresult

Testing Library for Jupyter Notebooks
https://pypi.org/project/nbresult/
MIT License
8 stars 2 forks source link
notebook-jupyter python testing

nbresult

A simple package to test Jupyter notebook result for the Le Wagon's Data Science Bootcamp.

1. Installation

Installation with pip from Pypi:

pip install nbresult

2. Usage

2.1 Basic usage

Considering the default data challenge architecture:

.
├── challenge.ipynb
├── Makefile
├── README.md
├── data
│   └── data.csv
└── tests
    └── __init__.py

If you want to test a variable log_model_score from the challenge.ipynb notebook with pytest:

variable

Anywhere in the notebook you can add a cell with the following code:

from nbresult import ChallengeResult

result = ChallengeResult('score',
    score=log_model_score
)
result.write()

This outputs a score.pickle file in the tests directory:

.
├── challenge.ipynb
├── Makefile
├── README.md
├── data
│   └── data.csv
└── tests
    ├── __init__.py
    └── score.pickle

Now you would like to write test on the log_model_score with pytest. Create a test_score.py file:

# tests/test_score.py
from nbresult import ChallengeResultTestCase

class TestScore(ChallengeResultTestCase):

    def test_score_is_above_82(self):
        self.assertEqual(self.result.score > 0.82, True)

Finally you can run your tests with pytest:

pytest tests/test_score.py

pytest

OR

Run the tests with make:

# Makefile

default: pytest

pytest:
  PYTHONDONTWRITEBYTECODE=1 pytest -v --color=yes

make

OR

Run the tests inside the notebook:

from nbresult import ChallengeResult

result = ChallengeResult('score',
    score=log_model_score
)
result.write()
print(result.check())

notebook

2.2 Advanced usage

For more advanced folder structure, you also can specify a subdir folder in which store & read pickle file

from nbresult import ChallengeResult

result = ChallengeResult('score',
    subdir='a', # This will store pickle in tests/a/score.pickle
    score=log_model_score
)
result.write()
result.check()

Check out detailed example below

subdir

Testing

Run make