Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@
"uses": "actions/checkout@v4"
},
{
"name": "Set up Python",
"uses": "actions/setup-python@v4",
"name": "Install uv",
"uses": "astral-sh/setup-uv@v5",
"with": {
"python-version": "3.12"
"version": "0.6.0"
}
},
{
"name": "Install dependencies",
"run": "python -m pip install --upgrade pip\npip install -r requirements-dev.txt\npip install -e ."
"run": "uv sync --dev"
},
{
"name": "Tests",
"run": "pytest"
"run": "uv run --group dev pytest"
},
{
"name": "Integration tests",
"run": "pytest --nbval-lax --nbval-current-env Example.ipynb tests/Test*.ipynb"
"run": "uv run --group dev --nbval-lax --nbval-current-env Example.ipynb tests/Test*.ipynb"
},
{
"name": "Build package",
"run": "python -m build"
"run": "uv build"
},
{
"name": "Publish package distributions to PyPI",
Expand Down
24 changes: 14 additions & 10 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,37 @@
"uses": "actions/checkout@v4"
},
{
"name": "Set up Python",
"uses": "actions/setup-python@v4",
"name": "Install uv",
"uses": "astral-sh/setup-uv@v5",
"with": {
"python-version": "3.12"
"version": "0.6.0"
}
},
{
"name": "Install dependencies",
"run": "python -m pip install --upgrade pip\npip install -r requirements-dev.txt\npip install -e ."
"run": "uv sync --group dev"
},
{
"name": "Check format",
"run": "python -m ruff format --check ."
"run": "uv run --group dev ruff format --check ."
},
{
"name": "Check lints",
"run": "python -m ruff check ."
"run": "uv run --group dev ruff check ."
},
{
"name": "Tests",
"run": "python -m pytest"
"run": "uv run --group dev pytest"
},
{
"name": "Integration tests",
"run": "python -m pytest --nbval-lax --nbval-current-env Example.ipynb tests/Test*.ipynb"
}
"run": "uv run --group dev pytest --nbval-lax --nbval-current-env Example.ipynb tests/Test*.ipynb"
},
{
"name": "Build package",
"run": "uv build"
},
]
}
}
}
}
6 changes: 6 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Note: development is tracked on the [`develop` branch](https://github.com/chmp/ipytest/tree/develop).

## `develop`

- Add a description how to use `IPytest` in CI context. Thanks
[MusicalNinjaDad](https://github.com/MusicalNinjaDad) for the contribution
- Use `uv` for Python setup, use `hatchling` for the package build

## `0.14.2`

- Support collecting branch coverage in notebooks (e.g., via `--cov--branch`)
Expand Down
57 changes: 36 additions & 21 deletions Example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,16 @@
"\n",
"# define the tests\n",
"\n",
"\n",
"def test_my_func():\n",
" assert my_func(0) == 0\n",
" assert my_func(1) == 0\n",
" assert my_func(2) == 2\n",
" assert my_func(3) == 2\n",
" \n",
" \n",
"\n",
"\n",
"def my_func(x):\n",
" return x // 2 * 2 "
" return x // 2 * 2"
]
},
{
Expand Down Expand Up @@ -163,21 +164,25 @@
"\n",
"import pytest\n",
"\n",
"@pytest.mark.parametrize('input,expected', [\n",
" (0, 0),\n",
" (1, 0),\n",
" (2, 2),\n",
" (3, 2),\n",
"])\n",
"\n",
"@pytest.mark.parametrize(\n",
" \"input,expected\",\n",
" [\n",
" (0, 0),\n",
" (1, 0),\n",
" (2, 2),\n",
" (3, 2),\n",
" ],\n",
")\n",
"def test_parametrized(input, expected):\n",
" assert my_func(input) == expected\n",
" \n",
" \n",
"\n",
"\n",
"@pytest.fixture\n",
"def my_fixture():\n",
" return 42\n",
" \n",
" \n",
"\n",
"\n",
"def test_fixture(my_fixture):\n",
" assert my_fixture == 42"
]
Expand Down Expand Up @@ -216,17 +221,21 @@
"source": [
"%%ipytest -k feature1\n",
"\n",
"\n",
"def test_feature1_test1():\n",
" assert True\n",
"\n",
"\n",
"def test_feature1_test2():\n",
" assert True\n",
" \n",
"\n",
"\n",
"def test_feature2_test1():\n",
" assert False\n",
" \n",
" pytest.fail(\"expected failure\")\n",
"\n",
"\n",
"def test_feature2_test2():\n",
" assert False"
" pytest.fail(\"expected failure\")"
]
},
{
Expand Down Expand Up @@ -254,12 +263,14 @@
],
"source": [
"%%ipytest {test_feature1_test1}\n",
" \n",
"\n",
"\n",
"def test_feature1_test1():\n",
" assert True\n",
"\n",
"\n",
"def test_feature1_test2():\n",
" assert False"
" assert pytest.fail(\"expected failure\")"
]
},
{
Expand Down Expand Up @@ -288,11 +299,13 @@
"source": [
"%%ipytest --deselect {test_feature1_test2}\n",
"\n",
"\n",
"def test_feature1_test1():\n",
" assert True\n",
"\n",
"\n",
"def test_feature1_test2():\n",
" assert False"
" pytest.fail(\"expected failure\")"
]
},
{
Expand Down Expand Up @@ -321,9 +334,10 @@
"source": [
"%%ipytest -x --pdb\n",
"\n",
"\n",
"def test_example():\n",
" for i in range(10):\n",
" if i == 5: \n",
" if i == 5:\n",
" raise ValueError(i)"
]
},
Expand Down Expand Up @@ -353,6 +367,7 @@
"%%ipytest\n",
"# ipytest: raise_on_error=True\n",
"\n",
"\n",
"def test():\n",
" raise ValueError()"
]
Expand Down
36 changes: 36 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,42 @@ This command will first delete any previously defined tests, execute the cell
and then run pytest. For further details on how to use `ipytest` see the
[**example notebook**](./Example.ipynb) or the [reference](#reference) below.

### Enabling by default for all notebooks

[IPython startup scripts][ci-ipy-startup-scripts] allow to customize the interpreter,
for example to import and configure ipytest by default.Their location can be configured
via the `IPYTHONDIR` environment variable.

[ci-ipy-startup-scripts]: https://ipython.readthedocs.io/en/stable/interactive/tutorial.html#startup-files

### Running in CI

To run notebook tests in a CI workflow, you may want to

1. Set `raise_on_error=True` to ensure any `pytest` errors raise exceptions visible to the CI
system. For example via

```python
import ipytest
ipytest.autoconfig(raise_on_error=True)
```

2. Execute the notebooks with [`nbval`](https://github.com/computationalmodelling/nbval). The
`--nbval-lax` flag allows to only check for errors, not the exact notebook output which is likely
to change between `pytest` runs

To only set `raise_on_error=True` in CI systems you can check for common environment variables. See
[cibuildwheel.ci.detect_ci_provider][ci-detect_ci_provider] for a listing. For example

```python
import ipytest
import os

ipytest.autoconfig(raise_on_error="GITHUB_ACTIONS" in os.environ)
```

[ci-detect_ci_provider]: https://github.com/pypa/cibuildwheel/blob/c93d51ec540da7537ae66107a32c60dccd705102/cibuildwheel/ci.py#L21

## Global state

There are multiple sources of global state when using pytest inside the notebook:
Expand Down
6 changes: 3 additions & 3 deletions ipytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@


__all__ = [
"Error",
"autoconfig",
"run",
"config",
"clean",
"config",
"force_reload",
"reload",
"Error",
"run",
]
1 change: 1 addition & 0 deletions ipytest/_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Add syntatic sugar for configuration"""

import inspect
import warnings

Expand Down
7 changes: 4 additions & 3 deletions ipytest/cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def test():
[coverage-py-config-docs]: https://coverage.readthedocs.io/en/latest/config.html
[ipytest-cov-pytest-cov]: https://pytest-cov.readthedocs.io/en/latest/config.html
"""

import linecache
import os
import os.path
Expand Down Expand Up @@ -206,9 +207,9 @@ def on_post_run_cell(self, result):
)
if execution_count in self._execution_count_counts:
self._execution_count_counts[execution_count] += 1
self._info[
filename
] = f"In[{execution_count}/{self._execution_count_counts[execution_count]}]"
self._info[filename] = (
f"In[{execution_count}/{self._execution_count_counts[execution_count]}]"
)

else:
self._execution_count_counts[execution_count] = 0
Expand Down
3 changes: 2 additions & 1 deletion minidoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

`minidoc` will replace the content between the comments with the documentation
of the module. It preserves the comments itself. Therefore, it is safe to run
`minidoc` repeatedly on the same document.
`minidoc` repeatedly on the same document.

Per default minidoc will render a header for the module. To disable this
behavior add `"header": false` to the initial comment, as in:
Expand All @@ -24,6 +24,7 @@
<!-- minidoc "module": "my_module", "header": false -->
```
"""

#
# Copyright: Christopher Prohm, 2022
# Copied from https://github.com/chmp/libchmp/blob/main/src/chmp/minidoc.py
Expand Down
Loading