Decorators¶
Decorators can be used to change the behaviour of a method by decorating it.
-
htf.decorators.
after
(afterFunction, *afterArgs, **afterKwargs)¶ The @after decorator decorates a method so that afterFunction is called after the decorated function is called itself.
Parameters: Usage:
def runAfter(*args, **kwargs): print("runAfter", args, kwargs) @after(runAfter, 1, 2, 3, a="b") def function(): print("function") function() # prints: # function # runAfter (1, 2, 3) {'a': 'b'}
-
htf.decorators.
before
(beforeFunction, *beforeArgs, **beforeKwargs)¶ The @before decorator decorates a method so that beforeFunction is called before the decorated function is called itself.
Parameters: Usage:
def runBefore(*args, **kwargs): print("runBefore", args, kwargs) @before(runBefore, 1, 2, 3, a="b") def function(): print("function") function() # prints: # runBefore (1, 2, 3) {'a': 'b'} # function
-
htf.decorators.
enqueueException
(queue)¶ Decorate a method to fetch it’s exception that is enqueued into an exception queue. The enqueued exception can be handled elsewhere. The caught exception can be raised after enqueueing it.
Just decorate you method with the queue to be used and every exception that is raised within the decorated method will be put into the queue before the exception is raised again.
Parameters: queue (queue) – the queue to enqueue the exception. Usage:
@enqueueException(queue) def decoratedMethod(): raise Exception("This is a test")
-
htf.decorators.
meetsDOORSRequirements
(base_url, *requirements)¶ The
@meetsDOORSRequirements
decorator reports which requirements are fulfilled by a testcase in the test report shown in thedoors_testreport
and theHTMLTestReport
.Parameters: - base_url (str) – the DOORS base url the links are created with.
- *requirement (tuple of str) – a tuple of strings that name the fulfilled requirements, eg. “REQ_1”, “REQ_2”, etc.
Usage:
@meetsRequirements("doors://host:port/path-without-id-", "REQ_1", "REQ_2") def test_withAUsefulName(): pass
-
htf.decorators.
periodic
(period, maximumPeriod=None, runConditionLambda=None, raiseException=True)¶ Decorate a method to be run periodically.
Parameters: - period (float) – the period in seconds.
- maximumPeriod=None (float) – if set to a float >= period the periodic method may take up to maximumPeriod time without raising an exception. A warning is printed on stdout instead.
- runConditionLambda=None (callable) – a callable (method or lambda expression) that has to return true while the method is run periodically.
- raiseException=True (bool) – if set to
True
an exception is raised if the called method takes longer than the period (ormaximumPeriod
if set).
Warning
Non-realtime operating system will not ensure timing accuracy.
Usage:
@periodic(period=1.0) def periodicallyCalledEverySecond(): print("call")
@periodic
can also be used within classes:class ClassWithAPeriodicMethod(): @periodic(period=1.0) def periodicallyCalledEverySecond(self): print("call")
-
htf.decorators.
raises
(*exceptions)¶ Decorate a method to catch different exceptions.
Parameters: *exceptions (list of exceptions) – a tuple of exceptions that are fetched. Usage:
@raises(AssertionError) def test_failure(self): self.assertTrue(False) @raises(TimeoutException, UnknownException) def test_exceptions(self): raise TimeoutException("timeout") raise UnknownException("unknown exception")
-
htf.decorators.
skip
(reason)¶ Decorate a method to be skipped in view of the occasion.
Parameters: reason (str) – the reason to be put into the test report. Raises: SkipTest
– to skip the test.Usage:
@skip("This test is skipped in view of the occasion") def test_alwaysSkippedLambda(): pass
-
htf.decorators.
skipIf
(condition, reason)¶ Decorate a method to be skipped. The
condition
is evaluated at runtime.Parameters: Raises: SkipTest
– to skip the test ifcondition
is or returnsTrue
.Usage:
def skipTests(): return True @skipIf(skipTests, "Skipped if skipTests returns True") def test_skippedIf(): pass @skipIf(True, "Always skipped") def test_alwaysSkipped(): pass @skipIf(False, "Never skipped") def test_neverSkipped(): pass @skipIf(lambda: True, "Always skipped") def test_alwaysSkippedLambda(): pass
-
htf.decorators.
stacktrace
(func)¶ The
@stacktrace
decorator decorates a method so that its call stack is printed to stdout every time it is called. This is useful for debugging.Usage:
@stacktrace def function(): pass function() # will print(the call stack to stdout)