其他分享
首页 > 其他分享> > 08-pytest常用插件

08-pytest常用插件

作者:互联网

常用插件

pytest-rerunfailures

重新运行所有失败的用例

要重新运行所有测试失败,使用 --reruns 命令行选项,并指定要运行测试的最大次数:
pytest -vs --reruns 3 test_rerun.py

from time import sleep


def test_rerun1():
    sleep(0.5)
    assert 1 == 2

def test_rerun2():
    sleep(0.5)
    assert 2 == 2

def test_rerun2():
    sleep(0.5)
    assert 'h' == 'world'

执行结果:

test_rerun.py::test_rerun1 RERUN
test_rerun.py::test_rerun1 RERUN
test_rerun.py::test_rerun1 RERUN
test_rerun.py::test_rerun1 FAILED
test_rerun.py::test_rerun2 RERUN
test_rerun.py::test_rerun2 RERUN
test_rerun.py::test_rerun2 RERUN
test_rerun.py::test_rerun2 FAILED

添加重新运行的延时

要在两次重试之间增加延迟时间,使用 --reruns-delay 命令行选项,指定下次测试重新开始之前等待的秒数
pytest test_rerun.py --reruns 3 --reruns-delay 1
说明:明显看到重跑的两个测试用例之间有一段时间间隔

重新运行指定的测试用例

@pytest.mark.flaky(reruns=2,reruns_delay=2)
运行结果:

test_rerun.py::test_rerun1 FAILED
test_rerun.py::test_rerun2 PASSED
test_rerun.py::test_rerun3 RERUN
test_rerun.py::test_rerun3 RERUN
test_rerun.py::test_rerun3 FAILED

兼容性问题

pytest-assume

assert 多重断言

def test_assume():
    assert 1 == 2
    assert False == True
    assert 100 == 200

运行结果:

FAILED
test_assume.py:8 (test_assume)
1 != 2

Expected :2
Actual   :1
<Click to see difference>

def test_assume():
>       assert 1 == 2
E       assert 1 == 2

test_assume.py:10: AssertionError

结论
可以看到,第一行断言失败之后,后面的断言也不会执行,包括正常的代码

pytest.assume多重断言

def test_assume():
    # assert 1 == 2
    # assert False == True
    # assert 100 == 200
    pytest.assume(1 == 1)
    pytest.assume(False == True)
    pytest.assume(100 == 200)
    pytest.assume(4 != 1)

运行结果:

FAILED
test_assume.py:10 (test_assume)
tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = None

    def reraise(tp, value, tb=None):
        try:
            if value is None:
                value = tp()
            if value.__traceback__ is not tb:
>               raise value.with_traceback(tb)
E               pytest_assume.plugin.FailedAssumption: 
E               2 Failed Assumptions:
E               
E               test_assume.py:16: AssumptionFailure
E               >>	pytest.assume(False == True)
E               AssertionError: assert False
E               
E               test_assume.py:17: AssumptionFailure
E               >>	pytest.assume(100 == 200)
E               AssertionError: assert False

结论

pytest-ordering

pytest中测试用例执行顺序是从上往下执行

import pytest

def test_1():
    assert True

def test_2():
    assert 1 == 1

def test_2():
    assert 2 == 2

运行结果:

pytest-ordering控制执行顺序

import pytest


@pytest.mark.run(order=3)
def test_1():
    assert True

@pytest.mark.run(order=1)
def test_2():
    assert 1 == 1

@pytest.mark.run(order=2)
def test_3():
    assert 2 == 2

执行结果:

总结:
1、除了用@pytest.mark.run(order=3)这种方式指定测试用例顺序
2、还可以使用pytest.mark.third 方法指定

pytest-repeat

重复测试直到失败(重要!!!)

- 如果需要验证偶现问题,可以一次又一次地运行相同的测试直到失败,这个插件将很有用
- 可以将pytest的 -x 选项与pytest-repeat结合使用,以强制测试运行程序在第一次失败时停止

pytest --count=1000 -x test_file.py

代码如下:

def test_example():
    import random
    flag = random.choice([True, False])
    print(flag)
    sleep(1)
    assert flag

执行命令:
pytest --count 5 -x test_repeat.py

执行结果:

test_repeat.py::test_example[1-5] True
PASSED
test_repeat.py::test_example[2-5] True
PASSED
test_repeat.py::test_example[3-5] False
FAILED

================================================================= FAILURES =================================================================
____________________________________________________________ test_example[3-5] _____________________________________________________________

    def test_example():
        import random
        flag = random.choice([True, False])
        print(flag)
        sleep(1)
>       assert flag
E       assert False

test_repeat.py:17: AssertionError
========================================================= short test summary info ==========================================================
FAILED test_repeat.py::test_example[3-5] - assert False
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================================= 1 failed, 2 passed in 3.10s ========================================================

总结:执行了三次,第三次失败,就不再执行了。

@pytest.mark.repeat(count)

如果要在代码中将某些测试用例标记为执行重复多次,可以使用 @pytest.mark.repeat(count)

@pytest.mark.repeat(5)
def test_repeat():
    sleep(1)
    print("测试用例执行")

执行命令:
pytest test_repeat.py::test_repeat

执行结果:

test_repeat.py::test_repeat[1-5] 测试用例执行
PASSED
test_repeat.py::test_repeat[2-5] 测试用例执行
PASSED
test_repeat.py::test_repeat[3-5] 测试用例执行
PASSED
test_repeat.py::test_repeat[4-5] 测试用例执行
PASSED
test_repeat.py::test_repeat[5-5] 测试用例执行
PASSED

============================================================ 5 passed in 5.05s =============================================================

--repeat-scope

命令行参数

作用:可以覆盖默认的测试用例执行顺序,类似fixture的scope参数

- function:默认,范围针对每个用例重复执行,再执行下一个用例
- class:以class为用例集合单位,重复执行class里面的用例,再执行下一个
- module:以模块为单位,重复执行模块里面的用例,再执行下一个
- session:重复整个测试会话,即所有测试用例的执行一次,然后再执行第二次

class示例

class TestRepeat:

    def test_repeat1(self):
        print("test1++++++++test1")

    def test_repeat2(self):
        print("test2+++++++++test2")


class TestRepeat2:

    def test_repeat3(self):
        print("test3++++++++test3")

    def test_repeat4(self):
        print("test4+++++++++test4")

执行命令: pytest --count=2 --repeat-scope=class test_repeat.py
执行结果:

test_repeat.py::TestRepeat::test_repeat1[1-2] test1++++++++test1
PASSED
test_repeat.py::TestRepeat::test_repeat2[1-2] test2+++++++++test2
PASSED
test_repeat.py::TestRepeat::test_repeat1[2-2] test1++++++++test1
PASSED
test_repeat.py::TestRepeat::test_repeat2[2-2] test2+++++++++test2
PASSED
test_repeat.py::TestRepeat2::test_repeat3[1-2] test3++++++++test3
PASSED
test_repeat.py::TestRepeat2::test_repeat4[1-2] test4+++++++++test4
PASSED
test_repeat.py::TestRepeat2::test_repeat3[2-2] test3++++++++test3
PASSED
test_repeat.py::TestRepeat2::test_repeat4[2-2] test4+++++++++test4
PASSED

============================================================ 8 passed in 0.04s =============================================================

兼容性问题

其他

后续单独讲解pytest-xdis(重要!!!)插件的使用。

标签:插件,repeat,assume,08,py,assert,pytest,test
来源: https://www.cnblogs.com/murcy/p/14984439.html