Skip to content

Commit

Permalink
add testing of notebooks as a special script
Browse files Browse the repository at this point in the history
  • Loading branch information
arogozhnikov committed Sep 16, 2024
1 parent f7385c8 commit 487e88a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/test_notebooks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Test notebooks

on: [push, pull_request]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8', '3.12']

steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: pip
- name: Install dependencies to run/test notebooks
run: |
pip install nbformat nbconvert jupyter pillow numpy torch tensorflow
- name: Testing notebooks
run: |
pip install -e . && pytest scripts/test_notebooks.py
27 changes: 11 additions & 16 deletions tests/test_notebooks.py → scripts/test_notebooks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
"""
Script assumes torch, tf and numpy are already installed.
Also needs:
"nbformat",
"nbconvert",
"jupyter",
"pillow",
"""

from typing import Dict

from io import StringIO

from tests import is_backend_tested

__author__ = "Alex Rogozhnikov"

from pathlib import Path
import nbformat
import pytest
from nbconvert.preprocessors import ExecutePreprocessor


Expand Down Expand Up @@ -40,17 +48,8 @@ def test_notebook_1():

def test_notebook_2_with_all_backends():
[notebook] = Path(__file__).parent.with_name("docs").glob("2-*.ipynb")
backends = []
if is_backend_tested("torch"):
# notebook uses name pytorch
backends.append("pytorch")
if is_backend_tested("tensorflow"):
backends.append("tensorflow")

if len(backends) == 0:
pytest.skip()

for backend in backends:
for backend in ["pytorch", "tensorflow"]:
print("Testing {} with backend {}".format(notebook, backend))
replacements = {r"flavour = \"pytorch\"": r"flavour = \"{}\"".format(backend)}
expected_string = "selected {} backend".format(backend)
Expand All @@ -60,13 +59,9 @@ def test_notebook_2_with_all_backends():

def test_notebook_3():
[notebook] = Path(__file__).parent.with_name("docs").glob("3-*.ipynb")
if not is_backend_tested("torch"):
pytest.skip()
render_notebook(notebook, replacements={})


def test_notebook_4():
[notebook] = Path(__file__).parent.with_name("docs").glob("4-*.ipynb")
if not is_backend_tested("torch"):
pytest.skip()
render_notebook(notebook, replacements={})

0 comments on commit 487e88a

Please sign in to comment.