当前位置: 首页 > news >正文

Pytest学习和使用-Pytest如何进行分布式测试?(pytest-xdist)

1 什么是分布式测试?

  • 在进行本文之前,先了解些基础知识,什么是分布式测试?

  • 分布式测试:是指通过局域网和Internet,把分布于不同地点、独立完成特定功能的测试计算机连接起来,以达到测试资源共享、分散操作、集中管理、协同工作、负载均衡、测试过程监控等目的的计算机网络测试。

  • 通俗的讲:分布式测试 就是活太多,一个人干费时间,那就让多个人一起干,节省了资源和时间。

2 为什么要进行分布式测试?

2.1 场景1:自动化测试场景
2.2 场景2:性能测试场景

所以总结来说,其实就是为了提升效率和质量。

3 分布式测试有什么特点?

特点说明
网格化多节点互联互通,可资源共享
分布性地域和计算机上,协同工作、负载均衡、可扩展性、高可用性
开放性可移植性、可互操作性、可伸缩性、易获得性
实时性各种信息都必须是实时的
动态性测试过程对象和活动动态映射
处理不确定性具有处理不确定性的能力
容错及安全性容错能力强,可靠性高、安全性好

4 分布式测试关键技术是什么?

技术点要求
分布式环境获取全局状态,能够方便地监视和操纵测试过程;集中式的分布式策略。
分布式环境下的节点通信稳定的通信环境;适合用基于消息通信的方式来实现。
测试任务调度静态调度、动态调度和混合调度。

5 分布式执行用例的前置条件是什么?

6 pytest-xdist安装

pip3 install pytest-xdist
  1. C:\Users\Administrator>pip3 install pytest-xdist

  2. Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple

  3. Requirement already satisfied: pytest-xdist in d:\python37\lib\site-packages (1.31.0)

  4. Requirement already satisfied: six in d:\python37\lib\site-packages (from pytest-xdist) (1.15.0)

  5. Requirement already satisfied: execnet>=1.1in d:\python37\lib\site-packages (from pytest-xdist) (1.8.0)

  6. Requirement already satisfied: pytest>=4.4.0in d:\python37\lib\site-packages (from pytest-xdist) (6.2.4)

  7. Requirement already satisfied: pytest-forked in d:\python37\lib\site-packages (from pytest-xdist) (1.1.3)

  8. Requirement already satisfied: apipkg>=1.4in d:\python37\lib\site-packages (from execnet>=1.1->pytest-xdist) (1.5)

  9. Requirement already satisfied: toml in d:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (0.10.2)

  10. Requirement already satisfied: attrs>=19.2.0in d:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (20.3.0)

  11. Requirement already satisfied: colorama in d:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (0.4.4)

  12. Requirement already satisfied: atomicwrites>=1.0in d:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (1.4.0)

  13. Requirement already satisfied: pluggy<1.0.0a1,>=0.12in d:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (0.13.1)

  14. Requirement already satisfied: py>=1.8.2in d:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (1.10.0)

  15. Requirement already satisfied: importlib-metadata>=0.12in d:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (2.1.1)

  16. Requirement already satisfied: packaging in d:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (20.8)

  17. Requirement already satisfied: iniconfig in d:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (1.1.1)

  18. Requirement already satisfied: zipp>=0.5in d:\python37\lib\site-packages (from importlib-metadata>=0.12->pytest>=4.4.0->pytest-xdist) (1.2.0)

  19. Requirement already satisfied: pyparsing>=2.0.2in d:\python37\lib\site-packages (from packaging->pytest>=4.4.0->pytest-xdist) (2.4.7)

7 pytest-xdist的优势

8 pytest-xdist的使用

8.1 普通执行
  1. import pytest

  2. import time

  3. class TestCase01():

  4. def test_case_01(self):

  5. time.sleep(1)

  6. print("case01$$$$$$$$$$$$$$$$$$$$$")

  7. def test_case_02(self):

  8. time.sleep(1)

  9. print("case02$$$$$$$$$$$$$$$$$$$$$")

  10. def test_case_03(self):

  11. time.sleep(1)

  12. print("case03$$$$$$$$$$$$$$$$$$$$$")

  13. def test_case_04(self):

  14. time.sleep(1)

  15. print("case04$$$$$$$$$$$$$$$$$$$$$")

  16. def test_case_05(self):

  17. time.sleep(1)

  18. print("case05$$$$$$$$$$$$$$$$$$$$$")

  19. def test_case_06(self):

  20. time.sleep(1)

  21. print("case06$$$$$$$$$$$$$$$$$$$$$")

  22. class TestCase02():

  23. def test_case_07(self):

  24. time.sleep(1)

  25. print("case07$$$$$$$$$$$$$$$$$$$$$")

  26. def test_case_08(self):

  27. time.sleep(1)

  28. print("case08$$$$$$$$$$$$$$$$$$$$$")

  29. def test_case_09(self):

  30. time.sleep(1)

  31. print("case08$$$$$$$$$$$$$$$$$$$$$")

  32. if __name__ == '__main__':

  33. pytest.main(["-s", "test_xdist.py"])

