pytest 中 record_property的用法,记录用例结果

一、需求介绍

pytest的测试用例是不允许返回值的,即在用例的最后不要写return。但有时需要记录用例的测试结果,做统计测试,需要知道用例最后得到的具体的数值,这个时候就需要一个记录结果的方法。pytest提供了一个记录结果的 ——> record_property。

二、record_property

1.作用

常用来记录单条用例的结果,保证与其他用例互不干扰,可以用来及时记录测试结果,防止程序崩溃后,丢失所有测试结果。

2.使用方式:

# test_case.py
def test(record_property):
	record_property("执行结果","result")

# conftest.py
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    # Execute all other hooks to obtain the report object
    outcome = yield

    rep = outcome.get_result()
	properties = rep.__dict__['user_properties']  # 读取记录的属性
posted @ 2024-12-26 15:13  Jonia430  阅读(76)  评论(0)    收藏  举报