所以总结来说,其实就是为了提升效率和质量。
特点 | 说明 |
---|---|
网格化 | 多节点互联互通,可资源共享 |
分布性 | 地域和计算机上,协同工作、负载均衡、可扩展性、高可用性 |
开放性 | 可移植性、可互操作性、可伸缩性、易获得性 |
实时性 | 各种信息都必须是实时的 |
动态性 | 测试过程对象和活动动态映射 |
处理不确定性 | 具有处理不确定性的能力 |
容错及安全性 | 容错能力强,可靠性高、安全性好 |
技术点 | 要求 |
---|---|
分布式环境 | 获取全局状态,能够方便地监视和操纵测试过程;集中式的分布式策略。 |
分布式环境下的节点通信 | 稳定的通信环境;适合用基于消息通信的方式来实现。 |
测试任务调度 | 静态调度、动态调度和混合调度。 |
pytest-xdist
让自动化测试用例分布式执行,节省测试时间,属于进程级别的并发;pip3 install pytest-xdist
C:\Users\Administrator>pip3 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: execnet>=1.1 in d:\python37\lib\site-packages (from pytest-xdist) (1.8.0)
Requirement already satisfied: pytest>=4.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: apipkg>=1.4 in d:\python37\lib\site-packages (from execnet>=1.1->pytest-xdist) (1.5)
Requirement already satisfied: toml in d:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (0.10.2)
Requirement already satisfied: attrs>=19.2.0 in d:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (20.3.0)
Requirement already satisfied: colorama in d:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (0.4.4)
Requirement already satisfied: atomicwrites>=1.0 in d:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (1.4.0)
Requirement already satisfied: pluggy<1.0.0a1,>=0.12 in d:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (0.13.1)
Requirement already satisfied: py>=1.8.2 in d:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (1.10.0)
Requirement already satisfied: importlib-metadata>=0.12 in d:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (2.1.1)
Requirement already satisfied: packaging in d:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (20.8)
Requirement already satisfied: iniconfig in d:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (1.1.1)
Requirement already satisfied: zipp>=0.5 in d:\python37\lib\site-packages (from importlib-metadata>=0.12->pytest>=4.4.0->pytest-xdist) (1.2.0)
Requirement already satisfied: pyparsing>=2.0.2 in d:\python37\lib\site-packages (from packaging->pytest>=4.4.0->pytest-xdist) (2.4.7)
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/16
# 文件名称:test_xdist.py
# 作用:pytest-xdist分布式测试
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import pytest
import time
class 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"])
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 ==============================
pytest -s -n auto test_xdist.py
(venv) F:\pytest_study\test_case\test_j>pytest -s -n auto test_xdist.py
============================================ test 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 ==============================================
-n auto
:可以自动检测到系统的CPU
核数;auto
利用了所有CPU
来跑用例;CPU
来跑用例:# x为cpu个数
pytest -s -n x
CPU
来跑用例时长为6.27s:(venv) F:\pytest_study\test_case\test_j>pytest -s -n 2 test_xdist.py
============================================ test 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 ==============================================
pytest -s -n auto --html=report.html --self-contained-html
pytest -s -n auto test_xdist.py --html=report.thml --self-contained-htm
l
gw0 [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 ==============================================
pytest-xdist
执行默认是无须的;--dist
参数来控制顺序;参数 | 说明 |
---|---|
| 同一个模块 |
| 同一个文件名来分组 |
pytest-xdist
没有内置的支持来确保会话范围的夹具仅执行一次;FileLock
方法仅仅产生一次fixture
数据:import pytest
from filelock import FileLock
@pytest.fixture(scope="session")
def login():
print("====登录===")
with FileLock("session.lock"):
name = "zhang"
password= "123456"
# web ui自动化
# 声明一个driver,再返回
# 接口自动化
# 发起一个登录请求,将token返回都可以这样写
yield name, password
print("====退出====")
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。