pytest特性记录:打印失败用例的局部变量

关键字:-l

pytest运行参数 -l(--showlocal),在用例执行过程中,加上-l参数,可以在用例执行失败的时候,自动打印用例内部的局部变量值,方便我们定位问题,调试代码。举例如下:

import pytest


class TestPytestFeatures(object):
    def test_case_01(self):
        num = 1
        assert num == 1

    def test_case_02(self):
        tmp = ["a", "b"]
        assert isinstance(tmp, dict)


if __name__ == "__main__":
    pytest.main(["-l", "test_case_pytest_features.py"])


"""
运行结果
============================= test session starts =============================
platform win32 -- Python 3.8.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: E:\icp_capp\help
plugins: allure-pytest-2.9.43
collected 2 items

test_case_pytest_features.py .F                                          [100%]

================================== FAILURES ===================================
_______________________ TestPytestFeatures.test_case_02 _______________________

self = <help.test_case_pytest_features.TestPytestFeatures object at 0x0000020815942E20>

    def test_case_02(self):
        tmp = ["a", "b"]
>       assert isinstance(tmp, dict)
E       AssertionError: assert False
E        +  where False = isinstance(['a', 'b'], dict)

self       = <help.test_case_pytest_features.TestPytestFeatures object at 0x0000020815942E20>
tmp        = ['a', 'b']

test_case_pytest_features.py:18: AssertionError
=========================== short test summary info ===========================
FAILED test_case_pytest_features.py::TestPytestFeatures::test_case_02 - Asser...
========================= 1 failed, 1 passed in 0.09s =========================

Process finished with exit code 0

"""

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值