关键字:-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
"""