From 16a0c014641b57e15f911319fc772e783813ddfa Mon Sep 17 00:00:00 2001 From: Caleb Sitton Date: Fri, 2 Aug 2024 10:26:17 -0600 Subject: [PATCH 1/2] Started work on unittest tester --- UnittestTester.py | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 UnittestTester.py diff --git a/UnittestTester.py b/UnittestTester.py new file mode 100644 index 00000000..91d63972 --- /dev/null +++ b/UnittestTester.py @@ -0,0 +1,65 @@ +import os +from raven.scripts import library_handler +from raven.scripts.TestHarness.testers import RavenPython + +try: + import unittest + unittest_found = True +except ModuleNotFoundError or ImportError: + unittest_found = False + +class UnittestTester(RavenPython): + """ + This class simplifies use of the unittest module for running unit tests through rook. + """ + + @staticmethod + def get_valid_params(): + """ + Return a list of valid parameters and their descriptions for this type + of test. + @ In, None + @ Out, params, _ValidParameters, the parameters for this class. + """ + params = RavenPython.get_valid_params() + params.add_param('unittest_args', '', "Arguments to the unittest module") + params.add_param('test_sequence_to_run', '', "Ordered list of test cases in the input file to run") + return params + + def __init__(self, name, params): + """ + Initializer for the class. Takes a String name and a dictionary params + @ In, name, string, name of the test. + @ In, params, dictionary, parameters for the class + @ Out, None. + """ + RavenPython.__init__(self, name, params) + + def check_runnable(self): + """ + Checks if this test can be run. + @ In, None + @ Out, check_runnable, boolean, If True can run this test. + """ + if not unittest_found: + self.set_skip('skipped (required unittest module is not found)') + return False + + return RavenPython.check_runnable(self) + + def get_command(self): + """ + returns the command used by this tester. + @ In, None + @ Out, get_command, string, command to run. + """ + # If the test command has been specified, use it + if (command := self._get_test_command()) is not None: + return ' '.join([command, '-m unittest', self.specs["unittest_args"], self.specs["input"]]) + + # Otherwise, if the python command has been specified, use it + if len(self.specs["python_command"]) == 0: + pythonCommand = self._get_python_command() + else: + pythonCommand = self.specs["python_command"] + return pythonCommand+" "+self.specs["input"] From d2a6adea259d3757f871bccf23df0a0af5e1ae0c Mon Sep 17 00:00:00 2001 From: Caleb Sitton Date: Mon, 23 Sep 2024 09:35:24 -0500 Subject: [PATCH 2/2] UnittestTester operational and used for unit tests --- developer_tools/rook.ini | 2 +- run_tests | 2 +- .../Testers/UnittestTester.py | 15 +++++--- tests/unit_tests/tests | 36 +++++++++---------- 4 files changed, 30 insertions(+), 25 deletions(-) rename UnittestTester.py => src/Testers/UnittestTester.py (80%) diff --git a/developer_tools/rook.ini b/developer_tools/rook.ini index b13eaeef..e9985b3c 100644 --- a/developer_tools/rook.ini +++ b/developer_tools/rook.ini @@ -1,4 +1,4 @@ [rook] add_non_default_run_types = qsub -testers_dir = HERON/src/Testers +testers_dir = HERON/src/Testers, src/Testers test_dir = tests \ No newline at end of file diff --git a/run_tests b/run_tests index 3b4d3d24..90af1e0b 100755 --- a/run_tests +++ b/run_tests @@ -91,7 +91,7 @@ case $TEST_SET in ;; esac -$PYTHON_COMMAND $SCRIPT_DIR/raven/rook/main.py --config-file=$SCRIPT_DIR/developer_tools/rook.ini --test-dir $TEST_DIR --testers-dir $RAVEN_DIR/scripts/TestHarness/testers,$RAVEN_DIR/../HERON/src/Testers "${ARGS[@]}" +$PYTHON_COMMAND $SCRIPT_DIR/raven/rook/main.py --config-file=$SCRIPT_DIR/developer_tools/rook.ini --test-dir $TEST_DIR --testers-dir $RAVEN_DIR/scripts/TestHarness/testers,$RAVEN_DIR/../HERON/src/Testers,$RAVEN_DIR/../src/Testers "${ARGS[@]}" # store return codes individually (rc) and combined (ALL_PASS) rc=$? diff --git a/UnittestTester.py b/src/Testers/UnittestTester.py similarity index 80% rename from UnittestTester.py rename to src/Testers/UnittestTester.py index 91d63972..d554d2c0 100644 --- a/UnittestTester.py +++ b/src/Testers/UnittestTester.py @@ -1,6 +1,11 @@ import os -from raven.scripts import library_handler -from raven.scripts.TestHarness.testers import RavenPython +import sys + +FORCE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) +sys.path.append(FORCE_DIR) +from raven.scripts.TestHarness.testers.RavenPython import RavenPython +# clear scripts from path +sys.path.pop() try: import unittest @@ -8,7 +13,7 @@ except ModuleNotFoundError or ImportError: unittest_found = False -class UnittestTester(RavenPython): +class Unittest(RavenPython): """ This class simplifies use of the unittest module for running unit tests through rook. """ @@ -22,8 +27,8 @@ def get_valid_params(): @ Out, params, _ValidParameters, the parameters for this class. """ params = RavenPython.get_valid_params() + # 'input' param can be test case or test suite; unittest will handle either when called params.add_param('unittest_args', '', "Arguments to the unittest module") - params.add_param('test_sequence_to_run', '', "Ordered list of test cases in the input file to run") return params def __init__(self, name, params): @@ -62,4 +67,4 @@ def get_command(self): pythonCommand = self._get_python_command() else: pythonCommand = self.specs["python_command"] - return pythonCommand+" "+self.specs["input"] + return ' '.join([pythonCommand, '-m unittest', self.specs["unittest_args"], self.specs["input"]]) diff --git a/tests/unit_tests/tests b/tests/unit_tests/tests index 4a340f4e..8bcaa970 100644 --- a/tests/unit_tests/tests +++ b/tests/unit_tests/tests @@ -1,46 +1,46 @@ [Tests] [./TestMinimalInput] - type = RavenPython - input = '-m unittest test_heron.TestMinimalInput' + type = Unittest + input = 'test_heron.TestMinimalInput' [../] [./TestExpandedInput1] - type = RavenPython - input = '-m unittest test_heron.TestExpandedInput1' + type = Unittest + input = 'test_heron.TestExpandedInput1' [../] [./TestExpandedInput2] - type = RavenPython - input = '-m unittest test_heron.TestExpandedInput2' + type = Unittest + input = 'test_heron.TestExpandedInput2' [../] [./TestNoComponentsNode] - type = RavenPython - input = '-m unittest test_heron.TestNoComponentsNode' + type = Unittest + input = 'test_heron.TestNoComponentsNode' [../] [./TestNoComponentNodes] - type = RavenPython - input = '-m unittest test_heron.TestNoComponentNodes' + type = Unittest + input = 'test_heron.TestNoComponentNodes' [../] [./TestMissingSubnodes] - type = RavenPython - input = '-m unittest test_heron.TestMissingSubnodes' + type = Unittest + input = 'test_heron.TestMissingSubnodes' [../] [./TestEmptyCompSetsFolder] - type = RavenPython - input = '-m unittest test_heron.TestEmptyCompSetsFolder' + type = Unittest + input = 'test_heron.TestEmptyCompSetsFolder' [../] [./TestCompSetsFolderWithBadJSON] - type = RavenPython - input = '-m unittest test_heron.TestCompSetsFolderWithBadJSON' + type = Unittest + input = 'test_heron.TestCompSetsFolderWithBadJSON' [../] [./TestCompSetsFolderMultFiles] - type = RavenPython - input = '-m unittest test_heron.TestCompSetsFolderMultFiles' + type = Unittest + input = 'test_heron.TestCompSetsFolderMultFiles' [../] []