Skip to content

Commit

Permalink
Merge pull request #2818 from vkarak/bugfix/make-test_module
Browse files Browse the repository at this point in the history
[enhancement] Set the module of test created by `make_test`
  • Loading branch information
vkarak committed Mar 8, 2023
2 parents b84d51d + 43770b1 commit 9992947
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
17 changes: 15 additions & 2 deletions reframe/core/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#

import functools
import inspect
import types
import collections

Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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)
Expand All @@ -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
17 changes: 17 additions & 0 deletions unittests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ['*']
Expand Down

0 comments on commit 9992947

Please sign in to comment.