From 240445581ce3860585a5ec277655274bd6b8e6f7 Mon Sep 17 00:00:00 2001 From: Stijn de Gooijer Date: Tue, 29 Aug 2023 13:50:58 +0200 Subject: [PATCH] Use pytest to run Python code snippets (#389) --- .github/workflows/ci.yaml | 2 +- .gitignore | 1 + Makefile | 6 +++--- pyproject.toml | 5 +++++ requirements.txt | 1 + tests/test_python.py | 16 ++++++++++++++++ 6 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 pyproject.toml create mode 100644 tests/test_python.py diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 2457d4474..4ebb3930c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -31,7 +31,7 @@ jobs: - name: Set up Graphviz uses: ts-graphviz/setup-graphviz@v1 - - run: find docs/src/python -type f | xargs -n 1 bash -c 'python -W error $0 || exit 255' + - run: pytest black: runs-on: ubuntu-latest diff --git a/.gitignore b/.gitignore index cda0fc491..4c5aabf85 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ # Python /.venv __pycache__ +.pytest_cache/ # Project /docs/data/ diff --git a/Makefile b/Makefile index c4d8d2103..3b9ff234d 100644 --- a/Makefile +++ b/Makefile @@ -43,9 +43,9 @@ lint: .venv ## Lint code examples # python $(VENV_BIN)/black --check . -.PHONY: test-python -test-python: .venv ## Test Python code examples - find docs/src/python -type f | xargs -n 1 bash -c '$(VENV_BIN)/python -W error $$0 || exit 255' +.PHONY: test +test: .venv ## Test Python code examples + $(VENV_BIN)/pytest .PHONY: help help: ## Display this help screen diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 000000000..04e4a2224 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,5 @@ +[tool.pytest.ini_options] +filterwarnings = [ + "error", + "ignore:Matplotlib is currently using agg:UserWarning", +] diff --git a/requirements.txt b/requirements.txt index 5a2b0ca1f..e2856babd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,3 +11,4 @@ markdown-exec[ansi]==1.6.0 PyGithub==1.59.1 black==23.7.0 +pytest==7.4.0 diff --git a/tests/test_python.py b/tests/test_python.py new file mode 100644 index 000000000..8c756ffef --- /dev/null +++ b/tests/test_python.py @@ -0,0 +1,16 @@ +"""Run all Python code snippets.""" +import matplotlib +import pytest +import runpy +from pathlib import Path + +# Do not show plots +matplotlib.use("Agg") + +python_snippets_dir = Path(__file__).parent.parent / "docs" / "src" / "python" +snippets = list(python_snippets_dir.rglob("*.py")) + + +@pytest.mark.parametrize("snippet", snippets) +def test_run_python_snippets(snippet): + runpy.run_path(snippet)