执行结果如下,使用了9.14s:

  1. test_xdist.py::TestCase01::test_case_01

  2. test_xdist.py::TestCase01::test_case_02

  3. test_xdist.py::TestCase01::test_case_03

  4. test_xdist.py::TestCase01::test_case_04

  5. test_xdist.py::TestCase01::test_case_05

  6. test_xdist.py::TestCase01::test_case_06

  7. test_xdist.py::TestCase02::test_case_07 PASSED [ 11%]case01$$$$$$$$$$$$$$$$$$$$$

  8. PASSED [ 22%]case02$$$$$$$$$$$$$$$$$$$$$

  9. PASSED [ 33%]case03$$$$$$$$$$$$$$$$$$$$$

  10. PASSED [ 44%]case04$$$$$$$$$$$$$$$$$$$$$

  11. PASSED [ 55%]case05$$$$$$$$$$$$$$$$$$$$$

  12. PASSED [ 66%]case06$$$$$$$$$$$$$$$$$$$$$

  13. PASSED [ 77%]case07$$$$$$$$$$$$$$$$$$$$$

  14. test_xdist.py::TestCase02::test_case_08 PASSED [ 88%]case08$$$$$$$$$$$$$$$$$$$$$

  15. test_xdist.py::TestCase02::test_case_09 PASSED [100%]case08$$$$$$$$$$$$$$$$$$$$$

  16. ============================== 9 passed in9.14s ==============================

8.2 上述代码分布式执行:

pytest -s -n auto test_xdist.py
  1. (venv) F:\pytest_study\test_case\test_j>pytest -s -n auto test_xdist.py

  2. ============================================ test session starts =============================================

  3. platform win32 -- Python 3.7.0, pytest-6.2.4, py-1.10.0, pluggy-0.13.1

  4. rootdir: F:\pytest_study, configfile: pytest.ini

  5. 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,

  6. repeat-0.9.1, rerunfailures-10.3, xdist-1.31.0

  7. gw0 [9] / gw1 [9] / gw2 [9] / gw3 [9] / gw4 [9] / gw5 [9] / gw6 [9] / gw7 [9]

  8. .........

  9. ============================================= 9 passed in4.51s ==============================================

8.3 指定CPU运行数量
  1. # x为cpu个数

  2. pytest -s -n x

  1. (venv) F:\pytest_study\test_case\test_j>pytest -s -n 2 test_xdist.py

  2. ============================================ test session starts =============================================

  3. platform win32 -- Python 3.7.0, pytest-6.2.4, py-1.10.0, pluggy-0.13.1

  4. rootdir: F:\pytest_study, configfile: pytest.ini

  5. 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,

  6. repeat-0.9.1, rerunfailures-10.3, xdist-1.31.0

  7. gw0 [9] / gw1 [9]

  8. .........

  9. ============================================= 9 passed in6.27s ==============================================

8.4 与pytest-html一起使用
pytest -s -n auto --html=report.html --self-contained-html
  1. pytest -s -n auto test_xdist.py --html=report.thml --self-contained-htm

  2. l

  1. gw0 [9] / gw1 [9] / gw2 [9] / gw3 [9] / gw4 [9] / gw5 [9] / gw6 [9] / gw7 [9]

  2. .........

  3. ------------------ generated html file: file://F:\pytest_study\test_case\test_j\report.thml ------------------

  4. ============================================= 9 passed in4.68s ==============================================

8.5 让pytest-xdist按照指定顺序执行
参数说明
--dist=loadscope同一个模块module下的函数和同一个测试类class下的方法来分组
--dist=loadfile同一个文件名来分组
8.6 pytest-xdist如何保持session执行一次
  1. import pytest

  2. from filelock import FileLock

  3. @pytest.fixture(scope="session")

  4. def login():

  5. print("====登录===")

  6. with FileLock("session.lock"):

  7. name = "zhang"

  8. password= "123456"

  9. # web ui自动化

  10. # 声明一个driver,再返回

  11. # 接口自动化

  12. # 发起一个登录请求,将token返回都可以这样写

  13. yield name, password

  14. print("====退出====")

感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取

http://www.jsqmd.com/news/253480/

相关文章:

  • 9款实用的论文查重工具全部免费提供,每天可多次检测,确保学术成果更原创
  • 2026年企业客户服务新趋势:全渠道SCRM推荐,微盛·企微管家如何助力降本增效?
  • 【电动车】基于削峰填谷的电动汽车多目标优化调度策略研究附Matlab代码
  • Postman接口测试之:添加Cookie伪造请求
  • 解密Fiddler,从零开始轻松掌握弱网测试技巧!
  • 发现9款完全免费的优质论文查重工具,每日无限次检测,让论文写作更顺利
  • 推荐9款零成本的论文查重软件,每天可自由检测多次,轻松提升论文通过率
  • python基于django的网上书店的图书销售商城
  • 从写作到降重:7大AI模型一站式解决方案
  • django-flask基于python婚纱摄影预约管理系统pycharm -Vue
  • python基于django的老年人健康养生系统的设计与实现
  • AI赋能学术:7款论文工具核心功能解析
  • 如何做接口自动化测试?
  • django-flask基于python律师服务预约系统pycharm -Vue
  • python基于django 的酒店客房预定管理系统的设计与实现
  • 拒绝抄袭风险:7大智能改写工具实战指南
  • django-flask基于python建筑材料管理系统pycharm -Vue
  • django-flask基于python教职工教师教学科研档案管理系统
  • 7款AI论文写作助手盘点:提升效率与降低重复率技巧
  • python基于django固定资产折旧及租赁维修管理系统的设计与实现
  • 英国的 IT 技术出版社 图书对应的免费代码 https://github.com/PacktPublishing
  • 7大AI论文助手盘点:改写降重功能实测对比
  • django-flask基于python汽车维修保养管理系统pycharm -Vue
  • 今天为大家推荐9款完全免费的论文查重工具,每天都能不限次数进行检测,轻松解决重复率问题
  • 收藏!大模型岗位全景解析:小白程序员转型必看指南
  • GESP认证C++编程真题解析 | B4260 [GESP202503 二级] 时间跨越
  • 机器人及自动驾驶定位99%靠它?组合导航差分技术全解析(附选型指南)
  • 收藏备用!AI记忆系统三阶段演进:从RAG到Agent Memory的读写革命
  • A.每日一题——128. 最长连续序列
  • 塑料管材挤出机结构设计