前言
改造pytest命令列執行測驗用例時,回傳的內容,
添加用例描述
在撰寫測驗用例時,若將測驗目的寫為測驗用例名稱,可能會導致用例名稱過長,但是使用簡短的編號表示用例名稱時,從執行結果中又無法知曉具體的測驗邏輯,基于此,想要改造測驗結果的回傳內容,攜帶上用例描述資訊,
在conftest.py中添加代碼如下:
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
report.description = str(item.function.__doc__)
if not report.description:
report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")
else:
report.nodeid = "Description of this test is : " + report.description
效果如下:
test_doc.py::TestDoc::test_01
Description of this test is : this is the first test PASSED [ 33%]
test_doc.py::TestDoc::test_02
Description of this test is : this is the second test PASSED [ 66%]
test_doc.py::TestDoc::test_03
Description of this test is : None PASSED [100%]
如果有更好的解決方法,歡迎留言
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/88145.html
標籤:Python
上一篇:os.scandir遍歷檔案
