Skip to content

Commit

Permalink
adds set_module_info and invokes via setUpClass with new filename and…
Browse files Browse the repository at this point in the history
… module keyword args; invoke_tests for pytest (or unittest) invocation; adds VIA_UNITTEST; adds warning that check_coverage not general purpose; todo note on using path delim
  • Loading branch information
tomasohara committed Jan 16, 2024
1 parent a370884 commit a5091bf
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions mezcla/unittest_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@
THIS_PACKAGE = getattr(mezcla.debug, "__package__", None)
debug.assertion(THIS_PACKAGE == "mezcla")

# Environment options
VIA_UNITTEST = system.getenv_bool(
"VIA_UNITTEST", False,
description="Run tests via unittest instead of pytest")

#-------------------------------------------------------------------------------

def get_temp_dir(keep=None):
"""Get temporary directory, omitting later deletion if KEEP"""
Expand Down Expand Up @@ -134,6 +140,14 @@ def wrapper(x):
debug.trace(7, f"pytest_fixture_wrapper() => {gh.elide(wrapper)}")
return wrapper

def invoke_tests(filename, via_unittest=VIA_UNITTEST):
"""Invoke TESTS defined in FILENAME, optionally VIA_UNITTEST"""
if via_unittest:
unittest.main()
else:
pytest.main(filename)

#-------------------------------------------------------------------------------

class TestWrapper(unittest.TestCase):
"""Class for testcase definition
Expand Down Expand Up @@ -166,9 +180,11 @@ class TestWrapper(unittest.TestCase):
## __test__ = False # make sure not assumed test

@classmethod
def setUpClass(cls):
"""Per-class initialization: make sure script_module set properly"""
debug.trace_fmtd(6, "TestWrapper.setupClass(): cls={c}", c=cls)
def setUpClass(cls, filename=None, module=None):
"""Per-class initialization: make sure script_module set properly
Note: Optional FILENAME is path for testing script and MODULE the imported object for tested script
"""
debug.trace(6, f"TestWrapper.setupClass({cls}, fn={filename}, mod={module})")
super().setUpClass()
cls.class_setup = True
debug.trace_object(7, cls, "TestWrapper class")
Expand All @@ -194,6 +210,18 @@ def setUpClass(cls):
## TODO: gh.full_mkdir
gh.run("mkdir -p {dir}", dir=cls.temp_base)

# Warn that coverage support is limited
if cls.check_coverage:
# note: For proper invocation info, see https://coverage.readthedocs.io/en/latest
test_module = "pytest ..." if not VIA_UNITTEST else "unittest discover"
debug.trace(4, "FYI: coverage check only covers run_script usages; "
"invoke externally for more general support:\n"
f" python -m coverage run -m {test_module}")

# Optionally, setup up script_file and script_module
if filename:
cls.set_module_info(filename, module_object=module)

return

@staticmethod
Expand Down Expand Up @@ -236,11 +264,23 @@ def get_testing_module_name(test_filename, module_object=None):
def get_module_file_path(test_filename):
"""Return absolute path of module being tested"""
result = system.absolute_path(test_filename)
## TODO3: use os.path.delim instead of /
result = my_re.sub(r'tests\/test_(.*\.py)', r'\1', result)
debug.assertion(result.endswith(".py"))
debug.trace(7, f'get_module_file_path({test_filename}) => {result}')
return result

@classmethod
def set_module_info(cls, test_filename, module_object=None):
"""Sets both script_module and script_path
Note: normally invoked in setupClass method
Usage: cls.set_module_info(__file__, THE_MODULE)
"""
debug.trace(7, f'set_module_info({test_filename}, {module_object})')
cls.script_module = cls.get_testing_module_name(test_filename, module_object)
cls.script_file = cls.get_module_file_path(test_filename)
return

def setUp(self):
"""Per-test initializations
Notes:
Expand Down

0 comments on commit a5091bf

Please sign in to comment.