Unit tests in IPython notebooks.
Project description
ipytest - Unit tests in IPython notebooks
Usage | Global state | How does it work? | Changes | Reference | Development | Related packages | License
Sometimes quick experiments in IPython grow large and you find yourself wanting
unit tests. This module aims to make testing code in IPython notebooks easy. At
its core, it offers a way to run pytest
tests inside the
notebook environment. It is also designed to make the transfer of the tests into
proper python modules easy.
Installation: pip install ipytest
Features:
- support for pytest inside notebooks (with all bells and whistles)
- tight integration with IPython via magics and automatic code transforms
Usage
For usage see the example notebook or the documentation for
the core API below. The suggested way to import ipytest
is:
import ipytest
ipytest.autoconfig()
Afterwards in a new cell, tests can be executed as in:
%%run_pytest[clean] -qq
def test_example():
assert [1, 2, 3] == [1, 2, 3]
This command will first delete any previously defined tests, execute the cell and then run pytest. See the reference for a detailed list of available functionality.
Global state
There are multiple sources of global state when using pytest inside the notebook:
- pytest will find any test function ever defined. This behavior can lead to
unexpected results when test functions are renamed, as their previous
definition is still available inside the kernel.
ipytest
ships theclean_test
function to delete such instances. Also the%%run_pytest[clean]
magic clears any previously defined tests. - Python's module system caches imports and therefore acts as a global state.
To test the most recent version of any module, the module needs to be
reloaded.
ipytest
offers thereload
function. Theautoreload
extension of IPython may also help here. To test local packages, it is advisable to install them as development packages, e.g.,pip install -e .
. - For async code, IPython will create an event loop in the current thread.
This setup may interfere with async tests. To support these use cases,
ipytest supports running tests in a separate thread. Simply setup ipytest
via
ipytest.autoconfig(run_in_thread=True)
.
How does it work?
In its default configuration (via autoconfig()
), ipytest
performs the
following steps:
- Register pytest's assertion rewriter with the IPython kernel. The rewriter will rewrite any assert statements entered into the notebook to give better error messages. This change will affect also non test based code, but should generally improve the development experience.
- Ensure the notebook can be mapped to a file.
ipytest
will create a temporary file in the current directory and remove if afterwards. - Register the notebook scope temporarily as a module. This step is necessary to allow pytest's doctest plugin to import the notebook. If an existing module would be overwritten this step is skipped with a warning.
- Call pytest with a plugin that makes pytest believe the notebook is a proper module
ipytest
can pass additional arguments to pytest. Per default, only the
filename that is associated with the notebook is passed. There are a number of
ways to configure this behavior:
ipytest.config(addopts=...)
oripytest.autconfig(addopts=...)
allow to specify a list of strings that are added to the command line. For example,ipytest.autoconfig(addopts=["-x", "--pdb"])
will attach the debugger on the first test failure and not run further tests.ipytest.run(...)
: allows to specify additional arguments as strings%%run_pytest
and%%run_pytest[clean]
allow to specify additional arguments in the same lineipytest.config(defopts=False)
oripytest.autoconfig(defopts=False)
will instructipytest
to not pass the current module filename. It can still be passed manually by adding{MODULE}
to the command line.
The arguments are formatted using Python's standard string formatting.
Currently, only the {MODULE}
variable is understood. It is replaced with the
filename associated with the notebook.
Changes
Note: development is tracked on the develop
branch.
development
:- Remove the
ModuleCollectorPlugin
in favor of relying on pytest's builtin collection mechanism - Allow to fully customize the command line and to skip passing the current module as an argument
- Simplify config implementation: restrict config changes to function calls
- Allow to use the generated module name in the arguments passed to pytest
by using
{MODULE}
- Require
python>=3.6
- Remove
%%rewrite_asserts
magic - Remove
tempfile_fallback
config option - Remove
register_module
config option - Remove
raise_on_error
config option - Remove
filename
argument foripytest.run
- Remove
return_exit_code
argument fromipytest.run
- Remove
running_as_test
function
- Remove the
0.9.1
:- Add
ipython
as an explicit dependency
- Add
0.9.0
:- Add
Pytest>=5.4
to the requirements - Remove legacy functionality, mostly plain unittest integration
- The
tempfile_fallback
also kicks in, if a filename was configured, but the file does not exist - Add
register_module
option to register the notebook with the module system of Python. This way--doctest-modules
works as expected
- Add
0.8.1
: release with sdist for conda-forge0.8.0
:- Add the
autoconfig
helper to simplfy setup with reasonable defaults - Stop using deprecated pytest API
- Add the
0.7.1
:- fix assertion rewriting for
pytest>=5.0.0
- fix assertion rewriting for
0.7.0
:- add option to run tests in separate threads. This change allows to test
async code with the
pytest_asyncio
plugin - add a proper signature to
ipytest.config(...)
and show the current settings as a repr.
- add option to run tests in separate threads. This change allows to test
async code with the
0.6.0
: officially remove python 2 support. Whileipytest
was marked to work on python 2 and python 3, this statement was not tested and most likely not true. This change only documents the current state.0.5.0
:- Fix assertion rewriting via magics in
ipython>=7
- Add support to raise a
RuntimeError
on test errors (setipytest.config.raise_on_error = True
) - Add support to set base arguments (set
ipytest.config.addopts = []
) - Add config setting to enable magics (set
ipytest.config.magics = True
). - Add config setting to create a temporary file to work without the
notebook filename (set
ipytest.config.tempfile_fallback = True
). - Allow to set multiple config values at the same time by calling the
config object (
ipytest.config(...)
). - Add
ipytest.running_as_test()
to detect whether a notebook is executed as a test.
- Fix assertion rewriting via magics in
0.4.0
: add support for automatic AST transforms, deprecate non pytest API.0.3.0
: change default pattern forclean_tests
to match pytest discovery0.2.2
: add support for assert rewriting with current pytest versions0.2.1
: add ipython magics to simplify test execution0.2.0
: support for using pytest inside notebooks0.1.0
: support for runningunittest.FunctionTestCase
,unittest.TestCases
, anddoctests
.
Reference
ipytest.autoconfig
ipytest.autoconfig(rewrite_asserts=<default>, magics=<default>, clean=<default>, addopts=<default>, run_in_thread=<default>, defopts=<default>)
Configure ipytest
with reasonable defaults.
Specifically, it sets:
rewrite_asserts
:True
magics
:True
clean
:'[Tt]est*'
addopts
:('-q',)
run_in_thread
:False
defopts
:True
See ipytest.config for details.
%%run_pytest ...
IPython magic that first executes the cell, then executes ipytest.run()
. Any
arguments passed on the magic line be passed on to pytest. To register the
magics, run ipytest.autoconfig()
or ipytest.config(magics=True)
first.
Additional arguments can be passed to pytest. See the section "How does it work" for specifics.
For example:
%%run_pytest -qq
def test_example():
...
%%run_pytest[clean] ...
Same as the %%run_pytest
, but cleans any previously found tests, i.e., only
tests defined in the current cell are executed. To register the magics, run
ipytest.autoconfig()
or ipytest.config(magics=True)
first.
Additional arguments can be passed to pytest. See the section "How does it work" arguments for specifics.
ipytest.config
ipytest.config(rewrite_asserts=<keep>, magics=<keep>, clean=<keep>, addopts=<keep>, run_in_thread=<keep>, defopts=<keep>)
Configure ipytest
To update the configuration, call this function as in:
ipytest.config(rewrite_asserts=True)
The following settings are supported:
rewrite_asserts
(default:False
): enable ipython AST transforms globally to rewrite assertsmagics
(default:False
): if set toTrue
register the ipytest magicsclean
(default:[Tt]est*
): the pattern used to clean variablesaddopts
(default:()
): pytest command line arguments to prepend to every pytest invocation. For example settingipytest.config(addopts=['-qq'])
will execute pytest with the least verbosityrun_in_thread
(default:False
): ifTrue
, pytest will be run a separate thread. This way of running is required when testing async code withpytest_asyncio
since it starts a separate event loopdefopts
(default:True
): ifTrue
, ipytest will add the current module to the arguments passed to pytest. IfFalse
only the arguments given andadopts
are passed. Such a setup may be helpful to customize the test selection
ipytest.exit_code
The return code of the last pytest invocation.
ipytest.run
ipytest.run(*args, module=None, plugins=())
Execute all tests in the passed module (defaults to main) with pytest.
Parameters
- args (any): additional commandline options passed to pytest
- module (any): the module containing the tests. If not given, main will be used.
- filename (any): the filename of the file containing the tests. It has to be a real file, e.g., a notebook name, since itts existence will be checked by pytest. If not given, the file attribute of the passed module will be used.
- plugins (any): additional plugins passed to pytest.
ipytest.clean_tests
ipytest.clean_tests(pattern=None, items=None)
Delete tests with names matching the given pattern.
In IPython the results of all evaluations are kept in global variables unless explicitly deleted. This behavior implies that when tests are renamed the previous definitions will still be found if not deleted. This method aims to simply this process.
An effecitve pattern is to start with the cell containing tests with a call to clean_tests, then defined all test cases, and finally call run_tests. This way renaming tests works as expected.
Arguments:
- pattern: a glob pattern used to match the tests to delete.
-
- items: the globals object containing the tests. If None is given, the: globals object is determined from the call stack.
ipytest.reload
ipytest.reload(*mods)
Reload all modules passed as strings.
This function may be useful, when mixing code in external modules and notebooks.
Usage:
reload("ipytest._util", "ipytest")
Development
Setup the virtual environment via:
pip install -r requirements-dev.txt
To execute the unit tests of ipytest
run
python make.py test
python make.py integration
Before commit execute python make.py precommit
to update the documentation,
format the code, and run tests.
To create a new release execute:
python make.py release
Related packages
ipytest
is designed to enable running tests within an interactive notebook
session. There are also other packages that aim to use notebooks as tests
themselves, for example by comparing the output of running all cells to the
output of previous runs. These packages include:
- nbval is actively
maintained. It is also used in the integration tests of
ipytest
. - pytest-ipynb seems to be no longer maintained as the latest commit was on March 2016. .
- ...
Please create an issue, if I missed a packaged or mischaracterized any package.
License
The MIT License (MIT)
Copyright (c) 2015 - 2021 Christopher Prohm
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ipytest-0.10.0b2.tar.gz
.
File metadata
- Download URL: ipytest-0.10.0b2.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca46fbf297aef94fe825bc07067744629615a6834008e917c144f00d8f64c069 |
|
MD5 | 01ca3728bdfbe2ece74b4072af4f220b |
|
BLAKE2b-256 | 63a94f2e04ec8a8318ca3351aa89e556f5b9d4ebb83c326f9c3b78e795c76321 |
Provenance
File details
Details for the file ipytest-0.10.0b2-py3-none-any.whl
.
File metadata
- Download URL: ipytest-0.10.0b2-py3-none-any.whl
- Upload date:
- Size: 14.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13e74ddcf7fcc61cfc4b25a1cdb82d6dbedf2eb3fb049c672e55a9ed3f0dae9a |
|
MD5 | 4a34dbec94fdc29df97d1743c142d5f7 |
|
BLAKE2b-256 | 20276c3186d440a0e95c4a7acf29bd1beb5d74f9a1e649ea60d980ed27ea0418 |