Bootstrap

Pytest如何并发执行自动化脚本

【原文链接】Pytest如何并发执行自动化脚本

当自动化脚本数量非常多的时候,全量脚本的执行耗时很长,此时就希望自动化脚本能够并发执行,第三方插件pytest-xdist 能很好的支持此功能,pytest-xdist是从进程层面进行的并行执行,能够真正做到并行执行

首先需要安装pytest-xdist插件,执行如下命令

pip install pytest-xdist

下面编写如下脚本,有8个测试用例,每个用例中通过time.sleep 等待模拟用例耗时,具体代码如下,通过粗略估计,如果直接使用pytest顺序执行,至少需要36秒的时间。

import time

def test_01():
    time.sleep(1)
    print("in test01...")
    assert 1==1

def test_02():
    time.sleep(2)
    print("in test02...")
    assert 1==1

def test_03():
    time.sleep(3)
    print("in test03...")
    assert 1==1

def test_04():
    time.sleep(4)
    print("in test04...")
    assert 1==1

def test_05():
    time.sleep(5)
    print("in test05...")
    assert 1==1

def test_06():
    time.sleep(6)
    print("in test06...")
    assert 1==1

def test_07():
    time.sleep(7)
    print("in test07...")
    assert 1==1

def test_08():
    time.sleep(8)
    print("in test08...")
    assert 1==1

首先使用pytest命令顺序执行,执行结果如下,可以发现总耗时38秒。

(demo--ip5ZZo0) D:\demo>pytest -s
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
rootdir: D:\demo
plugins: repeat-0.9.1
collected 8 items

test_demo.py in test01...
.in test02...
.in test03...
.in test04...
.in test05...
.in test06...
.in test07...
.in test08...
.

=================== 8 passed in 38.36s ====================

(demo--ip5ZZo0) D:\demo>

下面在安装pytest-xdist插件后,通过 -n auto 参数即可实现并行执行,-n 后面可以指定并行执行的数量,比如 -n 3 表示同一时间有三个任务在执行,如果是 -n auto 则自动计算当前服务器的cpu的总核数,然后将所有的cpu全部用上,此时服务器将会出现比较卡的状况,下面为使用 -n auto的执行结果,可以发现这里仅需要9秒,因为在test_08中等待了8秒,即此时耗时依赖最大耗时的用例。

(demo--ip5ZZo0) D:\demo>pytest -s -n auto
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
rootdir: D:\demo
plugins: forked-1.4.0, repeat-0.9.1, xdist-2.5.0
gw0 I / gw1 I / gw2 I / gw3 I / gw4 I / gw5 I / gw6 I / gw7 gw0 C / gw1 I / gw2 I / gw3 I / gw4 I / gw5 I / gw6 I / gw7 gw0 C / gw1 C / gw2 I / gw3 I / gw4 I / gw5 I / gw6 I / gw7 gw0 C / gw1 C / gw2 C / gw3 I / gw4 I / gw5 I / gw6 I / gw7 gw0 C / gw1 C / gw2 C / gw3 C / gw4 I / gw5 I / gw6 I / gw7 gw0 C / gw1 C / gw2 C / gw3 C / gw4 C / gw5 I / gw6 I / gw7 gw0 C / gw1 C / gw2 C / gw3 C / gw4 C / gw5 C / gw6 I / gw7 gw0 C / gw1 C / gw2 C / gw3 C / gw4 C / gw5 C / gw6 C / gw7 gw0 C / gw1 C / gw2 C / gw3 C / gw4 C / gw5 C / gw6 C / gw7 gw0 ok / gw1 C / gw2 C / gw3 C / gw4 C / gw5 C / gw6 C / gw7gw0 [8] / gw1 C / gw2 C / gw3 C / gw4 C / gw5 C / gw6 C / gwgw0 [8] / gw1 ok / gw2 C / gw3 C / gw4 C / gw5 C / gw6 C / ggw0 [8] / gw1 [8] / gw2 C / gw3 C / gw4 C / gw5 C / gw6 C / gw0 [8] / gw1 [8] / gw2 ok / gw3 C / gw4 C / gw5 C / gw6 C /gw0 [8] / gw1 [8] / gw2 [8] / gw3 C / gw4 C / gw5 C / gw6 C gw0 [8] / gw1 [8] / gw2 [8] / gw3 ok / gw4 C / gw5 C / gw6 Cgw0 [8] / gw1 [8] / gw2 [8] / gw3 [8] / gw4 C / gw5 C / gw6 gw0 [8] / gw1 [8] / gw2 [8] / gw3 [8] / gw4 ok / gw5 C / gw6gw0 [8] / gw1 [8] / gw2 [8] / gw3 [8] / gw4 [8] / gw5 C / gwgw0 [8] / gw1 [8] / gw2 [8] / gw3 [8] / gw4 [8] / gw5 ok / ggw0 [8] / gw1 [8] / gw2 [8] / gw3 [8] / gw4 [8] / gw5 [8] / gw0 [8] / gw1 [8] / gw2 [8] / gw3 [8] / gw4 [8] / gw5 [8] / gw0 [8] / gw1 [8] / gw2 [8] / gw3 [8] / gw4 [8] / gw5 [8] / gw0 [8] / gw1 [8] / gw2 [8] / gw3 [8] / gw4 [8] / gw5 [8] / gw0 [8] / gw1 [8] / gw2 [8] / gw3 [8] / gw4 [8] / gw5 [8] / gw6 [8] / gw7 [8]
........
==================== 8 passed in 9.43s ====================

(demo--ip5ZZo0) D:\demo>

如下结果为设置同时执行4个任务,即并行执行量设置为4,此时耗时12.87秒。

(demo--ip5ZZo0) D:\demo>pytest -s -n 4
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
rootdir: D:\demo
plugins: forked-1.4.0, repeat-0.9.1, xdist-2.5.0
gw0 [8] / gw1 [8] / gw2 [8] / gw3 [8]
........
=================== 8 passed in 12.87s ====================

(demo--ip5ZZo0) D:\demo>

可以发现通过pytest-xdist插件可以大幅提升脚本的执行效率。

;