• Python测试框架之pytest高阶用法(六)


    1.修改 Python traceback 输出

    pytest --showlocals     # show local variables in tracebacks
    pytest -l               # show local variables (shortcut)
    pytest --tb=auto        # (default) 'long' tracebacks for the first and last
                            # entry, but 'short' style for the other entries
    pytest --tb=long        # exhaustive, informative traceback formatting
    pytest --tb=short       # shorter traceback format
    pytest --tb=line        # only one line per failure
    pytest --tb=native      # Python standard library formatting
    pytest --tb=no          # no traceback at all
    

      --full-trace 参数会打印更多的错误输出信息,比参数 --tb=long 还多,即使是 Ctrl+C 触发的错误,也会打印出来

    2. 执行失败的时候跳转到 PDB

    pytest --pdb              # 每次遇到失败都跳转到 PDB
    pytest -x --pdb           # 第一次遇到失败就跳转到 PDB,结束测试执行
    pytest --pdb --maxfail=3  # 只有前三次失败跳转到 PDB 
    

      pdb 是 Python 标准库的调试模块。在 pytest 中,可以直接使用 --pdb 参数在测试失败时开启调试;

    直接使用 --pdb 参数:

    import pytest
    def division(a, b):
        return int(a / b)
    @pytest.mark.parametrize('a, b, c', [(4, 2, 2), (0, 2, 0), (1, 0, 0), (6, 8, 0)])
    def test_1(a, b, c):
        res = division(a, b)
        assert res == c
    @pytest.mark.parametrize('a', [100,75])
    @pytest.mark.parametrize('b, c', [(4,25),(3,25)])
    def test_1(a, b, c):
        res = division(a, b)
        assert res == c
    if __name__ == '__main__':
        pytest.main(["-q","--pdb"])  

    断言失败,进入 pdb:

    pdb 提示符出现后,便可以使用 pdb 的交互调试功能,查看错误时,有以下常用命令:

    •  p/print expr :输出变量 expr 的值;
    • pp expr :美化输出 expr 的值;
    • l/list :列出错误并显示错误之前和之后的5行代码;
    • l/lsit begin, end :列出错误,并显示指定行号之间的代码;
    • a/args :打印当前函数的所有参数和变量;
    • u/up :移动到堆栈的上一层;
    • d/down :移动到堆栈的下一层;
    • q/quit :退出当前调试会话(也会退出测试会话);

    在控制台与 pdb 进行交互:

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PDB post_mortem >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    > e:pycharmprojectslianxi	est_abc.py(286)test_1()
    -> assert res == c
    (Pdb) p
    *** SyntaxError: unexpected EOF while parsing
    (Pdb) p a
    75
    (Pdb) pp a
    75
    (Pdb) l
    281  	    assert res == c
    282  	@pytest.mark.parametrize('a', [100,75])
    283  	@pytest.mark.parametrize('b, c', [(4,25),(3,25)])
    284  	def test_1(a, b, c):
    285  	    res = division(a, b)
    286  ->	    assert res == c
    287  	if __name__ == '__main__':
    288  	    pytest.main(["-q","--pdb"])
    289  	
    290  	# import pytest
    291  	# @pytest.fixture(scope='function')
    (Pdb) l 275,285
    275  	import pytest
    276  	def division(a, b):
    277  	    return int(a / b)
    278  	@pytest.mark.parametrize('a, b, c', [(4, 2, 2), (0, 2, 0), (1, 0, 0), (6, 8, 0)])
    279  	def test_1(a, b, c):
    280  	    res = division(a, b)
    281  	    assert res == c
    282  	@pytest.mark.parametrize('a', [100,75])
    283  	@pytest.mark.parametrize('b, c', [(4,25),(3,25)])
    284  	def test_1(a, b, c):
    285  	    res = division(a, b)
    (Pdb) a
    a = 75
    b = 4
    c = 25
    (Pdb) u
    > d:myprogramminiconda3libsite-packages\_pytestpython.py(180)pytest_pyfunc_call()
    -> result = testfunction(**testargs)
    (Pdb) u
    > d:myprogramminiconda3libsite-packagespluggycallers.py(187)_multicall()
    -> res = hook_impl.function(*args)
    (Pdb) d
    > d:myprogramminiconda3libsite-packages\_pytestpython.py(180)pytest_pyfunc_call()
    -> result = testfunction(**testargs)
    (Pdb) q
    
    ------ generated html file: file://E:PycharmProjectslianxi
    eport.html ------
    =========================== short test summary info ===========================
    FAILED test_abc.py::test_1[4-25-75] - assert 18 == 25
    !!!!!!!!!!!!!!!!!! _pytest.outcomes.Exit: Quitting debugger !!!!!!!!!!!!!!!!!!!
    1 failed, 1 passed in 112.57s (0:01:52)
    

      更多的pdb使用方法可参考pdb详细的使用方法:https://docs.python.org/3/library/pdb.html

    3.设置断点

    在用例脚本中加入如下python代码,pytest会自动关闭执行输出的抓取,这里,其他test脚本不会受到影响,带断点的test上一个test正常输出

     import pdb; pdb.set_trace()

    4.获取用例执行性能数据

    pytest --durations=10
    

      

    5.生成 JUnitXML 格式的结果文件

    这种格式的结果文件可以被Jenkins或其他CI工具解析

    pytest --junitxml=path

    6.禁用插件 

    例如,关闭 doctest 插件

    pytest -p no:doctest

    7.从Python代码中调用pytest

    pytest.main()                      # 基本用法
    pytest.main(['-x', 'mytestdir'])   # 传入配置参数
     
     
    // 指定自定义的或额外的插件
    # content of myinvoke.py
    import pytest
    class MyPlugin(object):
        def pytest_sessionfinish(self):
            print("*** test run reporting finishing")
     
    pytest.main(["-qq"], plugins=[MyPlugin()]) 

    8.测试脚本迁移后快速部署包含pytest的virtualenv

    例如你从Gitlab仓库里clone了项目组的刀刀同学编写的测试脚本到你自己的电脑里,你想修改些东西,并调试,咋办?可以通过下面的操作快速创建 VirtualEnv

    cd <repository>
    pip install -e .
    

      

     

      

  • 相关阅读:
    P1074 靶形数独
    PYTHON-模块 sys os random shutil-练习
    PYTHON-模块-time&datetime-练习 +目录规范
    PYTHON-模块time&datetime+ 目录规范
    PYTHON-模块定义 搜索路径
    PYTHON-匿名函数,递归与二分法,面向过程编程-练习
    PYTHON-匿名函数,递归与二分法,面向过程编程
    PYTHON-有参装饰器,无参装饰器,语法糖
    PYTHON-迭代器,xxx生成式
    PYTHON-函数对象,嵌套,名称空间与作用域,闭包函数
  • 原文地址:https://www.cnblogs.com/wxcx/p/13806363.html
Copyright © 2020-2023  润新知