pytest学习系列_插件之分布式执行

Posted durant0420

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pytest学习系列_插件之分布式执行相关的知识,希望对你有一定的参考价值。

一、前言

  在我们进行接口自动化的时候,用例往往有成百上千上万条用例,串行执行时间在分钟或者是小时级别。虽然能满足我们一般的回归性测试需求,但是还是对于企业级别的项目来说,还是显得有些力不从心。那么有没有一种比较好的解决方案呢,pytest的pytest-xdist插件可以很好解决我们的困惑

二、pytest-xdist的安装

  pip install pytest-xdist

三、测试实践步骤

  1、项目结构

  技术图片

   2、测试代码示例

#!/usr/bin/python3
# -*- coding: UTF-8 -*-
"""
@author:durant.zeng
@Description:描述
@file:test_xdist.py
@time:2020/12/09
"""
import requests

def test_tmall_001():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())

def test_tmall_002():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())

def test_tmall_003():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())

def test_tmall_004():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())

def test_tmall_005():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())

def test_tmall_006():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())

def test_tmall_007():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())

def test_tmall_008():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())

def test_tmall_001():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())

def test_tmall_009():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())

def test_tmall_010():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())


def test_tmall_011():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())


def test_tmall_012():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())

def test_tmall_013():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())

def test_tmall_014():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())

def test_tmall_015():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())

def test_tmall_016():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())

def test_tmall_017():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())

def test_tmall_018():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())

def test_tmall_019():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())

def test_tmall_020():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())

def test_tmall_021():
    r = requests.get(url="https://www.tmall.com")
    print(r.elapsed.total_seconds())

  3、正常执行的耗时

    pytest -v -s

 xdistTest	est_xdist_002.py::test_baidu_020 ?                                                                                                98% ████
█████▉0.138376

 xdistTest	est_xdist_002.py::test_baidu_021 ?                                                                                               100% ████
██████
==================================================================== warnings summary =====================================================================
c:usersdurant.zeng.virtualenvscloudstoragelibsite-packagespykwalifycore.py:7
  c:usersdurant.zeng.virtualenvscloudstoragelibsite-packagespykwalifycore.py:7: DeprecationWarning: the imp module is deprecated in favour of import
lib; see the modules documentation for alternative uses
    import imp

-- Docs: https://docs.pytest.org/en/stable/warnings.html

Results (12.24s):
      63 passed

  可以看出来三个文件总共是63个测试用例,总的执行时间为12.24s

  4、使用pytest-xdist插件分布式执行

  pytest -v -s -n auto

  参数说明:

  -n auto:自动的检测到执行器的cpu个数

 xdistTest	est_xdist_002.py::test_baidu_020 ?                                                                                                98% ████
█████▉ xdistTest/test_xdist_002.py

 xdistTest	est_xdist_002.py::test_baidu_021 ?                                                                                               100% ████
██████ xdistTest/test_xdist_002.py


==================================================================== warnings summary =====================================================================
c:usersdurant.zeng.virtualenvscloudstoragelibsite-packagespykwalifycore.py:7
c:usersdurant.zeng.virtualenvscloudstoragelibsite-packagespykwalifycore.py:7
c:usersdurant.zeng.virtualenvscloudstoragelibsite-packagespykwalifycore.py:7
c:usersdurant.zeng.virtualenvscloudstoragelibsite-packagespykwalifycore.py:7
  c:usersdurant.zeng.virtualenvscloudstoragelibsite-packagespykwalifycore.py:7: DeprecationWarning: the imp module is deprecated in favour of import
lib; see the modules documentation for alternative uses
    import imp

-- Docs: https://docs.pytest.org/en/stable/warnings.html

Results (7.01s):
      63 passed

  可以看出来,使用分布式插件执行,总的执行时间为7.01s,大大减少了耗时

以上是关于pytest学习系列_插件之分布式执行的主要内容,如果未能解决你的问题,请参考以下文章

46-pytest-分布式插件pytest-xdist使用

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

pytest---分布式执行用例(pytest-xdist)

pytest接口自动化测试框架 | pytest获取执行数据pytest禁用插件

pytest_03_pycharm运行pytest (转:上海悠悠)

pytest学习和使用17-Pytest如何重复执行用例?(pytest-repeat)