Quick spec from your Python tests

Using Python’s own unittest package, here’s a small script that can iterate over your test suite to output a small, quick, nice list of the tests in your application:

import unittest

loader = unittest.TestLoader()
tests = loader.loadTestsFromName('path.to.your.tests.package')
for test in tests._tests:
  print test._tests[0].__class__.__name__.replace("Test", "")
  for method in test._tests:
    print "  %s" % method._testMethodName.replace("test_", "").capitalize().replace("_", " ")

This would yield something like this:

Business
    Accounts have at least one entry
    Clerks cannot close accounts
Security
    Users can create new accounts
    Anonymous users cannot access private areas

Of course, you’ll get better results if you follow Google’s naming conventions for your tests… ;) This is not rspec (nor an alternative to it) but it might be useful to some of you.

Just as a reminder for Django users: you might need to

setenv DJANGO_SETTINGS_MODULE application.settings

or

export DJANGO_SETTINGS_MODULE=application.settings

in order to make the script work properly! At least I had to :)