python - 如何在 pytest 中检索功能测试结果以写入我自己的日志文件
问题描述
我已经研究/学习了几天,但似乎无法弄清楚。我要做的就是检索每个测试函数的状态,这样我就可以写出自己的日志文件。
我很确定我需要的部分是 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 方法写入的自定义输出日志文件的片段。你会注意到 Result: 是空的。我相信我想要的是填写 Result: with 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:
--------------------------------------
谢谢你的帮助。铝
解决方案
您可以在 conftest.py 中定义挂钩,该挂钩必须驻留在 PWD 或系统路径中
PFA 示例测试代码以检查输入数字是否为正
测试.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
推荐阅读
- sql - 如果该月没有条目,如何选择该月 SQL 的最后一天
- python - 删除python中特定范围之外的日期
- android - java.lang.RuntimeException:无法反序列化对象。期望一个列表,但得到一个类 java.lang.String(在字段“图像”中找到)
- php - 如何将一些警报或消息发送到另一个 php 文件
- python-3.x - Python 类型注解减慢代码速度
- ios - 共享扩展代码未获取 YouTube 应用 URL
- powershell - Powershell不会elseif
- azure - Get-AzRoleAssignment 在 Azure Runbook 中引发 Microsoft.Rest.Azure.CloudException
- java - 使用 Spring WebSecurityConfigurerAdapter 时如何保持我的 Elastic Beanstalk 实例健康?
- postgresql - 使用 pyarrow 将熊猫数据框批量插入到 postgresql