diff --git a/tests/test_examples_biobuddy.py b/tests/test_examples_biobuddy.py new file mode 100644 index 0000000..7ec6462 --- /dev/null +++ b/tests/test_examples_biobuddy.py @@ -0,0 +1,22 @@ +""" +Tests for biobuddy examples. +Run each example script to ensure they execute without errors. +""" + +import pytest +from utils_examples import ExampleRunner + + +@pytest.mark.skipif( + not pytest.importorskip("biobuddy", reason="biobuddy is not installed"), + reason="biobuddy module not available", +) +def test_from_biobuddy_model(): + """ + Test the biobuddy example script. + """ + example_path = ExampleRunner.example_folder() / "biobuddy" / "from_biobuddy_model.py" + assert example_path.exists(), f"Example file not found: {example_path}" + + # Run the example + ExampleRunner.run_example_module(example_path) diff --git a/tests/test_examples_biorbd.py b/tests/test_examples_biorbd.py new file mode 100644 index 0000000..7b9ede6 --- /dev/null +++ b/tests/test_examples_biorbd.py @@ -0,0 +1,94 @@ +""" +Tests for biorbd examples. +Run each example script to ensure they execute without errors. +""" + +from utils_examples import ExampleRunner + + +def test_main_double_pendulum(): + """ + Test the main.py example with double pendulum (using animate function). + """ + example_path = ExampleRunner.example_folder() / "biorbd" / "main.py" + assert example_path.exists(), f"Example file not found: {example_path}" + + # Run the example + ExampleRunner.run_example_module(example_path) + + +def test_from_biorbd_model(): + """ + Test loading a biorbd model from biorbd object. + """ + example_path = ExampleRunner.example_folder() / "biorbd" / "from_biorbd_model.py" + assert example_path.exists(), f"Example file not found: {example_path}" + + # Run the example + ExampleRunner.run_example_module(example_path) + + +def test_gait_reconstruction(): + """ + Test the gait reconstruction example. + """ + example_path = ExampleRunner.example_folder() / "biorbd" / "gait_reconstruction.py" + assert example_path.exists(), f"Example file not found: {example_path}" + + # Run the example + ExampleRunner.run_example_module(example_path) + + +def test_marker_tracking(): + """ + Test the marker tracking example. + """ + example_path = ExampleRunner.example_folder() / "biorbd" / "marker_tracking.py" + assert example_path.exists(), f"Example file not found: {example_path}" + + # Run the example + ExampleRunner.run_example_module(example_path) + + +def test_meshline_model(): + """ + Test the meshline model example. + """ + example_path = ExampleRunner.example_folder() / "biorbd" / "meshline_model.py" + assert example_path.exists(), f"Example file not found: {example_path}" + + # Run the example + ExampleRunner.run_example_module(example_path) + + +def test_msk_model(): + """ + Test the MSK model with persistent markers example. + """ + example_path = ExampleRunner.example_folder() / "biorbd" / "msk_model.py" + assert example_path.exists(), f"Example file not found: {example_path}" + + # Run the example + ExampleRunner.run_example_module(example_path) + + +def test_multi_models(): + """ + Test the multi-models example. + """ + example_path = ExampleRunner.example_folder() / "biorbd" / "multi_models.py" + assert example_path.exists(), f"Example file not found: {example_path}" + + # Run the example + ExampleRunner.run_example_module(example_path) + + +def test_no_mesh(): + """ + Test the no mesh example. + """ + example_path = ExampleRunner.example_folder() / "biorbd" / "no_mesh.py" + assert example_path.exists(), f"Example file not found: {example_path}" + + # Run the example + ExampleRunner.run_example_module(example_path) diff --git a/tests/test_examples_c3d.py b/tests/test_examples_c3d.py new file mode 100644 index 0000000..25a2e9a --- /dev/null +++ b/tests/test_examples_c3d.py @@ -0,0 +1,40 @@ +""" +Tests for c3d examples. +Run each example script to ensure they execute without errors. +""" + +import pytest +from utils_examples import ExampleRunner + + +def test_c3d_main(): + """ + Test the main.py C3D example. + """ + example_path = ExampleRunner.example_folder() / "c3d" / "main.py" + assert example_path.exists(), f"Example file not found: {example_path}" + + # Run the example + ExampleRunner.run_example_module(example_path) + + +def test_c3d_gait(): + """ + Test the gait.py C3D example. + """ + example_path = ExampleRunner.example_folder() / "c3d" / "gait.py" + assert example_path.exists(), f"Example file not found: {example_path}" + + # Run the example + ExampleRunner.run_example_module(example_path) + + +def test_c3d_running_gait(): + """ + Test the running_gait.py C3D example. + """ + example_path = ExampleRunner.example_folder() / "c3d" / "running_gait.py" + assert example_path.exists(), f"Example file not found: {example_path}" + + # Run the example + ExampleRunner.run_example_module(example_path) diff --git a/tests/test_examples_osim.py b/tests/test_examples_osim.py new file mode 100644 index 0000000..04ceef7 --- /dev/null +++ b/tests/test_examples_osim.py @@ -0,0 +1,33 @@ +""" +Tests for OpenSim examples. +Run each example script to ensure they execute without errors. +""" + +import pytest +from utils_examples import ExampleRunner + + +@pytest.mark.skipif( + not pytest.importorskip("opensim", reason="opensim is not installed"), + reason="opensim module not available", +) +def test_from_osim_model(): + """ + Test the OpenSim model example. + """ + example_path = ExampleRunner.example_folder() / "osim" / "from_osim_model.py" + assert example_path.exists(), f"Example file not found: {example_path}" + + # Run the example + ExampleRunner.run_example_module(example_path) + + +def test_trc_reader(): + """ + Test the TRC reader example. + """ + example_path = ExampleRunner.example_folder() / "osim" / "trc_reader.py" + assert example_path.exists(), f"Example file not found: {example_path}" + + # Run the example + ExampleRunner.run_example_module(example_path) diff --git a/tests/utils_examples.py b/tests/utils_examples.py new file mode 100644 index 0000000..b50c740 --- /dev/null +++ b/tests/utils_examples.py @@ -0,0 +1,50 @@ +""" +Utility functions for running example scripts in tests. +""" + +import os +import sys +from pathlib import Path +import importlib.util + + +class ExampleRunner: + @staticmethod + def example_folder() -> Path: + """Get the path to the examples folder.""" + return Path(__file__).parent / "../examples" + + @staticmethod + def run_example_module(module_path: Path): + """ + Dynamically import and run an example module. + + Args: + module_path: Path to the example Python file to run + """ + # Add the module's directory to sys.path temporarily + module_dir = str(module_path.parent) + original_cwd = os.getcwd() + original_path = sys.path.copy() + + try: + # Change to the example directory + os.chdir(module_dir) + sys.path.insert(0, module_dir) + + # Load the module + spec = importlib.util.spec_from_file_location("test_module", module_path) + if spec and spec.loader: + module = importlib.util.module_from_spec(spec) + + # Execute the module + spec.loader.exec_module(module) + + # If the module has a main function, call it + if hasattr(module, "main"): + module.main() + + finally: + # Restore original directory and path + os.chdir(original_cwd) + sys.path = original_path