When To Use Pytest Timeout
Perhaps you’re wondering when it is a good idea to use Pytest timeout or timeouts?
Calls to External Resources
External resources may include APIs (vendor, internal), databases or the consumption of events from an event broker (Kafka topic) etc.
Sequential Test Runs
Some examples may include setting the authentication/authorization session cookie before you test a specific feature.
Networking or Connectivity Issues
Or likely somebody in your platform team redeployed a cluster to a new VPC and set the networking or security groups incorrectly and your application cannot connect to a resource.
Code Looping Error
This ensures that resources never run for longer than necessary and any execution over this timeframe would gracefully exit.
Database Connections
While most database API libraries and packages handle the opening/closing of connections, some legacy databases may not directly support this.
time.sleep(delay) # Change to test delays
Note - In order to install the pytest timeout plugin please ensure to run
Set Pytest Timeout via CLI
Assuming you’ve installed the plugin, you can easily set the timeout using the CLI command --timeout=X
where X
is the desired timeout in seconds.
pytest tests/unit/test_factorial.py —timeout=5
Another simple and seamless option way is to set the pytest timeout within the pytest.ini
file.
[pytest]
timeout = 5
log_cli=true
log_level=INFO
A simple and more controlled way is to set the pytest timeout is via the @pytest.mark.timeout()
decorator.
Set Pytest Timeout via Global Timeout
pytestmark = pytest.mark.timeout(3)
Short Note — Timeout Methods
Pytest timeout uses 2 methods to decide how to timeout — Signal and Thread.
Signal Method
This method schedules an alarm when the test item starts and cancels the alarm when the test finishes. If the alarm expires during the test the signal handler will dump the stack of any other threads running to stderr and use pytest.fail() to interrupt the test.
Thread Method
According to the official docs
For each test item the pytest-timeout plugin starts a timer thread which will terminate the whole process after the specified timeout. When a test item finishes this timer thread is cancelled and the test run continues.
The downsides of this method are that there is a relatively large overhead for running each test and that test runs are not completed. The benefit of this method is that the pytest process is not terminated and the test run can complete normally.