diff --git a/scripts/TestHarness/testers/RavenUnittest.py b/scripts/TestHarness/testers/RavenUnittest.py new file mode 100644 index 0000000000..75fd9efc90 --- /dev/null +++ b/scripts/TestHarness/testers/RavenUnittest.py @@ -0,0 +1,70 @@ +""" +This runs tests written in python that use the unittest module. +""" + +import os +import sys + +from RavenPython import RavenPython + +try: + import unittest + unittest_found = True +except ModuleNotFoundError or ImportError: + unittest_found = False + +class Unittest(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() + # '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") + 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 ' '.join([pythonCommand, '-m unittest', self.specs["unittest_args"], self.specs["input"]]) diff --git a/tests/framework/unit_tests/CustomDrivers/TestPythonRaven.py b/tests/framework/unit_tests/CustomDrivers/TestPythonRaven.py index 20aa78028c..126e101bc9 100644 --- a/tests/framework/unit_tests/CustomDrivers/TestPythonRaven.py +++ b/tests/framework/unit_tests/CustomDrivers/TestPythonRaven.py @@ -67,7 +67,7 @@ def test_findFileFramework(self): # a pip installation or using an executable, looking for a file from the framework # directory doesn't make sense because those installations don't ship with the # ravenframework tests. We'll skip this test in those cases. - if os.path.abspath(os.path.join('..', 'tests', 'framework')) != frameworkTestDir: + if os.path.abspath(os.path.join('..', '..', '..', '..', 'tests', 'framework')) != frameworkTestDir: self.skipTest('Skipping test_findFileFramework because the framework tests directory is not where' + \ ' we expect it to be based on the location of the current file. Perhaps the installation' + \ ' of ravenframework is not the git installation?') @@ -88,7 +88,7 @@ def test_runXML(self): code = self.raven.runWorkflow() self.assertEqual(code, 0) -if __name__ == '__main__': +if __name__ == '__main__': # Not run when unittest called from command line or Unittest tester is used unittest.main() # note: return code is 1 if any tests fail/crash diff --git a/tests/framework/unit_tests/CustomDrivers/tests b/tests/framework/unit_tests/CustomDrivers/tests index 100a3c7c4f..75fcde110e 100644 --- a/tests/framework/unit_tests/CustomDrivers/tests +++ b/tests/framework/unit_tests/CustomDrivers/tests @@ -4,6 +4,11 @@ input = 'TestPythonRaven.py' [../] + [./test_unittest_tester] + type = 'Unittest' + input = 'TestPythonRaven.TestPythonRaven' + [../] + [./demo_python_raven] type = 'RavenPython' input = 'DemoPythonRaven.py'