Skip to content

Commit

Permalink
Test examples in CI (#29)
Browse files Browse the repository at this point in the history
Create a CI configuration with GitHub Actions that runs all the examples in CI, to see if all existing examples keep working when changes are made.
  • Loading branch information
EwoutH authored Mar 5, 2024
1 parent 31d8494 commit 0e0668f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/run-examples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Run Python examples

on:
push:
pull_request:
workflow_dispatch:
schedule:
- cron: '0 6 * * 1'

jobs:
run-examples:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install uxsim and dependencies
run: pip install .
- name: Install pytest other dependencies
run: pip install pytest setuptools gymnasium torch
- name: Run examples with pytest
run: pytest demos_and_examples/test_examples.py --durations=0
23 changes: 23 additions & 0 deletions demos_and_examples/test_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest
import subprocess
import os

# Directory containing example scripts
examples_dir = 'demos_and_examples'

# Dynamically generate test cases for each example script
def pytest_generate_tests(metafunc):
# List all .py files in the examples_dir
example_scripts = [f for f in os.listdir(examples_dir) if f.endswith('.py')]
# If the test function expects an "example_script" argument, parametrize it
if 'example_script' in metafunc.fixturenames:
metafunc.parametrize('example_script', example_scripts)

def test_example_runs(example_script):
"""Test that a Python example script runs successfully."""
# Build the script path
script_path = os.path.join(examples_dir, example_script)
# Run the script as a separate process
result = subprocess.run(['python', script_path], capture_output=True, text=True)
# Assert that the script ran successfully
assert result.returncode == 0, f"Script {example_script} failed with output:\n{result.stdout}\n{result.stderr}"

0 comments on commit 0e0668f

Please sign in to comment.