zoukankan      html  css  js  c++  java
  • pytest文档50-命令行参数--durations统计用例运行时间

    前言

    写完一个项目的自动化用例之后,发现有些用例运行较慢,影响整体的用例运行速度,于是领导说找出运行慢的那几个用例优化下。
    --durations 参数可以统计出每个用例运行的时间,对用例的时间做个排序。

    --durations=N

    pytest -h 查看命令行参数,关于 --durations=N 参数的使用方式

    >pytest -h
    
    reporting:
      --durations=N         show N slowest setup/test durations (N=0 for all).
    

    当 N=0 的时候显示全部用例的运行时间

    --durations=0

    先写几个pytest的用例,在用例里面加sleep时间,这样方便看到每个用例运行的持续时间

    import pytest
    import time
    # 作者-上海悠悠 QQ交流群:717225969
    # blog地址 https://www.cnblogs.com/yoyoketang/
    
    
    @pytest.fixture()
    def set_up_fixture():
        time.sleep(0.1)
        yield
        time.sleep(0.2)
    
    
    def test_01(set_up_fixture):
        print("用例1")
        time.sleep(1.0)
    
    
    def test_02(set_up_fixture):
        print("用例2")
        time.sleep(0.6)
    
    
    def test_03(set_up_fixture):
        print("用例3")
        time.sleep(1.2)
    
    
    def test_04(set_up_fixture):
        print("用例4")
        time.sleep(0.3)
    
    
    def test_05(set_up_fixture):
        print("用例5")
        time.sleep(2.3)
    

    当 N=0 的时候显示全部用例的运行时间

    >pytest test_dur.py --durations=0 -v
    ============================= test session starts =============================
    collected 5 items
    
    test_dur.py::test_01 PASSED                                              [ 20%]
    test_dur.py::test_02 PASSED                                              [ 40%]
    test_dur.py::test_03 PASSED                                              [ 60%]
    test_dur.py::test_04 PASSED                                              [ 80%]
    test_dur.py::test_05 PASSED                                              [100%]
    
    =========================== slowest test durations ============================
    2.30s call     test_dur.py::test_05
    1.20s call     test_dur.py::test_03
    1.00s call     test_dur.py::test_01
    0.60s call     test_dur.py::test_02
    0.30s call     test_dur.py::test_04
    0.20s teardown test_dur.py::test_05
    0.20s teardown test_dur.py::test_01
    0.20s teardown test_dur.py::test_02
    0.20s teardown test_dur.py::test_03
    0.20s teardown test_dur.py::test_04
    0.10s setup    test_dur.py::test_03
    0.10s setup    test_dur.py::test_01
    0.10s setup    test_dur.py::test_02
    0.10s setup    test_dur.py::test_05
    0.10s setup    test_dur.py::test_04
    ========================== 5 passed in 7.05 seconds ===========================
    

    用例运行的时候会经历3个阶段:setup,call,teardown。call就是测试用例,setup和teardown就是用例的fixture部分。

    --durations=3

    如果我们只需要筛选出运行时间最慢的3条用例,可以设置--durations=3

    >pytest test_dur.py --durations=3 -v
    ============================= test session starts =============================
    
    collected 5 items
    
    test_dur.py::test_01 PASSED                                              [ 20%]
    test_dur.py::test_02 PASSED                                              [ 40%]
    test_dur.py::test_03 PASSED                                              [ 60%]
    test_dur.py::test_04 PASSED                                              [ 80%]
    test_dur.py::test_05 PASSED                                              [100%]
    
    ========================== slowest 3 test durations ===========================
    2.30s call     test_dur.py::test_05
    1.20s call     test_dur.py::test_03
    1.00s call     test_dur.py::test_01
    ========================== 5 passed in 7.00 seconds ===========================
    

    这样就可以对运行慢的用例针对性优化。

  • 相关阅读:
    SharePoint 2013 中的SQL Server 安全
    SharePoint 2013 的HTML5特性之响应式布局
    SharePoint 2013 一些小技巧
    SharePoint 2013 排错之"Code blocks are not allowed in this file"
    SharePoint 2013 创建搜索中心及搜索设置
    SharePoint 2013 使用PowerShell创建State Service
    SharePoint 2013 内容部署功能简介
    SharePoint 使用PowerShell恢复误删的网站集
    SharePoint 自定义WebPart之间的连接
    linux之misc及使用misc创建字符设备
  • 原文地址:https://www.cnblogs.com/yoyoketang/p/13604333.html
Copyright © 2011-2022 走看看