Skip to content

Commit

Permalink
Merge pull request #2382 from caleb-sitton-inl/unittest-tester
Browse files Browse the repository at this point in the history
Moved Unittest tester from FORCE
  • Loading branch information
dylanjm authored Oct 29, 2024
2 parents 515c65a + 2477df8 commit 0826f8a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
70 changes: 70 additions & 0 deletions scripts/TestHarness/testers/RavenUnittest.py
Original file line number Diff line number Diff line change
@@ -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"]])
4 changes: 2 additions & 2 deletions tests/framework/unit_tests/CustomDrivers/TestPythonRaven.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?')
Expand All @@ -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

Expand Down
5 changes: 5 additions & 0 deletions tests/framework/unit_tests/CustomDrivers/tests
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
input = 'TestPythonRaven.py'
[../]

[./test_unittest_tester]
type = 'Unittest'
input = 'TestPythonRaven.TestPythonRaven'
[../]

[./demo_python_raven]
type = 'RavenPython'
input = 'DemoPythonRaven.py'
Expand Down

0 comments on commit 0826f8a

Please sign in to comment.