Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running pytest test functions inside a jupyter notebook

I'm working on a presentation about python testing options, and one of the technologies I want to demo is pytest. I'm planning to do the presentation from an jupyter/ipython notebook. Ideally I'd like to be able to define a test function in a cell and then use pytest to run just that function; that way I can demonstrate individual features of pytest, etc.

Is there an existing way to do this? None of the interactive features I saw in the pytest api seem to fit the bill, but I'm certainly no pytest expert. I'd be happy to write a plugin to do it, so any advice on the best approach in that direction would be great.

I did see pytest-ipynb, but it does not seem to do what I need. If that assessment is wrong, advice on how to use that would be great, too.

like image 473
abingham Avatar asked Dec 23 '16 15:12

abingham


People also ask

Can I run pytest in Jupyter Notebook?

This small pytest plugin allows it to discover and run tests written inside ipython notebook cells. It works by examining notebook global scope, putting it into a module object and passing it to pytest for futher processing. No temporary files or bytecode hacks.

How do I run a specific pytest function?

Running pytest We can run a specific test file by giving its name as an argument. A specific function can be run by providing its name after the :: characters. Markers can be used to group tests. A marked grouped of tests is then run with pytest -m .

How do I run a function in Jupyter?

Inside the Notebook. When you open a new Jupyter notebook, you'll notice that it contains a cell. Cells are how notebooks are structured and are the areas where you write your code. To run a piece of code, click on the cell to select it, then press SHIFT+ENTER or press the play button in the toolbar above.


1 Answers

There is a similar module that looks very mature: https://github.com/chmp/ipytest

It is referenced in the module developed by @akaiola and is more active and regularly updated / maintained.

Here is an example from the Github repository (https://github.com/chmp/ipytest/blob/master/Example.ipynb)

Jupyter Notebook with iPytest

And another one I have created recently

example 2

like image 79
Jean-Francois T. Avatar answered Sep 25 '22 13:09

Jean-Francois T.