1 pytest automated test - My point of view on test case timeout processing
1.1 Background
During the execution of the use case, the network may be waiting for some special files, and due to some exceptions, these conditions cannot be met and the use case is stuck. This situation is not allowed in automated testing and will waste a lot of waste. The time of the test task affects the progress of the test task and even leads to the extension of the release cycle of the version.
In order to set a timeout time for each use case, there are several methods:
1.2 Method 1: Add to each use case@
Modifier
@It does not work well under Windows. If 1 use case timed out, the entire test task will be interrupted, which is very nonsense!
1.3 Method 2: Use the hook function to add a timeout modifier to each use case
However,The following method is of no use in Windows!, the use case that timed out was also successfully executed!
Execution results under Windows:
Other so-called usetimeout
The timeout process of markers is of no use under Windows!
1.4 Method 3: Introduce a method that uses threads to handle use cases timeouts
The timeout of the use case is actually mainly to monitor the timeout of each task, and by creating a basic functionbase_func
Each time this function is called, it creates a thread and sets a timeout time for the thread. The execution statements in the use case are passed through this thread functionbase_func
To execute,base_func
The timeout task will fail.
Output result:
Both use cases were executed, and the second use case did not abort the execution because the first use case timed out!
Use basic functions to wrap the need to test the execution statement, because when actually testing is executed, each statement may execute timeout, encapsulating execution can ensure the rigor of the test script.
Author statement: This article is used to record and share the author's learning experience. Some texts or examples may come from AI platforms, such as: Doubao,DeepSeek (Silicon-based flow)(Register link)etc. Due to my limited level, there are inevitably mistakes in expression. Welcome to leave a message and communicate and give advice!
Copyright © 2022~2025 All rights reserved.