pytest之常用插件

pytest常用的插件有哪些

pip install pytest-ordering  控制用例的执行顺序(重点)
pip install pytest-xdist    分布式并发执行测试用例(重点)
pip install pytest-dependency   控制用例的依赖关系 (了解)
pip install pytest-rerunfailures   失败重跑(了解)
pip install pytest-assume          多重较验(了解)
pip install pytest-random-order  用例随机执行(了解)
pip install pytest-html            测试报告(了解)

pytest-ordering 用法

不加排序


"""
 pytest 调整测试用例执行顺序
"""


def test_01():
    print("test 01")


def test_02():
    print("test 02")


def test_03():
    print("test 03")


def test_04():
    print("test 04")

运行上面的代码:

E:\Home_Work\Home_Work2\pytest01\test>pytest test_oder.py
collected 4 items                                                                                                                    

test_oder.py::test_01 test 01
PASSED
test_oder.py::test_02 test 02
PASSED
test_oder.py::test_03 test 03
PASSED
test_oder.py::test_04 test 04
PASSED

========================================================= 4 passed in 0.54s =========================================================

E:\Home_Work\Home_Work2\pytest01\test>

加排序

"""
 pytest 调整测试用例执行顺序
"""
import pytest


@pytest.mark.run(order=4)
def test_01():
    print("test 01")


@pytest.mark.run(order=2)
def test_02():
    print("test 02")


@pytest.mark.run(order=3)
def test_03():
    print("test 03")


@pytest.mark.run(order=1)
def test_04():
    print("test 04")

运行代码:

E:\Home_Work\Home_Work2\pytest01\test>pytest test_oder.py
collected 4 items                                                                                                                                                                                                                                 

test_oder.py::test_04 test 04
PASSED
test_oder.py::test_02 test 02
PASSED
test_oder.py::test_03 test 03
PASSED
test_oder.py::test_01 test 01
PASSED

========================================================= 4 passed in 0.61s =========================================================

E:\Home_Work\Home_Work2\pytest01\test>

上面代码根据oder排序进行运行的,如果不加排序就根据由上至下的顺序执行。

pytest-xdist 用法

PYTEST 多进程并行与分布式执行 (PS:分布式采用的是多进程
pytest-xdist仅支持多进程,不支持多线程。
场景 1:
测试用例 1000 条,一个用例执行 1 钟,一个测试人员执行需要 1000 分 钟。通常我们会用人力成本换取时间成本,加几个人一起执行,时间就会 缩短。如果 10 人一起执行只需要 100 分钟,这就是一种布式场景。

场景 2:
假设有个报名系统,对报名总数统计,数据同时进行修改操作的时候有可能出现问题,需要模拟这个场景,需要多用户并发请求数据。

解决:
使用分布式并发执行测试用例。分布式插件:pytest-xdist

安装及运行: pip install pytest-xdist
注意: 用例多的时候效果明显,多进程并发执行,同时支持 allure

用例运行条件:

1.用例之间都是独立的(不存在依赖关系);
2.用例执行无先后顺序要求;

常用参数解析:

-n:进程数,也就是cpu个数。可以指定个数,最大值为当前机器cpu个数,也可以设置为auto,自动识别cpu个数。


"""
 pytest 分布式执行测试用例
"""
import time

import pytest


@pytest.mark.run(order=4)
def test_01():
    time.sleep(1)
    print("test 01")


@pytest.mark.run(order=2)
def test_02():
    time.sleep(1)
    print("test 02")


@pytest.mark.run(order=3)
def test_03():
    time.sleep(1)
    print("test 03")


@pytest.mark.run(order=1)
def test_04():
    time.sleep(1)
    print("test 04")

运行结果如下:

gw0 - gw7 代表了8个进程

E:\Home_Work\Home_Work2\pytest01\test>pytest -n 8 test_oder.py
testconfig-0.2.0, xdist-2.3.0
[gw0] win32 Python 3.8.7 cwd: E:\Home_Work\Home_Work2\pytest01\test
[gw1] win32 Python 3.8.7 cwd: E:\Home_Work\Home_Work2\pytest01\test
[gw2] win32 Python 3.8.7 cwd: E:\Home_Work\Home_Work2\pytest01\test
[gw3] win32 Python 3.8.7 cwd: E:\Home_Work\Home_Work2\pytest01\test
[gw4] win32 Python 3.8.7 cwd: E:\Home_Work\Home_Work2\pytest01\test
[gw5] win32 Python 3.8.7 cwd: E:\Home_Work\Home_Work2\pytest01\test
[gw6] win32 Python 3.8.7 cwd: E:\Home_Work\Home_Work2\pytest01\test
[gw7] win32 Python 3.8.7 cwd: E:\Home_Work\Home_Work2\pytest01\test
[gw0] Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)]
[gw1] Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)]
[gw2] Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)]
[gw3] Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)]
[gw4] Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)]
[gw5] Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)]
[gw6] Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)]
[gw7] Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)]
gw0 [4] / gw1 [4] / gw2 [4] / gw3 [4] / gw4 [4] / gw5 [4] / gw6 [4] / gw7 [4]
scheduling tests via LoadScheduling

test_oder.py::test_02
test_oder.py::test_04
test_oder.py::test_03
test_oder.py::test_01
[gw1] PASSED test_oder.py::test_02
[gw3] PASSED test_oder.py::test_01
[gw0] PASSED test_oder.py::test_04
[gw2] PASSED test_oder.py::test_03

======================================================== 4 passed in 14.16s =========================================================

E:\Home_Work\Home_Work2\pytest01\test>pytest -n 8 test_oder.py

自动分配:


E:\Home_Work\Home_Work2\pytest01\test>pytest -n auto test_oder.py
testconfig-0.2.0, xdist-2.3.0
[gw0] win32 Python 3.8.7 cwd: E:\Home_Work\Home_Work2\pytest01\test
[gw1] win32 Python 3.8.7 cwd: E:\Home_Work\Home_Work2\pytest01\test
[gw2] win32 Python 3.8.7 cwd: E:\Home_Work\Home_Work2\pytest01\test
[gw3] win32 Python 3.8.7 cwd: E:\Home_Work\Home_Work2\pytest01\test
[gw0] Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)]
[gw1] Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)]
[gw2] Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)]
[gw3] Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)]
gw0 [4] / gw1 [4] / gw2 [4] / gw3 [4]
scheduling tests via LoadScheduling

test_oder.py::test_02
test_oder.py::test_03
test_oder.py::test_01
test_oder.py::test_04
[gw3] PASSED test_oder.py::test_01
[gw0] PASSED test_oder.py::test_04
[gw2] PASSED test_oder.py::test_03
[gw1] PASSED test_oder.py::test_02

========================================================= 4 passed in 8.37s =========================================================

E:\Home_Work\Home_Work2\pytest01\test>

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值