Skip to main content

jupyter-pytest-2

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. This allows you to share objects, dataframes, and references between your notebook, pytest fixtures and tests.

There may be memory leaks, but i did not bother testing it.

You can not run pytest itself and give it a notebook, like

pytest example.ipynb

This will not work.

But why??!!1

I wrote this plugin to write data quality tests in databricks platform.

Installation

pip install jupyter-pytest-2

Example notebook

https://github.com/sashgorokhov/jupyter-pytest-2/blob/master/example.ipynb

Usage

Install the plugin, copypaste this into the notebook cell and run:

import pytest


def test_foo():
    assert True

pytest.main()

You can separate this by different cells, write fixtures, parametrized or not, use different fixture scopes - you can use everything you love pytest for.

import pytest

some_out_of_test_object = 'Hello, world!'

# Cell

@pytest.fixture(params=[1,2,3])
def foo(request):
  return request.param
  
# Cell

def test_foo(foo):
  assert some_out_of_test_object == 'Hello, world!'
  assert isinstance(foo, int)
  
# Cell

pytest.main(args=['-sv'])

Usage notes

  1. Plugin will discover only tests, fixtures and data in cells which were run in the current kernel scope. If you restart kernel, or load a notebook, you need to re-run cells with test related stuff.

  2. If you write some tests, execute a cell, and then remove or rename that test, this test will remain in global scope and will be found & executed by plugin. This can lead to an unexpected things, even harming ones. When renaming or removing test or fixture, also restart the kernel.

Enjoy!

Metadata

Release files for jupyter-pytest-2 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jupyter-pytest-2 1.0.1
File Size Uploaded
jupyter-pytest-2-1.0.1.tar.gz 3.2 kB Details

Release files / jupyter-pytest-2-1.0.1.tar.gz

Download URL jupyter-pytest-2-1.0.1.tar.gz
Size 3.2 kB
Tags Source
SHA-256 checksum
How to use checksums
e60e1ecb257d6e60392525c654ff4f03d49c7cba7e6c35894ab67dc611468fda
BLAKE2b-256 checksum
How to use checksums
87db999be2c1959b25787f7643c9322e7ef3166731f8340aaedc71685bb82a65
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.2

Release history Release notifications | RSS feed

This release

1.0.1 This release

1 release file

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page