如何使案件记录输出到单独的文件? [英] how to make nose to log output of cases to separate files?

查看:98
本文介绍了如何使案件记录输出到单独的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用鼻子来运行一堆测试用例. 我想将每种情况的输出记录到单独的文件中,并想知道每种情况的结果[成功/失败].不幸的是,我不知道该怎么做.有人可以提供一些线索吗?谢谢

I am using nose to run a bunch of test cases. I would like to record output of each case to separate files, and to know result[success/failure] of each case. unfortunately, I can not figure out how to do it with nose. can anybody provide some clues? thank you

推荐答案

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

Firstly, this sounds like unusual usage, and may indicate that you should rethink your testing scheme.

我可以想到几种解决方法.最简单的方法是让每个测试日志本身,而不是由鼻子替您完成.如果您只有几个测试,或者只想记录几个测试的结果,那么肯定是这样做的方法.

I can think of a couple of ways to address this. The simplest would be to have each test log itself instead of having nose do it for you. If you have only a few tests, or only care to log the results of a few tests, this would definitely be the way to do it.

更复杂,更通用的方法是写鼻子插件,用于记录每次测试完成时的结果.为此,您需要编写一个实现afterTest()方法的插件.

A more complex and general approach would be to write a nose plug-in that records the result of each test as it finishes. To do this, you'd want to write a plug-in that implements the afterTest() method.

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.一些注意事项:

This will append to a logfile named after your specific test a datestamp and True for success/False for failure. A couple of notes:

  • See the commented-out line for an example of the results that are printed.
  • This plug-in will have to be registered by nose; see the docs.
  • Once registered, the plug-in will have to be enabled; use the commandline option --with-separatereports (automagically generated based on the plug-in name).
  • This will be pretty slow since it is touching files for every test. You may want an sqlite DB that is open or something like that.

这篇关于如何使案件记录输出到单独的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