我已经研究/学习了几天,但似乎无法弄清楚。我要做的就是检索每个测试功能的状态,以便我可以写出自己的日志文件。
我很确定我需要的部分是TestReport对象,钩住pytest_testreport_status和pytest_runtest_makereport。我对pytest还是很陌生,只是没有得到如何将所有这些东西“粘合”在一起以使其工作。
这是一个简单的测试。测试中的最后一条语句是一个函数调用(process_test_function_results),该函数处理测试结果并将结果写到日志文件中。我相信我想要的是将TestReport对象传递给process_test_function_results()。只是不确定如何使这些功能与挂钩一起使用。
import pytest
import lib.dnet_generic as mynet_generic
import datetime
@pytest.mark.parametrize("cmd",[
"show system version | no-more",
"show bgp summary | no-more",
"show bgp neighbor | no-more",
])
def test_cmds(logger, device_connections, testinfo, error_queue, cmd, rp_logger):
dut = testinfo['topo_map']['devices']['dev3']['name']
connection = device_connections[dut]
cmdOutput = connection.send_command(cmd)
if "ERROR" in cmdOutput:
err_time = datetime.datetime.now()
err_mess = (f"ERROR: The command <{cmd}> returned an error.\n"
f"Time: {err_time}\n"
f"Device: {dut}\n"
)
mess_level = "error"
message = {'level' : mess_level , 'message' : err_mess}
error_queue.put(message)
connection._write_session_log(err_mess)
mynet_generic.process_test_function_results(error_queue, logger,testinfo,rp_logger)
以下是通过process_test_function_results方法写入自定义输出日志文件的内容的摘要。您会注意到结果:为空。我相信我要填写的结果是:TestReport.outcome
------------------------------------------------------------
---- Begin Function Test: test_isis_cmds[show
---- Start Time: Thu Oct 03 16:39:15 2019
------------------------------------------------------------
--------------------------------------
---- Test Function completed at: Thu Oct 03 16:39:16 2019
---- Result:
--------------------------------------
------------------------------------------------------------
---- Begin Function Test: test_isis_cmds[show
---- Start Time: Thu Oct 03 16:39:16 2019
------------------------------------------------------------
--------------------------------------
---- Test Function completed at: Thu Oct 03 16:39:16 2019
---- Result:
--------------------------------------
------------------------------------------------------------
---- Begin Function Test: test_isis_cmds[show
---- Start Time: Thu Oct 03 16:39:16 2019
------------------------------------------------------------
--------------------------------------
---- Test Function completed at: Thu Oct 03 16:39:17 2019
---- Result:
--------------------------------------
感谢您的帮助。 铝
答案 0 :(得分:1)
您可以在conftest.py中定义必须位于PWD或系统路径中的钩子
PFA示例测试代码以检查输入的数字是否为正
test.py
import pytest
@pytest.mark.parametrize("number",[1,3,0,-1])
def test_hooks(number):
assert int(number) >= 0
conftest.py->具有挂钩逻辑来捕获要记录的结果
import pytest
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
log_file = "test.log"
outcome = yield
result = outcome.get_result()
if result.when == "call":
try:
with open(log_file, "a") as f:
f.write(result.nodeid + " " + result.outcome+ " " + str(result.duration)
except Exception as e:
print("Error", e)
pass
代码段中的变量结果是包含运行详细信息的TestReport对象
示例日志文件输出
test.py::test_hooks[1] passed 0.000999927520752
test.py::test_hooks[3] passed 0.000999927520752
test.py::test_hooks[0] passed 0.00100016593933
test.py::test_hooks[-1] failed 0.00100016593933
您可以根据需要修改挂钩逻辑。这可以是pytest
中对象引用的便捷链接https://docs.pytest.org/en/latest/reference.html