From 2b22ce78bc8f82990bfd2c27c22932b8cbdc3faa Mon Sep 17 00:00:00 2001 From: Rasmus Oersoe Date: Tue, 26 Sep 2023 13:23:46 +0200 Subject: [PATCH] simplify unit tests --- .../01_icetray/test_icetray_examples.py | 46 ++++--------------- tests/examples/02_data/test_data_examples.py | 44 ++++-------------- .../03_weights/test_weights_examples.py | 25 +++------- .../04_training/test_training_examples.py | 42 ++++------------- 4 files changed, 31 insertions(+), 126 deletions(-) diff --git a/tests/examples/01_icetray/test_icetray_examples.py b/tests/examples/01_icetray/test_icetray_examples.py index 630f43f6f..cbdf470fd 100644 --- a/tests/examples/01_icetray/test_icetray_examples.py +++ b/tests/examples/01_icetray/test_icetray_examples.py @@ -1,47 +1,17 @@ """Test for examples in 01_icetray.""" import runpy import os +import pytest +from glob import glob + from graphnet.constants import GRAPHNET_ROOT_DIR EXAMPLE_PATH = os.path.join(GRAPHNET_ROOT_DIR, "examples/01_icetray") - -def test_01_convert_i3_files() -> None: - """Test for 01_convert_i3_files.""" - runpy.run_path( - os.path.join(EXAMPLE_PATH, "01_convert_i3_files.py"), - run_name="__main__", - ) - - -def test_02_compare_sqlite_and_parquet() -> None: - """Test for 02_compare_sqlite_and_parquet.""" - runpy.run_path( - os.path.join(EXAMPLE_PATH, "02_compare_sqlite_and_parquet.py"), - run_name="__main__", - ) - - -def test_03_i3_deployer_example() -> None: - """Test for 03_i3_deployer_example.""" - runpy.run_path( - os.path.join(EXAMPLE_PATH, "03_i3_deployer_example.py"), - run_name="__main__", - ) - - -def test_04_i3_module_in_native_icetray_example() -> None: - """Test for 04_i3_module_in_native_icetray_example.""" - runpy.run_path( - os.path.join( - EXAMPLE_PATH, "04_i3_module_in_native_icetray_example.py" - ), - run_name="__main__", - ) +examples = glob(EXAMPLE_PATH + "/*.py") -if __name__ == "__main__": - test_01_convert_i3_files() - test_02_compare_sqlite_and_parquet() - test_03_i3_deployer_example() - test_04_i3_module_in_native_icetray_example() +@pytest.mark.parametrize("example", examples) +def test_script_execution(example: str) -> None: + """Test function that executes example.""" + runpy.run_path(os.path.join(EXAMPLE_PATH, example)) diff --git a/tests/examples/02_data/test_data_examples.py b/tests/examples/02_data/test_data_examples.py index 4e1a9b40e..8faaca5c0 100644 --- a/tests/examples/02_data/test_data_examples.py +++ b/tests/examples/02_data/test_data_examples.py @@ -1,44 +1,16 @@ """Tests for examples in 02_data.""" import runpy import os +import pytest +from glob import glob + from graphnet.constants import GRAPHNET_ROOT_DIR EXAMPLE_PATH = os.path.join(GRAPHNET_ROOT_DIR, "examples/02_data") +examples = glob(EXAMPLE_PATH + "/*.py") -def test_01_read_dataset() -> None: - """Test for 01_read_dataset.""" - runpy.run_path( - os.path.join(EXAMPLE_PATH, "01_read_dataset.py"), run_name="__main__" - ) - - -def test_02_plot_feature_distribution() -> None: - """Test for 02_plot_feature_distribution.""" - runpy.run_path( - os.path.join(EXAMPLE_PATH, "02_plot_feature_distributions.py"), - run_name="__main__", - ) - - -def test_03_convert_parquet_to_sqlite() -> None: - """Test for 03_convert_parquet_to_sqlite.""" - runpy.run_path( - os.path.join(EXAMPLE_PATH, "03_convert_parquet_to_sqlite.py"), - run_name="__main__", - ) - - -def test_04_ensemble_dataset() -> None: - """Test for 04_ensemble_dataset.""" - runpy.run_path( - os.path.join(EXAMPLE_PATH, "04_ensemble_dataset.py"), - run_name="__main__", - ) - - -if __name__ == "__main__": - test_01_read_dataset() - test_02_plot_feature_distribution() - test_03_convert_parquet_to_sqlite() - test_04_ensemble_dataset() +@pytest.mark.parametrize("example", examples) +def test_script_execution(example: str) -> None: + """Test function that executes example.""" + runpy.run_path(os.path.join(EXAMPLE_PATH, example)) diff --git a/tests/examples/03_weights/test_weights_examples.py b/tests/examples/03_weights/test_weights_examples.py index 7ab580502..5dddee264 100644 --- a/tests/examples/03_weights/test_weights_examples.py +++ b/tests/examples/03_weights/test_weights_examples.py @@ -2,26 +2,15 @@ import runpy import os from graphnet.constants import GRAPHNET_ROOT_DIR +from glob import glob +import pytest EXAMPLE_PATH = os.path.join(GRAPHNET_ROOT_DIR, "examples/03_weights") +examples = glob(EXAMPLE_PATH + "/*.py") -def test_01_fit_uniform_weights() -> None: - """Test for 01_fit_uniform_weights.""" - runpy.run_path( - os.path.join(EXAMPLE_PATH, "01_fit_uniform_weights.py"), - run_name="__main__", - ) - -def test_02_fit_bjoern_low_weights() -> None: - """Test for 02_fit_bjoern_low_weights.""" - runpy.run_path( - os.path.join(EXAMPLE_PATH, "02_fit_bjoern_low_weights.py"), - run_name="__main__", - ) - - -if __name__ == "__main__": - test_01_fit_uniform_weights() - test_02_fit_bjoern_low_weights() +@pytest.mark.parametrize("example", examples) +def test_script_execution(example: str) -> None: + """Test function that executes example.""" + runpy.run_path(os.path.join(EXAMPLE_PATH, example)) diff --git a/tests/examples/04_training/test_training_examples.py b/tests/examples/04_training/test_training_examples.py index 4df5ba636..87cb955e4 100644 --- a/tests/examples/04_training/test_training_examples.py +++ b/tests/examples/04_training/test_training_examples.py @@ -1,44 +1,18 @@ """Test for examples in 04_training.""" import runpy import os +from glob import glob +import pytest + from graphnet.constants import GRAPHNET_ROOT_DIR EXAMPLE_PATH = os.path.join(GRAPHNET_ROOT_DIR, "examples/04_training") -def test_01_train_dynedge() -> None: - """Test for 01_train_dynedge.""" - runpy.run_path( - os.path.join(EXAMPLE_PATH, "01_train_dynedge.py"), run_name="__main__" - ) - - -def test_02_train_tito_model() -> None: - """Test for 02_train_tito_model.""" - runpy.run_path( - os.path.join(EXAMPLE_PATH, "02_train_tito_model.py"), - run_name="__main__", - ) - - -def test_03_train_dynedge_from_config() -> None: - """Test for 03_train_dynedge_from_config.""" - runpy.run_path( - os.path.join(EXAMPLE_PATH, "03_train_dynedge_from_config.py"), - run_name="__main__", - ) - - -def test_04_train_multiclassifier_from_configs() -> None: - """Test for 04_train_multiclassifier_from_configs.""" - runpy.run_path( - os.path.join(EXAMPLE_PATH, "04_train_multiclassifier_from_configs.py"), - run_name="__main__", - ) +examples = glob(EXAMPLE_PATH + "/*.py") -if __name__ == "__main__": - test_01_train_dynedge() - test_02_train_tito_model() - test_03_train_dynedge_from_config() - test_04_train_multiclassifier_from_configs() +@pytest.mark.parametrize("example", examples) +def test_script_execution(example: str) -> None: + """Test function that executes example.""" + runpy.run_path(os.path.join(EXAMPLE_PATH, example))