pytest教程-27-分布式执行用例插件-pytest-xdist

发布于:2024-04-28 ⋅ 阅读:(26) ⋅ 点赞:(0)

上一小节我们学习了pytest随机执行用例插件-pytest-random-order,本小节我们讲解一下pytest分布式执行用例插件pytest-xdist。

前言

平常我们手工测试用例非常多时,比如有1千条用例,假设每个用例执行需要1分钟。如果一个测试人员执行需要1000分钟才能执行完,当项目非常紧急的时候,

我们会用测试人力成本换取时间成本,这个时候多找个小伙伴把任务分成2部分,于是时间缩减一半。如果是十个人一起执行,1000个用例理论上只需100分钟就能完成,时间缩短到了1/10。大大节省的测试时间,为项目节省了时间成本。同样道理,当我们测试用例非常多的时候,一条条执行,很显然会比较慢,那么如何让测试用例并行执行呢,这就是我们接下来要讲的pytest分布式执行插件pytest-xdist。

什么是 pytest-xdist

pytest-xdist 是 pytest 测试框架的一个插件,它提供了多进程、多线程和分布式测试的支持。其中,多进程并发是其中一个引人注目的特性,它允许你同时在多个进程中运行测试用例,以加速整个测试过程。

分布式用例设计原则

  • 用例之间是独立的,用例之间没有依赖关系,用例可以完全独立运行【独立运行】
  • 用例执行没有顺序,随机顺序都能正常执行【随机执行】
  • 每个用例都能重复运行,运行结果不会影响其他用例【不影响其他用例】

安装插件

pip install pytest-xdist

基本使用

在安装了 pytest-xdist 之后,你可以在命令行中使用 -n 参数来指定并行执行的进程数。例如,如果你想要使用 4 个进程来运行测试,你可以这样做:

pytest -n 4

这将启动 4 个 worker 进程来并行执行测试。

控制执行顺序

默认情况下,pytest-xdist 是无序执行测试的。如果你需要按照一定的顺序执行测试,可以使用 --dist 参数。例如:

  • --dist=loadscope:按照模块(module)和测试类(class)来分组,确保同一个组的测试用例在同一进程中执行。
pytest -n 4 --dist=loadscope
  • --dist=loadfile:按照文件名来分组,确保同一个文件中的测试用例在同一进程中执行。
pytest -n 4 --dist=loadfile
  • --dist loadgroup: 测试按xdist_group标记分组。每组作为一个整体分配给可用的执行器。这保证了具有相同xdist_ group名称的所有测试都在同一个worker中运行。
@pytest.mark.xdist_group(name="group1")
def test1():
    pass

class TestA:
    @pytest.mark.xdist_group("group1")
    def test2():
        pass 

使用示例

顺序执行

import time
import pytest
def test_01():
    print('case 1')
    time.sleep(2)
    assert 1 == 1
def test_02():
    print('case 2')
    time.sleep(2)
    assert 1 == 1
def test_03():
    print('case 3')
    time.sleep(2)
    assert 1 == 1
def test_04():
    print('case 4')
    time.sleep(2)
    assert 1 == 1
def test_05():
    print('case 5')
    time.sleep(2)
    assert 1 == 1
if __name__ == '__main__':
    # 顺序执行
    pytest.main(['-s', 'test_78.py'])

325b1c526987d50659b46096f6735a8a.png

并行执行:加-n参数后面数字是并行数

import time
import pytest
def test_01():
    print('case 1')
    time.sleep(2)
    assert 1 == 1
def test_02():
    print('case 2')
    time.sleep(2)
    assert 1 == 1
def test_03():
    print('case 3')
    time.sleep(2)
    assert 1 == 1
def test_04():
    print('case 4')
    time.sleep(2)
    assert 1 == 1
def test_05():
    print('case 5')
    time.sleep(2)
    assert 1 == 1
if __name__ == '__main__':
    # 并行执行 加-n参数后面数字是并行数
    pytest.main(['-s', 'test_78.py', '-n=4', '--html=report.html', '--self-contained-html'])

3a31e0378023cc87755da865d09b9c7b.png

主从分布式测试

pytest-xdist 还支持主从(master-slave)模式,你可以在多台计算机上运行测试。首先,你需要在所有计算机上安装 pytest-xdist。然后,你可以在主机(master)上启动测试,同时在从机(workers)上启动监听。

主机(Master)配置

在主机上,创建一个配置文件(例如 pytest.ini),并设置以下参数:

[pytest]
addopts = -n auto

然后,在主机上运行以下命令:

pytest --dist=loadscope

从机(Worker)配置

在每台从机上,运行以下命令来启动监听:

pytest --dist=loadscope

这将使从机准备好接收来自主机的测试任务。

示例项目结构

假设你有一个名为 MyPytestDemo 的项目,其结构如下:

MyPytestDemo/
|-- conftest.py
|-- test_module1.py
|-- test_module2.py

在 conftest.py 中,你可以定义 fixtures:

# conftest.py
import pytest

@pytest.fixture(scope="session")
def setup():
    # 设置测试
    yield
    # 清理测试

在 test_module1.py 和 test_module2.py 中,你可以编写测试用例:

# test_module1.py
import pytest

def test_example1(setup):
    # 测试逻辑

    def test_example2(setup):
# 测试逻辑

运行分布式测试

在主机上运行分布式测试:

pytest --dist=loadscope -n auto MyPytestDemo/

这将在所有安装了 pytest-xdist 的计算机上并行执行测试。

cf67903e74b5404db5f6899add7c903a.jpeg