There is a delay in updating articles on this site, if you want to see articles about Python + Appium, please follow me over to Testhome. Testerhome.com/topics/2780…

From APP Android end automation test beginner’s notes, write wrong place we a lot of advice oh.

In the process of functional testing, we often encounter some accidental bugs and need to repeat the use cases to reproduce the problems. Therefore, in the process of automated testing, for some accidental bugs, we can also repeat the use cases for a single case or a module.

The method of repeated execution of test cases mainly summarizes three aspects:

① Run one or all test cases repeatedly

② Run the test case again after the test case fails

③ Run the test case repeatedly until it stops after the first failure

Repeat test cases

(a) Installation environment

To repeat the test case, you need to install the Pytest-Repeat plug-in

CMD: PIP install pytest-repeat. If the installation is successful, the version number of repeat will be displayed

** Partial installation: ** is installed in the current project environment, the newly created project will not synchronize the package, in PyCharm→File→setting, search PyTest Intrepreter, click “+”, as shown below:

Search pytest-repeat again and click Install Package to install

The following figure indicates that the installation is successful:

(2) Repeated execution of test cases

There are many ways to repeat the execution of a test case. For example, method 1 repeats the execution of a single test case. Method 2 repeated execution of each test case; In method 3, each test case is executed once for N times.

Method 1: Use annotations to repeatedly execute a single use case

Add an annotation @pytest.mark.repeat(value) before the test case, where value indicates the number of repetitions, to implement repeated execution of a single use case.

import pytest

class Test_Pytest:

    @pytest.mark.repeat(2)
    def test_one(self) :
        print("Test_one method execution")
		
    def test_two(self) :
	print("Test_two method execution")
Copy the code

The result is: the decorated test case test_one is executed twice in a row, followed by test_two, that is, the first test case is executed N times, followed by the next test case

Method 2: Execute all use cases repeatedly using command function arguments

Repeat the test case execution by passing -count to the terminal

import pytest

class Test_Pytest:

    def test_one(self) :
        print("Test_one method execution")
		
    def test_two(self) :
	print("Test_two method execution")

# Enter:
pytest -s -v --count=2 test_Pytest.py
Copy the code

The result is that the test case test_one is executed twice in a row and then test_two is executed twice, that is, each test case is executed N times in the order in which it is executed

Note: -s: indicates the mode information in the output use case, such as the print information of print, etc.

-v: displays detailed execution information about the use case, such as the file where the use case resides and the name of the use case.Copy the code

Method 3: execute all test cases once, repeat N times (I run the script using the following four parameters after the result is the same as the result of method 2 -count, but does not affect the operation of other functions, so here first make a record, later use this knowledge to solve. Have which big god, can help solve, thank!!

-repeat-scope is similar to the scope argument for a Pytest fixture, entered at the terminal, and has four arguments:

  • Function: the default value to repeat the use case N times before executing the next use case, as in method 1
  • Class: Each test case in a class is executed once, N times, and then the next test class is executed
  • Module: Executes each test case in a module once, N times, and then executes the next module
  • Session: Repeats the entire test session and all test cases in the session for N rounds

2. The test case fails to be executed and run again

(a) Installation environment

The rerun mechanism uses the PyTest plug-in called Rerunfailures, which you need to install to use it

PIP install pytest-rerunfailuresCopy the code

The following figure indicates that the installation is successful:

(II) Re-run after failure

Method 1: Implement failed rerun in the form of annotations

import pytest

class TestFailure:
    Run the use case twice after it fails, with an interval of 10 seconds
    @pytest.mark.flaky(reruns=2, reruns_delay=10)
    def test_one(self) :
        a = 1 + 2
        assert 1 == a

    def test_two(self) :
	a = 1 + 2
	assert 3 == a
Copy the code

The running results are as follows:

Note:

  • You need to search PyTest Intrepreter in PyCharm→File→setting to install Pytest-rerunfailures **
  • To use decorators, pyTest must be imported first
  • Reruns indicates the number of reruns, reruns_delay indicates the delay between reruns, in seconds

Method 2: Use command line parameters to retry a failure

Run the use case twice after it fails, with an interval of 10 seconds
import pytest

class TestFailure:
    def test_one(self) :
        a = 1 + 2
        assert 1 == a

    def test_two(self) :
	a = 1 + 2
	assert 3 == a

# Enter:
pytest -s -v --reruns=2 --reruns-delay=10 test.py
Copy the code

The running results are as follows:

Repeat the test case until the failure stops

Using pyTest’s -x option in conjunction with Pytest-repeat, you can stop the test case on the first failure of the repeated test case as follows:

# Repeat 5 times, stop at the first failure
import pytest

class TestFailure:
    def test_one(self) :
        a = 1 + 2
        assert 1 == a

    def test_two(self) :
	a = 1 + 2
	assert 3 == a

# Enter:
pytest -s -v --count=5 -x test.py
Copy the code

The running results are as follows: