Timing out
Timing Out Tests
You can set a timeout for a test by specifying the timeouts parameter explicitly for inline tests or using the Timeouts or Timeout decorator for decorated tests.
Tests can have one or more Timeouts and inherit any timeouts from the parent test.
If started is not set, then the current test’s start time is used.
Timeouts are cumulative, and any explicitly set timeout can’t be overwritten. Timeouts are defined using a list of the Timeout objects.
Timeouts can also be set externally using the xargs parameter that sets the timeouts parameter. See the xargs for more details. Given that timeouts are cumulative, make sure that the pattern used in the xargs is unique and does not match the parent and its child tests at the same time, otherwise the list of timeouts will contain duplicate entries.
The test timeout is evaluated at the start of the test. If the timeout value is reached, a Fail result is raised, and the test body is not executed. Given that child tests inherit timeouts from the parent, the timeout is propagated down the test Test Program Tree.
✋ Given that the test timeouts are evaluated only at the start of the test, it means that timeouts can be exceeded in any code that does not include sub-tests or steps.
Here is an example of a test with a timeout that uses a step inside
the for-loop. The timeout will be hit during execution of the When step
as it will inherit the timeout from its parent.
1 | with Test("my test", timeouts=[Timeout(10)]): |
Here is an example of the test where the timeout will not be hit as there are no sub-tests or steps inside the for-loop.
1 | with Test("my test", timeouts=[Timeout(10)]): |
Timing and Timing Out Code
You can place stopwatches and timeouts in your test code using a Timer class object. The object should be used as the context manager that wraps the code that needs to be timed or checked for a timeout before it gets executed.
✋ The Timer object’s timeout is only evaluated during the start of the code wrapped with the timer context manager.
Using timer()
The timer is an alias for the Timer class object. See Using Timer.
Using Timer()
The Timer class object can be used to check for a timeout or as a stopwatch. Once created, it can be used as a context manager.
1 | Timer(timeout=None, message=None, started=None) |
where
timeouttimeout in sec, default:Non3messagecustom timeout error message, default:Nonestartedstart time, default:None(set to current time)
The object has an elapsed attribute to get the current elapsed time,
which is the difference between the current and the started time.
The Timer object can be used for checking for a timeout if the timeout parameter is set.
1 | timeout = Timer(10) |
The Timer class object can be re-used as the started time is fixed to the initial value.
1 | # started time is fixed here and is set to the current time by default |
Another approach is to keep the started time fixed explicitly as follows:
1 | started = time.time() |
The Timer object can also be used as a stopwatch if the timeout parameter is not specified.
1 | stopwatch = Timer() |