From 43770b19fa7079840ed8c075d19d9f34c17528bd Mon Sep 17 00:00:00 2001 From: Vasileios Karakasis Date: Sat, 4 Mar 2023 01:01:20 +0100 Subject: [PATCH] Fix module of test created by `make_test` --- reframe/core/meta.py | 17 +++++++++++++++-- unittests/test_pipeline.py | 17 +++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/reframe/core/meta.py b/reframe/core/meta.py index e0085d72ce..c14bfd4016 100644 --- a/reframe/core/meta.py +++ b/reframe/core/meta.py @@ -8,6 +8,7 @@ # import functools +import inspect import types import collections @@ -821,7 +822,7 @@ def loggable_attrs(cls): return sorted(loggable_props + loggable_vars + loggable_params) -def make_test(name, bases, body, methods=None, **kwargs): +def make_test(name, bases, body, methods=None, module=None, **kwargs): '''Define a new test class programmatically. Using this method is completely equivalent to using the :keyword:`class` @@ -890,13 +891,18 @@ def validate(obj): :param methods: A list of functions to be bound as methods to the class that is being created. The functions will be bound with their original name. + :param module: The module name of the new test class. + If :obj:`None`, the module of the caller will be used. :param kwargs: Any keyword arguments to be passed to the :class:`RegressionTestMeta` metaclass. .. versionadded:: 3.10.0 .. versionchanged:: 3.11.0 - Added the ``methods`` arguments. + Added the ``methods`` argument. + + .. versionadded:: 4.2 + Added the ``module`` argument. ''' namespace = RegressionTestMeta.__prepare__(name, bases, **kwargs) @@ -915,4 +921,11 @@ def validate(obj): namespace.reset(k) cls = RegressionTestMeta(name, bases, namespace, **kwargs) + + if not module: + # Set the test's module to be that of our callers + caller = inspect.currentframe().f_back + module = caller.f_globals['__name__'] + + cls.__module__ = module return cls diff --git a/unittests/test_pipeline.py b/unittests/test_pipeline.py index 6f6d506811..0bb95574b9 100644 --- a/unittests/test_pipeline.py +++ b/unittests/test_pipeline.py @@ -1835,9 +1835,26 @@ def test_make_test_without_builtins(local_exec_ctx): ) assert hello_cls.__name__ == 'HelloTest' + assert hello_cls.__module__ == 'unittests.test_pipeline' _run(hello_cls(), *local_exec_ctx) +def test_make_test_with_module(): + hello_cls = make_test( + 'HelloTest', (rfm.RunOnlyRegressionTest,), + { + 'valid_systems': ['*'], + 'valid_prog_environs': ['*'], + 'executable': 'echo', + 'sanity_patterns': sn.assert_true(1) + }, + module='foo' + ) + + assert hello_cls.__name__ == 'HelloTest' + assert hello_cls.__module__ == 'foo' + + def test_make_test_with_builtins(local_exec_ctx): class _X(rfm.RunOnlyRegressionTest): valid_systems = ['*']