pytest接口自动化测试框架 | 多进程运行用例

Posted COCOgsta

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pytest接口自动化测试框架 | 多进程运行用例相关的知识,希望对你有一定的参考价值。

视频来源:B站《冒死上传!pytest接口自动化测试框架(基础理论到项目实战及二次开发)教学视频【软件测试】》

一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!

附上汇总贴:pytest接口自动化测试框架 | 汇总_COCOgsta的博客-CSDN博客


安装pytest-xdist

pip install pytest-xdist

运行模式:

pytest -n NUMCPUS

import pytest

def test_case01():
    assert 1==1


def test_case02():
    assert 1 == 1


def test_case03():
    assert 1 == 3


def test_case04():
    assert 1 == 4


def test_case05():
    assert 1 == 5


def test_case06():
    assert 1 == 1

if __name__ == '__main__':
    # 将测试发送到多个CPU
    # pytest.main(["-n", "2", "test_many.py"])
    # 使用与计算机具有的CPU内核一样多的进程
    pytest.main(["-n", "auto", "test_many.py"])

也是通过命令行执行生效

PS D:\\SynologyDrive\\CodeLearning\\WIN\\pytest\\base_used> pytest .\\test_fail2.py --maxfail 2
======================================================================================================== test session starts ========================================================================================================
platform win32 -- Python 3.6.6, pytest-7.0.1, pluggy-1.0.0
rootdir: D:\\SynologyDrive\\CodeLearning\\WIN\\pytest\\base_used
plugins: allure-pytest-2.9.45, forked-1.4.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.2, xdist-2.5.0
collected 3 items                                                                                                                                                                                                                    

test_fail2.py FF

============================================================================================================= FAILURES ==============================================================================================================
____________________________________________________________________________________________________________ test_fail01 ____________________________________________________________________________________________________________

    def test_fail01():
        print("第一次失败")
>       assert 1==2
E       assert 1 == 2

test_fail2.py:6: AssertionError
------------------------------------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------------------------------------
第一次失败
____________________________________________________________________________________________________________ test_fail02 ____________________________________________________________________________________________________________

    def test_fail02():
        print("第二次失败")
>       assert 1 == 2
E       assert 1 == 2

test_fail2.py:11: AssertionError
------------------------------------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------------------------------------
第二次失败
====================================================================================================== short test summary info ======================================================================================================
FAILED test_fail2.py::test_fail01 - assert 1 == 2
FAILED test_fail2.py::test_fail02 - assert 1 == 2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 2 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
========================================================================================================= 2 failed in 0.09s =========================================================================================================
PS D:\\SynologyDrive\\CodeLearning\\WIN\\pytest\\base_used>
PS D:\\SynologyDrive\\CodeLearning\\WIN\\pytest\\base_used> pip install pytest-xdist
Requirement already satisfied: pytest-xdist in c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\lib\\site-packages (2.5.0)
Requirement already satisfied: pytest>=6.2.0 in c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\lib\\site-packages (from pytest-xdist) (7.0.1)
Requirement already satisfied: execnet>=1.1 in c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\lib\\site-packages (from pytest-xdist) (1.9.0)
Requirement already satisfied: pytest-forked in c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\lib\\site-packages (from pytest-xdist) (1.4.0)
Requirement already satisfied: colorama; sys_platform == "win32" in c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\lib\\site-packages (from pytest>=6.2.0->pytest-xdist) (0.4.5)
Requirement already satisfied: importlib-metadata>=0.12; python_version < "3.8" in c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\lib\\site-packages (from pytest>=6.2.0->pytest-xdist) (4.8.3)
Requirement already satisfied: attrs>=19.2.0 in c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\lib\\site-packages (from pytest>=6.2.0->pytest-xdist) (21.4.0)
Requirement already satisfied: atomicwrites>=1.0; sys_platform == "win32" in c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\lib\\site-packages (from pytest>=6.2.0->pytest-xdist) (1.4.1)
Requirement already satisfied: tomli>=1.0.0 in c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\lib\\site-packages (from pytest>=6.2.0->pytest-xdist) (1.2.3)
Requirement already satisfied: pluggy<2.0,>=0.12 in c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\lib\\site-packages (from pytest>=6.2.0->pytest-xdist) (1.0.0)
Requirement already satisfied: py>=1.8.2 in c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\lib\\site-packages (from pytest>=6.2.0->pytest-xdist) (1.11.0)
Requirement already satisfied: iniconfig in c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\lib\\site-packages (from pytest>=6.2.0->pytest-xdist) (1.1.1)
Requirement already satisfied: packaging in c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\lib\\site-packages (from pytest>=6.2.0->pytest-xdist) (21.3)
Requirement already satisfied: zipp>=0.5 in c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\lib\\site-packages (from importlib-metadata>=0.12; python_version < "3.8"->pytest>=6.2.0->pytest-xdist) (3.6.0)
Requirement already satisfied: typing-extensions>=3.6.4; python_version < "3.8" in c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\lib\\site-packages (from importlib-metadata>=0.12; python_version < "3.8"->pytest>=6.2.0->p
ytest-xdist) (4.1.1)
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\lib\\site-packages (from packaging->pytest>=6.2.0->pytest-xdist) (3.0.7)
You are using pip version 10.0.1, however version 21.3.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
PS D:\\SynologyDrive\\CodeLearning\\WIN\\pytest\\base_used> pytest .\\test_fail2.py -n=2
======================================================================================================== test session starts ========================================================================================================
platform win32 -- Python 3.6.6, pytest-7.0.1, pluggy-1.0.0
rootdir: D:\\SynologyDrive\\CodeLearning\\WIN\\pytest\\base_used
plugins: allure-pytest-2.9.45, forked-1.4.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.2, xdist-2.5.0
gw0 [3] / gw1 [3]
FF.                                                                                                                                                                                                                            [100%]
============================================================================================================= FAILURES ==============================================================================================================
____________________________________________________________________________________________________________ test_fail01 ____________________________________________________________________________________________________________
[gw0] win32 -- Python 3.6.6 c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\python.exe

    def test_fail01():
        print("第一次失败")
