Decorators

Decorators can be used to change the behaviour of a method by decorating it.

htf.after(afterFunction, *afterArgs, **afterKwargs)

The @after decorator decorates a method so that afterFunction is called after the decorated function is called itself.

Parameters:
  • afterFunction (callable) – the function to be run after the decorated function.
  • *afterArgs (tuple) – positional arguments to be passed to afterFunction.
  • **afterKwargs (dict) – keyword-arguments to be passed to afterFunction.

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.before(beforeFunction, *beforeArgs, **beforeKwargs)

The @before decorator decorates a method so that beforeFunction is called before the decorated function is called itself.

Parameters:
  • beforeFunction (callable) – the function to be run before the decorated function.
  • *beforeArgs (tuple) – positional arguments to be passed to beforeFunction.
  • **beforeKwargs (dict) – keyword-arguments to be passed to beforeFunction.

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.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.meetsDOORSRequirements(base_url, *requirements)

The @meetsDOORSRequirements decorator reports which requirements are fulfilled by a testcase in the test report shown in the doors_testreport and the HTMLTestReport.

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:

@meetsDOORSRequirements("doors://host:port/path-without-id-",
                   "REQ_1", "REQ_2")
def test_withAUsefulName():
    pass
htf.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 (or maximumPeriod if set).

Warning

Non-realtime operating systems 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.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.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.skipIf(condition, reason)

Decorate a method to be skipped. The condition is evaluated at runtime.

Parameters:
  • condition (bool or callable) – the condition may be True, False or a callable object that must return True or False. The decorated test is skipped if condition is True or returns True.
  • reason (str) – the reason to be put into the test report.
Raises:

SkipTest – to skip the test if condition is or returns True.

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

def condition(self):
    return True

@skipIf(condition, "skipped")
def test_alwaysSkippedWithBoundMethod(self):
    pass
htf.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)
htf.tags(*tags)

Tag a test or class.

Parameters:*tags – the tags to be used
htf.timed(limit)

Decorate a method to ensure that it ends within a given time limit.

Parameters:limit (float) – the time limit in seconds.

Usage:

@timed(limit=0.1)
def decoratedMethodThatFailes():
    time.sleep(0.2)

@timed(limit=1.0)
def decoratedMethodThatDoesNotFail():
    time.sleep(0.2)