码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-12-15 12:05:34      阅读:2      评论:0      收藏:0      [点我收藏+]

标签:ack   use   cpu   级别   port   mod   http   bin   个数   

一、前言

  在我们进行接口自动化的时候,用例往往有成百上千上万条用例,串行执行时间在分钟或者是小时级别。虽然能满足我们一般的回归性测试需求,但是还是对于企业级别的项目来说,还是显得有些力不从心。那么有没有一种比较好的解决方案呢,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\test_xdist_002.py::test_baidu_020 ?                                                                                                98% ████
█████▉0.138376

 xdistTest\test_xdist_002.py::test_baidu_021 ?                                                                                               100% ████
██████
==================================================================== warnings summary =====================================================================
c:\users\durant.zeng\.virtualenvs\cloudstorage\lib\site-packages\pykwalify\core.py:7
  c:\users\durant.zeng\.virtualenvs\cloudstorage\lib\site-packages\pykwalify\core.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\test_xdist_002.py::test_baidu_020 ?                                                                                                98% ████
█████▉ xdistTest/test_xdist_002.py

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


==================================================================== warnings summary =====================================================================
c:\users\durant.zeng\.virtualenvs\cloudstorage\lib\site-packages\pykwalify\core.py:7
c:\users\durant.zeng\.virtualenvs\cloudstorage\lib\site-packages\pykwalify\core.py:7
c:\users\durant.zeng\.virtualenvs\cloudstorage\lib\site-packages\pykwalify\core.py:7
c:\users\durant.zeng\.virtualenvs\cloudstorage\lib\site-packages\pykwalify\core.py:7
  c:\users\durant.zeng\.virtualenvs\cloudstorage\lib\site-packages\pykwalify\core.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学习系列_插件之分布式执行

标签:ack   use   cpu   级别   port   mod   http   bin   个数   

原文地址:https://www.cnblogs.com/Durant0420/p/14106216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!