In the process of functional testing, we often encounter some occasional bugs, which need to be reproduced through repeated execution of use cases. Then, in the process of automated testing, some accidental bugs can also be repeated for a single use case or for a module.
There are three main ways to execute test cases repeatedly:
① Repeat running single or all test cases.
(2) after the failure, rerun the test case.
③ The test case runs repeatedly until it fails for the first time.
In order to execute test cases repeatedly, you need to install the pytest-repeat plug-in first.
Global installation: It is installed in the global environment. When the newly created project is imported into the global environment, the package will be imported. Cmd input: pip install pytest -repeat, and the repeated version number will be displayed after successful installation.
Partial installation: that is, it is installed in the current project environment, and the newly created project will not synchronize the package. In py charm→ file → settings, search for pytest intrepreter and click the+sign, as shown in the following figure:
Search pytest-repeat again, and click the installation package to install it.
The following figure shows that the installation was successful:
There are many ways to repeatedly execute test cases, such as: method 1 repeatedly execute a single test case; Method 2 repeats each test case; Method 3 is executed once for each test case, and * * * is executed n times.
Add a comment @ pytest.mark.repeat (value) before the test case, where value represents the number of repetitions to realize the repeated execution of a single use case.
As a result, the modified test case test_one will be executed twice in a row, and then test_two will be executed, that is, the first test case will be executed n times in sequence, and then the next test case will be executed.
Pass in the count at the terminal to realize the repeated execution of test cases.
The running results are as follows: test case test_one will be executed twice in a row, and then test_two will be executed twice, that is, each test case will be executed n times in the execution order.
Note: -s: indicates the mode information in the output use case, such as printing information.
-repeat-scope is similar to the scope parameter of pytest fixture. At the terminal input, -repeat-scope has four parameters:
The replay mechanism uses the plug-in of pytest, and the plug-in is called rerunfailures. To use it, you need to install this plug-in first.
The following figure shows that the installation was successful:
The running results are as follows:
note:
The running results are as follows:
By combining the -x option of pytest with pytest-repeat, you can stop running the test case when it fails for the first time in the process of repeatedly running the test case. The specific implementation method is as follows:
The running results are as follows: