zoukankan      html  css  js  c++  java
  • pytest学习系列_pytest-xdist插件之分布式执行

    一、前言

      在我们进行接口自动化的时候,用例往往有成百上千上万条用例,串行执行时间在分钟或者是小时级别。虽然能满足我们一般的回归性测试需求,但是还是对于企业级别的项目来说,还是显得有些力不从心。那么有没有一种比较好的解决方案呢,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 module's 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 module's documentation for alternative uses
        import imp
    
    -- Docs: https://docs.pytest.org/en/stable/warnings.html
    
    Results (7.01s):
          63 passed

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

    知道、想到、做到、得到
  • 相关阅读:
    在windows上使用win2000资源工具
    Apache与Tomcat整合
    web服务器和应用服务器概念比较
    linux定时删除N天前的文件(文件夹)
    程序员如何自我学习和成长?
    20210708总结
    Docker 常用命令!还有谁不会?
    Redis常用命令set
    laravel与thinkphp之间的区别与优缺点
    2021年7月总结
  • 原文地址:https://www.cnblogs.com/Durant0420/p/14106216.html
Copyright © 2011-2022 走看看