Collection of pytest utils built on top of nbval
Project description
nbvalx
nbvalx is a collection of pytest
utils built on top of nbval
.
nbvalx is currently developed and maintained at Università Cattolica del Sacro Cuore by Dr. Francesco Ballarin.
nbvalx can be pip install
ed from its GitHub repository or from PyPI
Cell magics for conditional running based on tags
Add a cell with
%load_ext nbvalx
at the beginning of a notebook to load nbvalx IPython
extension. The extension is implemented in nbvalx/jupyter_magics.py
.
The extension allows to register a list of allowed tags, and their values
%%register_run_if_allowed_tags
bool_tag: True, False
int_tag: 2, 10
string_tag: "value1", "value2"
and set the current value of each tag with
%%register_run_if_current_tags
bool_tag = False
int_tag = 10
string_tag = "value1"
The tags can then be used to conditionally run cells. As an example, if the subsequent two cells were
%%run_if bool_tag and int_tag > 5
print("Cell with float and integer comparison")
%%run_if string_tag == "value1"
print("Cell with string comparison")
the string "Cell with float and integer comparison"
would never get printed, while the "Cell with string comparison"
would indeed be printed.
Without nbvalx, the four cells above could have equivalently been written as
bool_tag = False
int_tag = 10
string_tag = "value1"
if bool_tag and int_tag > 5:
print("Cell with float and integer comparison")
if string_tag == "value1":
print("Cell with string comparison")
The plain formulation is certainly less verbose and more compact than nbvalx equivalent one with four cells. However, it is less "testing-friendly", because the values of bool_tag
, int_tag
and string_tag
are hardcoded in the notebook and cannot be easily varied. With a pytest
terminology, nbvalx tags correspond to defining a parametrization of the notebook.
A similar statement can be used as an HTML comment in markdown cells
<!-- keep_if string_tag == "value1" -->
This is a markdown cell which explains a code cell that will be run only if string_tag == "value1".
Markdown cells will be stripped only when running through pytest
with the --tag-collapse
option, see below.
See tests/notebooks/data/tags
for a few simple notebooks using tags.
Custom pytest hooks for jupyter notebooks
The file nbvalx/pytest_hooks_notebooks.py
contains a few utility functions to be used in pytest configuration file for notebooks tests.
The pytest
hooks which can be customized in this way are:
pytest_addoption
,pytest_collect_file
, andpytest_sessionstart
.
For clarity, the hooks implemented in nbvalx/pytest_hooks_notebooks.py
do not have a pytest_
prefix, as it will be the user's responsability to pick them up and assign them to the corresponding pytest
hook in a custom conftest.py
, as show in tests/notebooks/conftest.py
.
The hooks change the default behavior of nbval
in the following ways:
- the options
--nbval
and--nbval-lax
, whichnbval
requires to pass explicitly, are here enabled implicitly; - support for
MPI
run by providing the--np
option topytest
. When runningpytest --np 2
, nbvalx will start aipyparallel.Cluster
and run notebooks tests in parallel on 2 cores. In the default case one core is employed, and anipyparallel.Cluster
is not started; - support for tags, as introduced in the previous section, as governed by two flags:
--tag-collapse
: if enabled (default), strip all cells for which the%%run_if ...
or<!-- keep_if ... -->
conditions do not evaluate toTrue
. This may be used to prepare notebook files to be read by the end user, as stripping unused cells may improve the readability of the notebook. If not enabled, all cells will be kept.--ipynb-action
: eithercollect-notebooks
(default) orcreate-notebook
. Both actions create several copies of the original notebook that differ by the currently enabled tag. For instance, if the original notebook in the section above is callednotebook.ipynb
and has two allowed tags, the action will generate a filenotebook[tag1].ipynb
wheretag1
is assigned as the current value of the tag, and a filenotebook[tag2].ipynb
wheretag2
is assigned as the current value of the tag. Iftag-collapse
is enabled, cells associated to all remaining tags are stripped. Thecreate-notebook
action only generates the tagged notebooks; instead, thecollect-notebooks
additionally also runs them throughpytest
;
- support for collecting cell outputs to log files, which are saved in a work directory provided by the user with the argument
--work-dir
. This is helpful to debug failures while testing notebooks. Log files are of two formats: a text log, with extension.log
when running without--np
or.log-{rank}
when running in parallel; a notebook log, with extension.log.ipynb
. If no work directory is specified, the default value isf".ipynb_pytest/np_{np}/collapse_{tag_collapse}"
. In case the notebook depends on additonal data files (e.g., local python modules), the flag--link-data-in-work-dir
can be passed with glob patterns of data files that need to be symbolically linked in the work directory. The option can be passed multiple times in case multiple patterns are desired, and they will be joined with an or condition; - the notebook is treated as if it were a demo or tutorial, rather than a collection of unit tests in different cells. For this reason, if a cell fails, the next cells will be skipped;
- a new
# PYTEST_XFAIL
marker is introduced to mark cells as expected to fail. The marker must be the first entry of the cell. A similar marker# PYTEST_XFAIL_AND_SKIP_NEXT
marks the cell as expected to fail and interrupts execution of the subsequent cells. Both previous markers have a variant withXFAIL_IN_PARALLEL
instead ofXFAIL
, that consider the cell to be expected to fail only when the value provided to--np
is greater than one; - support for running notebooks through
coverage
without having to install thepyvtest-cov
plugin. Use flag--coverage-source
to set the module name for which coverage testing is requested.
Custom pytest hooks for unit tests
The file nbvalx/pytest_unit_tests.py
contains a few utility functions to be used in pytest configuration file for notebooks tests.
The pytest
hooks which can be customized in this way are:
pytest_runtest_setup
, andpytest_runtest_teardown
.
For clarity, the hooks implemented in nbvalx/pytest_unit_tests.py
do not have a pytest_
prefix, as it will be the user's responsability to pick them up and assign them to the corresponding pytest
hook in a custom conftest.py
, as show in tests/unit/conftest.py
.
The hooks are typically employed to obtain a MPI
-parallel safe execution of python unit tests by calling garbage collection and putting a MPI
barrier after each test.
Custom pytest hooks for unit tests
The file nbvalx/tempfile.py
contains MPI
parallel-safe context managers to create temporary files and directories. Similarly to the tempfile
module in the standard library, the following context managers are provided:
nbvalx.tempfile.TemporaryDirectory
,nbvalx.tempfile.TemporaryFile
.
Project details
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 nbvalx-0.3.1.tar.gz
.
File metadata
- Download URL: nbvalx-0.3.1.tar.gz
- Upload date:
- Size: 20.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c73d133d88e96a24da299b116ae53a15907fcd4e4cf390dfa0e1c7ca1d87caaf |
|
MD5 | 0c32f9cddd4dcd58c3acde9359835b93 |
|
BLAKE2b-256 | 6f1509899b45523faf7eca931dd8867345b716f1961856bed0ecf9461c6fa461 |
File details
Details for the file nbvalx-0.3.1-py3-none-any.whl
.
File metadata
- Download URL: nbvalx-0.3.1-py3-none-any.whl
- Upload date:
- Size: 18.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c49d4060a45847c964cf2e1fb2c46118f74c6cb1eabd842371e46091cb59600d |
|
MD5 | 10fad16b3c4048b0a092ad5f8e53c893 |
|
BLAKE2b-256 | 86a8a83fb394056b21feb7f7e6d3d1c3281d97b75c78ae13974c6acb409a13fb |