Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
Use pytest to run Python code snippets (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego authored Aug 29, 2023
1 parent 534eac2 commit 2404455
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# Python
/.venv
__pycache__
.pytest_cache/

# Project
/docs/data/
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[tool.pytest.ini_options]
filterwarnings = [
"error",
"ignore:Matplotlib is currently using agg:UserWarning",
]
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ markdown-exec[ansi]==1.6.0
PyGithub==1.59.1

black==23.7.0
pytest==7.4.0
16 changes: 16 additions & 0 deletions tests/test_python.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 2404455

Please sign in to comment.