python - 如何让 nose 将案例输出记录到单独的文件中?

标签 python testing nose

我正在使用 nose 运行一堆测试用例。 我想将每个案例的输出记录到单独的文件中,并了解每个案例的结果[成功/失败]。不幸的是,我不知道如何用 Nose 来做。谁能提供一些线索?谢谢

最佳答案

首先,这听起来像是不寻常的用法,可能表明您应该重新考虑您的测试方案。

我可以想出几种方法来解决这个问题。最简单的方法是让每个测试记录自己而不是让 nose 为你做。如果您只有几个测试,或者只关心记录几个测试的结果,这绝对是实现它的方法。

一个更复杂和通用的方法是 write a nose plug-in记录每个测试完成时的结果。为此,您需要编写一个实现 afterTest() 方法的插件。

from nose.plugins import Plugin
import datetime

class SeparateReports(Plugin):
  "Log the results of each test into a separate file."
  def afterTest(self, test):
    logname = test.id() + '.log'
    success = test.passed
    date = datetime.datetime.now()
    # print logname, datetime.datetime.now(), success
    with open(logname, 'a') as log:
        log.write("%s: %s\n" % (date, success))

这将附加到以您的特定测试命名的日志文件中,一个日期戳和 True 表示成功/False 表示失败。一些注意事项:

  • 有关打印结果的示例,请参阅注释掉的行。
  • 此插件必须是 registered by nose; see the docs .
  • 注册后,必须启用插件;使用命令行选项 --with-separatereports(根据插件名称自动生成)。
  • 这会很慢,因为它会为每个测试接触文件。您可能需要一个打开的 sqlite 数据库或类似的东西。

关于python - 如何让 nose 将案例输出记录到单独的文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14077299/

相关文章:

python - 将嵌套字典值转换为单个字符串

python - Openshift绑定(bind)TCP端口

python - Unicode解码错误: 'utf8' codec can't decode

testing - undefined 不是对象(评估 'config.forEach' )-Angular2 路由测试

windows - Go test编译成功,但是go build没有

python - 强制 nosetests 在以下划线开头的模块中查找 doctests

python - 将重叠的数值范围合并为连续的范围

testing - 如何在单元测试中测试函数的输出 (stdout/stderr)

python - Nosetest 和 unittest.expectedFailures

python - 在 python 中使用 nose 进行并行化测试