>       assert 1==2
E       assert 1 == 2

test_fail2.py:6: AssertionError
------------------------------------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------------------------------------
第一次失败
____________________________________________________________________________________________________________ test_fail02 ____________________________________________________________________________________________________________
[gw1] win32 -- Python 3.6.6 c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\python.exe

    def test_fail02():
        print("第二次失败")
>       assert 1 == 2
E       assert 1 == 2

test_fail2.py:11: AssertionError
------------------------------------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------------------------------------
第二次失败
====================================================================================================== short test summary info ======================================================================================================
FAILED test_fail2.py::test_fail01 - assert 1 == 2
FAILED test_fail2.py::test_fail02 - assert 1 == 2
==================================================================================================== 2 failed, 1 passed in 0.84s ====================================================================================================
PS D:\\SynologyDrive\\CodeLearning\\WIN\\pytest\\base_used> pytest .\\test_many.py -n=2
======================================================================================================== test session starts ========================================================================================================
platform win32 -- Python 3.6.6, pytest-7.0.1, pluggy-1.0.0
rootdir: D:\\SynologyDrive\\CodeLearning\\WIN\\pytest\\base_used
plugins: allure-pytest-2.9.45, forked-1.4.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.2, xdist-2.5.0
gw0 [6] / gw1 [6]
..FFF.                                                                                                                                                                                                                         [100%]
============================================================================================================= FAILURES ==============================================================================================================
____________________________________________________________________________________________________________ test_case03 ____________________________________________________________________________________________________________
[gw0] win32 -- Python 3.6.6 c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\python.exe

    def test_case03():
>       assert 1 == 3
E       assert 1 == 3

test_many.py:12: AssertionError
____________________________________________________________________________________________________________ test_case04 ____________________________________________________________________________________________________________
[gw1] win32 -- Python 3.6.6 c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\python.exe

    def test_case04():
>       assert 1 == 4
E       assert 1 == 4

test_many.py:16: AssertionError
____________________________________________________________________________________________________________ test_case05 ____________________________________________________________________________________________________________
[gw0] win32 -- Python 3.6.6 c:\\users\\guoliang\\appdata\\local\\programs\\python\\python36\\python.exe

    def test_case05():
>       assert 1 == 5
E       assert 1 == 5

test_many.py:20: AssertionError
====================================================================================================== short test summary info ======================================================================================================
FAILED test_many.py::test_case03 - assert 1 == 3
FAILED test_many.py::test_case04 - assert 1 == 4
FAILED test_many.py::test_case05 - assert 1 == 5
==================================================================================================== 3 failed, 3 passed in 0.83s ====================================================================================================
PS D:\\SynologyDrive\\CodeLearning\\WIN\\pytest\\base_used>

以上是关于pytest接口自动化测试框架 | 多进程运行用例的主要内容,如果未能解决你的问题,请参考以下文章

pytest接口自动化测试框架 | 使用装饰器修饰需要运行的用例

Python测试框架pytest(22)插件 - pytest-xdist(分布式执行)

pytest接口自动化测试框架 | 控制测试用例执行

Python语言+pytest框架+allure报告+log日志+yaml文件+mysql断言实现接口自动化框架

Python语言+pytest框架+allure报告+log日志+yaml文件+mysql断言实现接口自动化框架

pytest+requests+excel 接口自动化框架