Skip to content

Commit

Permalink
Update pytest unit tests for MC scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
Natsoulas committed Nov 5, 2024
1 parent 40372d7 commit 3cc5239
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 36 deletions.
53 changes: 25 additions & 28 deletions src/tests/test_bskMcTestScript.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
7 changes: 3 additions & 4 deletions src/tests/test_scenarioMonteCarloAttRW.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
6 changes: 2 additions & 4 deletions src/tests/test_scenarioMonteCarloSpice.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -55,5 +55,3 @@ def test_MonteCarloSimulationDatashader(show_plots):
assert testFailCount < 1, testMessages

return


0 comments on commit 3cc5239

Please sign in to comment.