[PYTHON] Use Jupyter Notebook as a unit test and manual creation tool

Jupyter Notebook It's convenient, do you guys use it? It's convenient for implementing and checking the code, but if you devise it, you can even do unit tests and manual production. In this article, I will introduce a convenient usage that is a little different from the conventional usage.

Used for unit tests

Jupyter is convenient because you can write code while checking the result. Wouldn't it be convenient if the confirmation work could be used for unit testing as it is?

You can do it with pytest-ipynb!

pytest-ipynb can be easily installed with pip.

$ pip install pytest-ipynb

This module is an extension of the test library py.test, so you also need to install py.test.

It's easy to use, just throw a Notebook named test * .ipynb into the directory where you're testing. After that, you can write the test contents in that cell as follows.

Test example


''' mul test '''
a = 100 * 2
assert a == 200

I tried to translate the function list of the manual.

--Discover the'test * .ipynb' file -Each cell in Notebook runs as a unit test (just use assert) --The comment (docstring) in the first row of each cell is treated as a test name or function name. --If you put'fixture *'or'setup *'in the docstring, it will be executed before each unit test. --If you write'SKIPCI'in the docstring, the Travis-CI test will be skipped (check if the CI environment variable is defined). --The IPython Notebook kernel will be restarted after testing --The working folder during testing is local to the .ipynb file

If you make a mistake in the translation, please point it out.

After that, just run py.test -v normally, and it will do the test including Notebook.

Used for Readme.md

It can also be used for Readme.md on Github, which is convenient when you want to describe the execution result. GitHub of the module "Image4Layer" that I created also generates Readme.md from Jupyter.

https://github.com/pashango2/Image4Layer

Choose File> Download as> Markdown (.md) from the Jupyter menu to generate a markdown zip file.

メニュー_042.png

Rename the generated .md and tweak it and you're done. However, if you generate an image etc., the image will be expanded in the root directory, so I think that it needs to be corrected.

Use for Sphinx

As I wrote in the article "I want everyone to use Sphinx conveniently" (http://qiita.com/pashango2/items/d1b379b699af85b529ce), Notebook can be added to Sphinx as well. I will. There is an extension of Sphinx called nbsphinx that allows you to capture Jupyter notebooks.

Installation is easy with pip.

pip install nbsphinx

When imported into Sphinx, the display will be as follows.

jupyter.png

For detailed settings of Sphinx, refer to the following article.

I made "sphinx-quickstart-plus" to make Sphinx documentation convenient I want everyone to use Sphinx conveniently

Used to measure processing time

It's a basic thing, but I think some people may not know it, so I will describe it.

The % timeit command also measures the processing time of a function with Jupyter. Moreover, the best time is measured with an appropriate number of trials in consideration of the fluctuation of the processing time, so you can get higher quality measurement results than you can measure by yourself.

In [1]: %timeit l = [x ** 2 for x in range(1000)]
1000 loops, best of 3: 45 ms per loop

Summary

I want to read other people's Jupyter Notebook utilization articles!

Recommended Posts

Use Jupyter Notebook as a unit test and manual creation tool
Post a Jupyter Notebook as a blog post
Use Jupyter Lab and Jupyter Notebook with EC2
Install and set Jupyter Notebook to create a study note creation environment [Mac]
Unit test Databricks Notebook
Put Jupyter and Docker Compose on your Chromebook and use it as a light development environment!
How to import NoteBook as a module in Jupyter (IPython)
I want to use a virtual environment with jupyter notebook!
How to import NoteBook as a module in Jupyter (IPython)
Use pip with Jupyter Notebook
Use Cython with Jupyter Notebook
Use Remotte as a user
How to use Jupyter Notebook
A simple way to launch Jupyter Notebook / Lab and set a password
Beginners add disks and use them as a file system (´ ・ ω ・ `)
Use DynamoDB as a lock manager
Use pymol as a python library
Make a sound with Jupyter notebook
Jupyter Notebook: 4 banal tips and tricks
Use blender as a python module
Use markdown with jupyter notebook (with shortcut)
Easy to use Jupyter notebook (Python3.5)
Specify the browser to use with Jupyter Notebook. Especially Mac. (And Vivaldi)