Launching TestFlows Machine: self-service deterministic simulation testing environment Read the post

Timeouts and conditions

Test Timeouts

You can set test timeouts using the timeouts parameter.

Timeouts

The Timeouts object can be either used as the value of the timeouts parameter or as a decorator that can be applied to any decorated test.

The Timeouts takes as an argument a list of objects that specify a timeout, either using the Timeout object directly or as a tuple of arguments that will be passed to it.

1
2
3
4
5
6
[
Timeout(...),
# tuple to be converted to a Timeout object
(timeout, message, started, name),
...
]

The Timeouts decorator should be used when you want to specify more than one timeout.

✋ Even though more than one timeout can be specified, only the value with the smallest value will be the effective timeout.

1
2
3
4
@TestScenario
@Timeouts([Timeout(10), Timeout(20)])
def my_test(self):
pass

For a single timeout, the Timeout decorator can be used instead.

Timeout

The Timeout object can be either used as one of the values in the list passed to the timeouts parameter or as a decorator that can be applied to any decorated test.

1
Timeout(timeout, message=None, started=None, name=None)

where

  • timeout timeout in sec
  • message custom timeout error message, default: None
  • started start time, default: None (set to the current test’s start time)
  • name name, default: None

For example,

1
2
3
4
with Test("my test", timeouts=[Timeout(10)]):
for i in range(12):
with When(f"I do something #{i}"):
time.sleep(1)

or it can be used as a decorator as follows:

1
2
3
4
@TestScenario
@Timeout(10)
def my_test(self):
pass

The when Condition

Some parameters support a when condition, specified as a function, as the last element. If present, the when function is called before test execution. The boolean result returned by the when function determines if the forced result is applied, if the function returns True, or not, if it returns False. The when function must take one argument, which is the instance of the test.

✋ The optional when function can define any logic that is needed to determine if some condition is met. Any callable that takes a current test object as the first and only argument that can be used.

Here is an example of using ffails with a when condition:

1
2
3
4
5
6
7
8
9
10
11
def version(*versions):
"""Check if the value of version test context variable
matches any in the list.
"""
def _when(test):
return test.context.version in versions
return _when

with Module("regression"):
# force to skip my_suite test only on version 2.0
Suite(run=my_suite, ffails={"my test": (Skip, "not supported", version("2.0"))})

Specialized keywords

when writing your test scenarios, the framework encourages the usage of specialized keywords because they can provide the much-needed context for your steps.

The specialized keywords map to core Step, Test, Suite, and Module test definition classes as follows:

  • Module is defined as a Module
  • Suite is defined as a Feature
  • Test is defined as a Scenario
  • Step is defined as one of the following:
    • Given is used define a step for precondition or setup
    • Background is used define a step for a complex precondition or setup
    • When is used to define a step for an action
    • And is used as a continuation of the previous step
    • By is used to define a sub-step
    • Then is used to define a step for positive assertion
    • But is used to define a step for negative assertion
    • Finally is used to define a cleanup step