安顺住房和城乡建设部网站,折页彩页设计,网谱网络科技,网站模板源文件20-pytes如何进行分布式测试#xff1f;#xff08;pytest-xdist#xff09;1 什么是分布式测试#xff1f;2 为什么要进行分布式测试#xff1f;2.1 场景1#xff1a;自动化测试场景2.2 场景2#xff1a;性能测试场景3 分布式测试有什么特点#xff1f;4 分布式测试关…
20-pytes如何进行分布式测试pytest-xdist1 什么是分布式测试2 为什么要进行分布式测试2.1 场景1自动化测试场景2.2 场景2性能测试场景3 分布式测试有什么特点4 分布式测试关键技术是什么5 分布式执行用例的前置条件是什么6 pytest-xdist安装7 pytest-xdist的优势8 pytest-xdist的使用8.1 普通执行8.2 上述代码分布式执行8.3 指定CPU运行数量8.4 与pytest-html一起使用8.5 让pytest-xdist按照指定顺序执行8.6 pytest-xdist如何保持session执行一次1 什么是分布式测试
在进行本文之前先了解些基础知识什么是分布式测试分布式测试是指通过局域网和Internet把分布于不同地点、独立完成特定功能的测试计算机连接起来以达到测试资源共享、分散操作、集中管理、协同工作、负载均衡、测试过程监控等目的的计算机网络测试。通俗的讲分布式测试 就是活太多一个人干费时间那就让多个人一起干节省了资源和时间。
2 为什么要进行分布式测试
2.1 场景1自动化测试场景
自动化测试时我们有很多用例比如2000条用例按照顺序执行每条用例执行1分钟那需要2000分钟什么概念2000分钟就30多个小时如果是冒烟测试估计还没人工跑的快还有如果是线上发布跑完2000条用例就太浪费时间了那如果我们让我们让用例分布式执行是不是可以节省很多时间
2.2 场景2性能测试场景
如果数据量很大我们使用1台压测机可能并发压力过大那就需要选择使用多台压测机比如Jmeter的 Agent/负载机这样也是一种分布式压测或者分布式性能测试场景。 所以总结来说其实就是为了提升效率和质量。 3 分布式测试有什么特点
特点说明网格化多节点互联互通可资源共享分布性地域和计算机上协同工作、负载均衡、可扩展性、高可用性开放性可移植性、可互操作性、可伸缩性、易获得性实时性各种信息都必须是实时的动态性测试过程对象和活动动态映射处理不确定性具有处理不确定性的能力容错及安全性容错能力强可靠性高、安全性好
4 分布式测试关键技术是什么
技术点要求分布式环境获取全局状态能够方便地监视和操纵测试过程集中式的分布式策略。分布式环境下的节点通信稳定的通信环境适合用基于消息通信的方式来实现。测试任务调度静态调度、动态调度和混合调度。
5 分布式执行用例的前置条件是什么
用例之间是独立且没有依赖关系完全独立运行用例执行没有顺序随机顺序都能正常执行每个用例都能重复运行运行结果不会影响其他用例。
6 pytest-xdist安装
pytest-xdist让自动化测试用例分布式执行节省测试时间属于进程级别的并发使用以下方法安装
pip3 install pytest-xdistC:\Users\Administratorpip3 install pytest-xdist
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: pytest-xdist in d:\python37\lib\site-packages (1.31.0)
Requirement already satisfied: six in d:\python37\lib\site-packages (from pytest-xdist) (1.15.0)
Requirement already satisfied: execnet1.1 in d:\python37\lib\site-packages (from pytest-xdist) (1.8.0)
Requirement already satisfied: pytest4.4.0 in d:\python37\lib\site-packages (from pytest-xdist) (6.2.4)
Requirement already satisfied: pytest-forked in d:\python37\lib\site-packages (from pytest-xdist) (1.1.3)
Requirement already satisfied: apipkg1.4 in d:\python37\lib\site-packages (from execnet1.1-pytest-xdist) (1.5)
Requirement already satisfied: toml in d:\python37\lib\site-packages (from pytest4.4.0-pytest-xdist) (0.10.2)
Requirement already satisfied: attrs19.2.0 in d:\python37\lib\site-packages (from pytest4.4.0-pytest-xdist) (20.3.0)
Requirement already satisfied: colorama in d:\python37\lib\site-packages (from pytest4.4.0-pytest-xdist) (0.4.4)
Requirement already satisfied: atomicwrites1.0 in d:\python37\lib\site-packages (from pytest4.4.0-pytest-xdist) (1.4.0)
Requirement already satisfied: pluggy1.0.0a1,0.12 in d:\python37\lib\site-packages (from pytest4.4.0-pytest-xdist) (0.13.1)
Requirement already satisfied: py1.8.2 in d:\python37\lib\site-packages (from pytest4.4.0-pytest-xdist) (1.10.0)
Requirement already satisfied: importlib-metadata0.12 in d:\python37\lib\site-packages (from pytest4.4.0-pytest-xdist) (2.1.1)
Requirement already satisfied: packaging in d:\python37\lib\site-packages (from pytest4.4.0-pytest-xdist) (20.8)
Requirement already satisfied: iniconfig in d:\python37\lib\site-packages (from pytest4.4.0-pytest-xdist) (1.1.1)
Requirement already satisfied: zipp0.5 in d:\python37\lib\site-packages (from importlib-metadata0.12-pytest4.4.0-pytest-xdist) (1.2.0)
Requirement already satisfied: pyparsing2.0.2 in d:\python37\lib\site-packages (from packaging-pytest4.4.0-pytest-xdist) (2.4.7)7 pytest-xdist的优势
测试运行并行化在子进程中重复运行测试可指定不同的Python解释程序或不同的平台并行运行测试。
8 pytest-xdist的使用
8.1 普通执行
# -*- coding:utf-8 -*-
# 作者虫无涯
# 日期2023/3/16
# 文件名称test_xdist.py
# 作用pytest-xdist分布式测试
# 联系VX(NoamaNelson)
# 博客https://blog.csdn.net/NoamaNelsonimport pytest
import timeclass TestCase01():def test_case_01(self):time.sleep(1)print(case01$$$$$$$$$$$$$$$$$$$$$)def test_case_02(self):time.sleep(1)print(case02$$$$$$$$$$$$$$$$$$$$$)def test_case_03(self):time.sleep(1)print(case03$$$$$$$$$$$$$$$$$$$$$)def test_case_04(self):time.sleep(1)print(case04$$$$$$$$$$$$$$$$$$$$$)def test_case_05(self):time.sleep(1)print(case05$$$$$$$$$$$$$$$$$$$$$)def test_case_06(self):time.sleep(1)print(case06$$$$$$$$$$$$$$$$$$$$$)class TestCase02():def test_case_07(self):time.sleep(1)print(case07$$$$$$$$$$$$$$$$$$$$$)def test_case_08(self):time.sleep(1)print(case08$$$$$$$$$$$$$$$$$$$$$)def test_case_09(self):time.sleep(1)print(case08$$$$$$$$$$$$$$$$$$$$$)if __name__ __main__:pytest.main([-s, test_xdist.py])
执行结果如下使用了9.14s
test_xdist.py::TestCase01::test_case_01
test_xdist.py::TestCase01::test_case_02
test_xdist.py::TestCase01::test_case_03
test_xdist.py::TestCase01::test_case_04
test_xdist.py::TestCase01::test_case_05
test_xdist.py::TestCase01::test_case_06
test_xdist.py::TestCase02::test_case_07 PASSED [ 11%]case01$$$$$$$$$$$$$$$$$$$$$
PASSED [ 22%]case02$$$$$$$$$$$$$$$$$$$$$
PASSED [ 33%]case03$$$$$$$$$$$$$$$$$$$$$
PASSED [ 44%]case04$$$$$$$$$$$$$$$$$$$$$
PASSED [ 55%]case05$$$$$$$$$$$$$$$$$$$$$
PASSED [ 66%]case06$$$$$$$$$$$$$$$$$$$$$
PASSED [ 77%]case07$$$$$$$$$$$$$$$$$$$$$test_xdist.py::TestCase02::test_case_08 PASSED [ 88%]case08$$$$$$$$$$$$$$$$$$$$$test_xdist.py::TestCase02::test_case_09 PASSED [100%]case08$$$$$$$$$$$$$$$$$$$$$ 9 passed in 9.14s 8.2 上述代码分布式执行
执行命令
pytest -s -n auto test_xdist.py结果如下用时4.51s可见分布式执行后大大缩短了测试时间
(venv) F:\pytest_study\test_case\test_jpytest -s -n auto test_xdist.pytest session starts
platform win32 -- Python 3.7.0, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: F:\pytest_study, configfile: pytest.ini
plugins: allure-pytest-2.8.12, assume-2.4.3, cov-2.8.1, forked-1.1.3, html-2.0.1, metadata-1.8.0, ordering-0.6,repeat-0.9.1, rerunfailures-10.3, xdist-1.31.0
gw0 [9] / gw1 [9] / gw2 [9] / gw3 [9] / gw4 [9] / gw5 [9] / gw6 [9] / gw7 [9]
.........9 passed in 4.51s 8.3 指定CPU运行数量
-n auto可以自动检测到系统的CPU核数使用auto利用了所有CPU来跑用例也可以指定使用几个CPU来跑用例
# x为cpu个数
pytest -s -n x如下可以看到使用两个CPU来跑用例时长为6.27s
(venv) F:\pytest_study\test_case\test_jpytest -s -n 2 test_xdist.pytest session starts
platform win32 -- Python 3.7.0, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: F:\pytest_study, configfile: pytest.ini
plugins: allure-pytest-2.8.12, assume-2.4.3, cov-2.8.1, forked-1.1.3, html-2.0.1, metadata-1.8.0, ordering-0.6,repeat-0.9.1, rerunfailures-10.3, xdist-1.31.0
gw0 [9] / gw1 [9]
.........9 passed in 6.27s 8.4 与pytest-html一起使用
命令如下
pytest -s -n auto --htmlreport.html --self-contained-html运行结果
pytest -s -n auto test_xdist.py --htmlreport.thml --self-contained-htm
lgw0 [9] / gw1 [9] / gw2 [9] / gw3 [9] / gw4 [9] / gw5 [9] / gw6 [9] / gw7 [9]
.........
------------------ generated html file: file://F:\pytest_study\test_case\test_j\report.thml ------------------9 passed in 4.68s 8.5 让pytest-xdist按照指定顺序执行
pytest-xdist执行默认是无须的可通过 --dist 参数来控制顺序
参数说明--distloadscope 同一个模块module下的函数和同一个测试类class下的方法来分组--distloadfile 同一个文件名来分组
8.6 pytest-xdist如何保持session执行一次
pytest-xdist没有内置的支持来确保会话范围的夹具仅执行一次可使用FileLock方法仅仅产生一次fixture数据
import pytest
from filelock import FileLockpytest.fixture(scopesession)
def login():print(登录)with FileLock(session.lock):name zhangpassword 123456# web ui自动化# 声明一个driver再返回# 接口自动化# 发起一个登录请求将token返回都可以这样写yield name, passwordprint(退出)