pytest some useful plugins practice

1. Multiple check pytest-assume

Simple checksum assert, although you can write multiple assert

    def test_add1(self):
        assert add(2,3)==5 assert add(1,3)==3 assert add(2,5)==7 

Since the second assertion fails, then the following assertion will not be executed.
So if you need more assertions, we need to perform a third-party plug-ins pytest-assume
the installation command:

pip install pytest-assume

Example:

    def test_add2(self):
        pytest.assume(add(1,2)==3) pytest.assume(add(1,4)==3) pytest.assume(add(2,2)==4) 

Even here, the second assertion fails, the third assertion will still continue.

 

2. Set the execution order pytest-ordering

For some context-dependent, and sometimes may need to set some specific order of execution, ordering plug pytest, it is a good solution to this problem
install command

pip  install  pytest-ordering

Sample script as follows:

def test_order1(): print ("first test") assert True def test_order2(): print ("second test") assert True 

Without adding ordering, order of execution test_order1 ---- test_order2, as shown:

 

 
before.png

After the order of a

@pytest.mark.run(order=2) def test_order1(): print ("first test") assert True @pytest.mark.run(order=1) def test_order2(): print ("second test") assert True 

The results are as follows:

 

 
after.png

5. Failure to run heavy pytest-rerunfailures

Failure by patients with severe run, personally feel that this is a very useful plug-ins.
For example:
When doing UI automation, such as selenuim or appium, encountered some elements failed to show, resulting in failure clicks, if combined with heavy run, it will improve the accuracy of the report.
Installation command:

pip install pytest-rerunfailures 

Sample script

class TestRerun(): @pytest.mark.run(order=2) @pytest.mark.flaky(reruns=5) def test_random(self): print(1) pytest.assume((random.randint(0,4)+3)==5) @pytest.mark.run(order=3) def test_random2(self): ''' 不加mark 命令行中 pytest -sq demo_rerun.py --reruns 5 :return: ''' print(2) pytest.assume((random.randint(0,4)+3)==5) @pytest.mark.run(order=1) @pytest.mark.flaky(reruns=6, reruns_delay=2) def test_example(self): print(3) assert random.choice([True, False])

Excuting an order:

pytest -sq demo_rerun.py --reruns 5
pytest -sq demo_rerun.py --reruns 5  --reruns-delay 1

The difference is, the following before each re-run will wait for 1 s
at the same time you can also specify the number of re-define run in the script, this time at run time, you do not need to add this parameter --reruns

@pytest.mark.flaky(reruns=6, reruns_delay=2) def test_example(self): print(3) assert random.choice([True, False]) 
 

Contrast can be seen, the execution order is changed

Guess you like

Origin www.cnblogs.com/peng-lan/p/11511569.html