how to make nose to log output of cases to separat

2019-08-11 00:52发布

问题:

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

回答1:

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.

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))

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.