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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值