You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/tutorial.rst
+40-1Lines changed: 40 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -418,6 +418,45 @@ In the following example ``var`` will be set to ``2`` after the setup phase is e
418
418
definc(self):
419
419
self.var +=1
420
420
421
+
Another important feature of the hooks syntax, is that hooks are inherited by derived tests, unless you override the function and re-hook it explicitly.
422
+
In the following example, the :func:`setflags()` will be executed before the compilation phase of the :class:`DerivedTest`:
423
+
424
+
.. code:: python
425
+
426
+
classBaseTest(rfm.RegressionTest):
427
+
def__init__(self):
428
+
...
429
+
self.build_system ='Make'
430
+
431
+
@rfm.run_before('compile')
432
+
defsetflags(self):
433
+
ifself.current_environ.name =='X':
434
+
self.build_system.cppflags = ['-Ifoo']
435
+
436
+
437
+
@rfm.simple_test
438
+
classDerivedTest(BaseTest):
439
+
def__init__(self):
440
+
super().__init__()
441
+
...
442
+
443
+
444
+
If you override a hooked function in a derived class, the base class' hook will not be executed, unless you explicitly call it with ``super()``.
445
+
In the following example, we completely disable the :func:`setflags()` hook of the base class:
446
+
447
+
448
+
.. code:: python
449
+
450
+
@rfm.simple_test
451
+
classDerivedTest(BaseTest):
452
+
@rfm.run_before('compile')
453
+
defsetflags(self):
454
+
pass
455
+
456
+
457
+
Notice that in order to redefine a hook, you need not only redefine the method in the derived class, but you should hook it at the same pipeline phase.
458
+
Otherwise, the base class hook will be executed.
459
+
421
460
422
461
.. note::
423
462
You may still configure your test per programming environment and per system partition by overriding the :func:`setup <reframe.core.pipeline.RegressionTest.setup>` method, as in ReFrame versions prior to 2.20, but this is now discouraged since it is more error prone, as you have to memorize the signature of the pipeline methods that you override and also remember to call ``super()``.
@@ -664,7 +703,7 @@ Here is the final example code that combines all the tests discussed before:
664
703
This test abstracts away the common functionality found in almost all of our tutorial tests (executable options, sanity checking, etc.) to a base class, from which all the concrete regression tests derive.
665
704
Each test then redefines only the parts that are specific to it.
666
705
Notice also that only the actual tests, i.e., the derived classes, are made visible to the framework through the ``@simple_test`` decorator.
667
-
Decorating the base class has now meaning, because it does not correspond to an actual test.
706
+
Decorating the base class has no meaning, because it does not correspond to an actual test.
668
707
669
708
The total line count of this refactored example is less than half of that of the individual tutorial tests.
670
709
Another interesting thing to note here is the base class accepting additional additional parameters to its constructor, so that the concrete subclasses can initialize it based on their needs.
0 commit comments