从Python模块中的nosetests获取测试结果

3 投票
2 回答
938 浏览
提问于 2025-04-18 10:13

我正在运行一个模块,它会用计时器来执行nosetests,代码大概是这样的:

import nose
from nosetimer import plugin
from collections import defaultdict
import time
import pandas as pd

plugin = plugin.TimerPlugin()
plugin.enabled = True
plugin.timer_ok = 1000
plugin.timer_warning = 2000
plugin.timer_no_color = False
logList = defaultdict(list)

nose.run(plugins=[plugin])
result = plugin._timed_tests
for test in result:
    logList[test].append(result[test])

我想知道有没有办法把每个测试的名称和它的结果(通过、失败或错误)对应起来,像这样:

{
'example.file.path.test1': 'pass',
'example.file.path.test2': 'pass',
'example.file.test3': 'fail',
'example.file.test4': 'pass',
'example.file.path2.test5': 'error',
'example.file.path2.test6': 'pass'
}

但是我不想通过读取标准输出(stdout)来获取这些信息。换句话说,nose有没有地方可以存储这些信息?我已经看了好几个小时的文档和代码,但还是没有找到答案,所以我觉得我可能漏掉了什么。

2 个回答

2

你还可以利用 nosetests --with-xunit 这个命令(nose.plugins.xunit.Xunit)来生成一个测试结果的xml文件。然后你可以很简单地解析这个xml文件,提取你想要的数据。

2

这些数据是可以获取的,但我记得的唯一方法就是使用nose插件接口来自己写一个插件。不过,插件其实并不复杂,尤其是像这种情况。你需要用到通过、失败/错误和开始测试的钩子,以及test.address(),这样才能让它正常工作,如果我没记错的话。

撰写回答