Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pytest script and CI job to test tutorials #2275

Closed
wants to merge 7 commits into from
Closed
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
23 changes: 0 additions & 23 deletions .github/workflows/build_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,3 @@ jobs:
- if: matrix.os == 'ubuntu'
name: Codecov
uses: codecov/codecov-action@v4

examples:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: 'pip'
- name: Install uv
run: pip install uv
- name: Install Mesa
run: uv pip install --system .[examples]
- name: Checkout mesa-examples
uses: actions/checkout@v4
with:
repository: projectmesa/mesa-examples
path: mesa-examples
- name: Test examples
run: |
cd mesa-examples
pytest -rA -Werror -Wdefault::FutureWarning test_examples.py
61 changes: 61 additions & 0 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Examples

on:
push:
branches:
- main
- release**
- "**maintenance"
paths-ignore:
- '**.md'
pull_request:
paths-ignore:
- '**.md'
workflow_dispatch:
schedule:
- cron: '0 6 * * 1'

jobs:
examples:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: 'pip'
- name: Install uv
run: pip install uv
- name: Install dependencies
run: uv pip install --system .[examples]
- name: Checkout mesa-examples
uses: actions/checkout@v4
with:
repository: projectmesa/mesa-examples
path: mesa-examples
- name: Test examples
run: |
cd mesa-examples
pytest -rA -Werror -Wdefault::FutureWarning test_examples.py

tutorials:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: 'pip'
- name: Install uv
run: pip install uv
- name: Install Mesa dependencies
run: uv pip install --system .
- name: Install tutorial dependencies
run: uv pip install --system pytest jupyter nbconvert seaborn pandas
# Run Pytest on Jupyter Notebooks
- name: Test Jupyter Notebooks with Pytest
env:
JUPYTER_PLATFORM_DIRS: 1
run: pytest -rA -Werror -Wdefault::FutureWarning tests/test_tutorials.py
2 changes: 1 addition & 1 deletion docs/tutorials/visualization_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@
" # plt.figure(), for thread safety purpose\n",
" fig = Figure()\n",
" ax = fig.subplots()\n",
" wealth_vals = [agent.wealth for agent in model.agents]\n",
" wealth_vals = [agent.wealth for agent in model.schedule.agents]\n",
" # Note: you have to use Matplotlib's OOP API instead of plt.hist\n",
" # because plt.hist is not thread-safe.\n",
" ax.hist(wealth_vals, bins=10)\n",
Expand Down
23 changes: 23 additions & 0 deletions tests/test_tutorials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import glob

import nbformat
import pytest
from nbconvert.preprocessors import ExecutePreprocessor

notebooks = glob.glob("docs/**/*.ipynb", recursive=True)


@pytest.mark.parametrize("notebook_path", notebooks)
def test_notebook_execution(notebook_path):
# Load the notebook
with open(notebook_path) as f:
nb = nbformat.read(f, as_version=4)

# Set up the notebook execution
ep = ExecutePreprocessor(timeout=600, kernel_name="python3")

try:
# Execute the notebook and catch any errors
ep.preprocess(nb, {"metadata": {"path": "./"}})
except Exception as e:
pytest.fail(f"Notebook {notebook_path} failed: {e!s}")
Loading