Python测试框架之pytest高阶用法(六)-晒酷学院

晒酷学院:https://shareku.ke.qq.com/
微信号:添加请注明晒酷学院            QQ群:979438600
微信号:添加请注明晒酷学院         QQ群:979438600
       
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:\pycharmprojects\lianxi\test_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:\myprogram\miniconda3\lib\site-packages\_pytest\python.py(180)pytest_pyfunc_call()
-> result = testfunction(**testargs)
(Pdb) u
> d:\myprogram\miniconda3\lib\site-packages\pluggy\callers.py(187)_multicall()
-> res = hook_impl.function(*args)
(Pdb) d
> d:\myprogram\miniconda3\lib\site-packages\_pytest\python.py(180)pytest_pyfunc_call()
-> result = testfunction(**testargs)
(Pdb) q
 
------ generated html file: file://E:\PycharmProjects\lianxi\report.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 .
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值