intertwingly

It’s just data

Unbundling Pirate Tests


Pirate's unittests were structured in the conventional way: one class with a large number of test_* methods, each containing a single test case.  One of my first changes to Pirate was to unbundle these into separate files.  I did this using a trick I picked up from Mark Pilgrim, namely to create a loop which dynamically created test_* methods, one per file, thus:

# For every test/*/filename.py file, create a test_filename method
for test in sys.argv[1:] or glob("test/*/*.py"):
    testName = "test_" + os.path.splitext(os.path.split(test)[1])[0]
    testFunc = lambda self, test=test: self.runTest(test)
    testFunc.__doc__ = testName
    instanceMethod = new.instancemethod(testFunc, None, PirateTest)
    setattr(PirateTest, testName, instanceMethod)