From 3cc52394190637c76d976a4c06035b05803661a7 Mon Sep 17 00:00:00 2001 From: Natsoulas Date: Mon, 4 Nov 2024 22:18:42 -0700 Subject: [PATCH] Update pytest unit tests for MC scenarios --- src/tests/test_bskMcTestScript.py | 53 +++++++++++------------ src/tests/test_scenarioMonteCarloAttRW.py | 7 ++- src/tests/test_scenarioMonteCarloSpice.py | 6 +-- 3 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/tests/test_bskMcTestScript.py b/src/tests/test_bskMcTestScript.py index f1ee8e9167..da71c6dfbe 100644 --- a/src/tests/test_bskMcTestScript.py +++ b/src/tests/test_bskMcTestScript.py @@ -1,7 +1,7 @@ # # ISC License # -# Copyright (c) 2016, Autonomous Vehicle Systems Lab, University of Colorado at Boulder +# Copyright (c) 2024, Autonomous Vehicle Systems Lab, University of Colorado at Boulder # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above @@ -22,63 +22,60 @@ # # Purpose: This script calls a series of bskSim Monte Carlo related simulations to ensure # that they complete properly. -# Creation Date: Nov 18, 2019 +# Recenlty updated: Nov 4, 2024 # import importlib import inspect import os -import platform import shutil import sys - import pytest +# Check if Bokeh is available +bokeh_spec = importlib.util.find_spec("bokeh") +bokeh_available = bokeh_spec is not None + filename = inspect.getframeinfo(inspect.currentframe()).filename path = os.path.dirname(os.path.abspath(filename)) sys.path.append(path + '/../../examples/MonteCarloExamples') - -# uncomment this line is this test is to be skipped in the global unit test run, adjust message as needed -# @pytest.mark.skipif(conditionstring) -# uncomment this line if this test has an expected failure, adjust message as needed -# @pytest.mark.xfail(True, reason="Previously set sim parameters are not consistent with new formulation\n") - -# @pytest.mark.skip(reason="MC can have issues completing.") - +# Skip test if Python version is less than 3.9 @pytest.mark.skipif(sys.version_info < (3, 9), reason="Test has issues with Controller class and older python.") +# Skip test if Bokeh is not available +@pytest.mark.skipif(not bokeh_available, + reason="Bokeh is not available. Skipping test.") @pytest.mark.slowtest @pytest.mark.scenarioTest def test_scenarioBskMcScenarios(show_plots): # These need to be run in serial such that the data is produced for analysis - scenarios = ['scenario_AttFeedbackMC', - 'scenarioAnalyzeMonteCarlo', - 'scenarioRerunMonteCarlo'] + scenarios = ['scenarioBskSimAttFeedbackMC', + 'scenarioVisualizeMonteCarlo'] testFailCount = 0 # zero unit test result counter testMessages = [] # create empty array to store test log messages - for bskSimCase in scenarios: + for i, bskSimCase in enumerate(scenarios): # import the bskSim script to be tested scene_plt = importlib.import_module(bskSimCase) try: - figureList = scene_plt.run(False) - - except OSError as err: - testFailCount = testFailCount + 1 - testMessages.append("OS error: {0}".format(err)) - - - print(path+ "/../../examples/MonteCarloExamples/scenario_AttFeedbackMC/") - if os.path.exists(path+ "/../../examples/MonteCarloExamples/scenario_AttFeedbackMC/"): - shutil.rmtree(path+ "/../../examples/MonteCarloExamples/scenario_AttFeedbackMC/") - # each test method requires a single assert method to be called - # this check below just makes sure no sub-test failures were found + if i == 0: + figureList = scene_plt.run(False) + else: + scene_plt.run() + + except Exception as err: + testFailCount += 1 + testMessages.append(f"Error in {bskSimCase}: {str(err)}") + + # Clean up + if os.path.exists(path + "/../../examples/MonteCarloExamples/scenarioBskSimAttFeedbackMC/"): + shutil.rmtree(path + "/../../examples/MonteCarloExamples/scenarioBskSimAttFeedbackMC/") assert testFailCount < 1, testMessages diff --git a/src/tests/test_scenarioMonteCarloAttRW.py b/src/tests/test_scenarioMonteCarloAttRW.py index b7ddc806dc..27e6abfff5 100644 --- a/src/tests/test_scenarioMonteCarloAttRW.py +++ b/src/tests/test_scenarioMonteCarloAttRW.py @@ -1,7 +1,7 @@ # # ISC License # -# Copyright (c) 2016, Autonomous Vehicle Systems Lab, University of Colorado at Boulder +# Copyright (c) 2024, Autonomous Vehicle Systems Lab, University of Colorado at Boulder # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above @@ -38,12 +38,11 @@ # Run initial conditions and plot with matplotlib -@pytest.mark.parametrize("MCCases", - [1,2]) +@pytest.mark.parametrize("MCCases", [1, 2]) @pytest.mark.slowtest @pytest.mark.scenarioTest def test_MonteCarloSimulation(show_plots, MCCases): """This function is called by the py.test environment.""" # each test method requires a single assert method to be called - scenarioMonteCarloAttRW.run(True, MCCases , show_plots) + scenarioMonteCarloAttRW.run(True, MCCases, show_plots, use_bokeh=False) return diff --git a/src/tests/test_scenarioMonteCarloSpice.py b/src/tests/test_scenarioMonteCarloSpice.py index b068a33012..a63280819d 100644 --- a/src/tests/test_scenarioMonteCarloSpice.py +++ b/src/tests/test_scenarioMonteCarloSpice.py @@ -1,7 +1,7 @@ # # ISC License # -# Copyright (c) 2016, Autonomous Vehicle Systems Lab, University of Colorado at Boulder +# Copyright (c) 2024, Autonomous Vehicle Systems Lab, University of Colorado at Boulder # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above @@ -39,7 +39,7 @@ @pytest.mark.skipif(sys.version_info < (3, 9) and platform.system() == 'Darwin', reason="Test has issues with Controller class and older python.") @pytest.mark.scenarioTest -def test_MonteCarloSimulationDatashader(show_plots): +def test_MonteCarloSimulationSpice(show_plots): """This function is called by the py.test environment.""" testFailCount = 0 # zero unit test result counter @@ -55,5 +55,3 @@ def test_MonteCarloSimulationDatashader(show_plots): assert testFailCount < 1, testMessages return - -