![](/img/trans.png)
[英]How to write a pytest to test for opening two separate files in a single function
[英]How to retrieve function test result in pytest to write out to my own log files
我已经研究/学习了几天,但似乎无法弄清楚。 我要做的就是检索每个测试 function 的状态,这样我就可以写出自己的日志文件。
我很确定我需要的部分是 TestReport object、钩子 pytest_testreport_status 和 pytest_runtest_makereport。 我对 pytest 还很陌生,我只是不知道如何将这一切“粘合”在一起以完成这项工作。
这是一个简单的测试。 测试中的最后一条语句是 function 调用 (process_test_function_results),它处理测试结果并将结果写入日志文件。 我相信我想要的是将 TestReport object 传递给 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)
下面是自定义 output 日志文件的片段,该文件看起来是通过 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 object ,其中包含运行详细信息
示例日志文件 output
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
您可以根据您的要求修改挂钩逻辑。 This can be a handy link https://docs.pytest.org/en/latest/reference.html for object references in pytest
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.