1

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

zx_wing
  • 1,918
  • 3
  • 26
  • 39

1 Answers1

5

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.
dbn
  • 13,144
  • 3
  • 60
  • 86
  • 1
    May I ask why you've used `success = test.passed != False`? can test.passed be something other than a boolean? (One might consider using `success = test.passed` instead) – Alex L Jan 03 '13 at 02:35
  • @AlexL I've reworded it slightly, let me know it that is more clear. – dbn Jan 03 '13 at 02:41
  • I'm reading the [testid plugin, afterTest()](https://github.com/nose-devs/nose/blob/master/nose/plugins/testid.py#L279), which comments "# None means test never ran, False means failed/err" – Alex L Jan 03 '13 at 02:54
  • perhaps you could use something like `status = {False: 'Failed', None: 'Not run', True: 'Passed'}`, then `status.get(test.passed)`? – Alex L Jan 03 '13 at 03:00
  • My issue was that I ran a successful test and it had test.passed = None. I'll check it out again and change the description if it works out. – dbn Jan 03 '13 at 04:19
  • thanks for detailed explanation. I will proceed to the plugin way as nose doesn't support it naturally. – zx_wing Jan 13 '13 at 03:32
  • @AlexL - I've changed it as you suggested. I don't know *what* was going on with my first test case. – dbn Feb 05 '13 at 07:07