Skip to content

Commit

Permalink
Add notebook testing workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
classiqdor committed May 7, 2024
1 parent b1b08ac commit a389bb9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/test-notebooks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,33 @@ jobs:
python -m pip install -U -r requirements.txt
python -m pip install -U -r requirements_tests.txt
- name: Get changed files - all
id: changed-files-all
uses: tj-actions/changed-files@v44
- name: Get changed files - ipynb
id: changed-files-ipynb
uses: tj-actions/changed-files@v44
with:
files: |
**.ipynb
- name: Set environment variables
run: |
if [ "${{ github.event_name }}" == 'pull_request' ]; then
echo "SHOULD_TEST_ALL_FILES=false" >> $GITHUB_ENV
echo "HAS_ANY_FILE_CHANGED=${{ steps.changed-files-all.outputs.any_changed }}" >> $GITHUB_ENV
echo "LIST_OF_FILE_CHANGED=${{ steps.changed-files-all.outputs.all_changed_files }}" >> $GITHUB_ENV
echo "HAS_ANY_IPYNB_CHANGED=${{ steps.changed-files-ipynb.outputs.any_changed }}" >> $GITHUB_ENV
echo "LIST_OF_IPYNB_CHANGED=${{ steps.changed-files-ipynb.outputs.all_changed_files }}" >> $GITHUB_ENV
elif [ "${{ github.event_name }}" == 'workflow_dispatch' ]; then
echo "SHOULD_TEST_ALL_FILES=true" >> $GITHUB_ENV
echo "HAS_ANY_FILE_CHANGED=None" >> $GITHUB_ENV
echo "LIST_OF_FILE_CHANGED=None" >> $GITHUB_ENV
echo "HAS_ANY_IPYNB_CHANGED=None" >> $GITHUB_ENV
echo "LIST_OF_IPYNB_CHANGED=None" >> $GITHUB_ENV
fi
- name: 'Run tests'
run: python -m pytest tests
env:
JUPYTER_PLATFORM_DIRS: "1"
34 changes: 34 additions & 0 deletions tests/test_notebooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
from collections.abc import Iterable
from pathlib import Path

from testbook import testbook

TIMEOUT: int = 60

ROOT_DIRECTORY = Path(__file__).parent


def test_notebooks():
for notebook_path in _get_notebooks():
with testbook(notebook_path, execute=True, timeout=TIMEOUT) as tb:
pass # we simply wish it to run without errors


def _get_notebooks() -> Iterable[str]:
if os.environ.get("SHOULD_TEST_ALL_FILES", "") == "true":
notebooks_to_test = _get_all_notebooks()
else:
if os.environ.get("HAS_ANY_IPYNB_CHANGED", "") == "true":
notebooks_to_test = os.environ.get("LIST_OF_IPYNB_CHANGED", "").split()
else:
notebooks_to_test = []

return notebooks_to_test


def _get_all_notebooks(directory=ROOT_DIRECTORY):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(".ipynb"):
yield os.path.join(root, file)

0 comments on commit a389bb9

Please sign in to comment.