Thursday, January 9, 2014

From nose to testr: More Flexible Test Discovery

testrepository requires a results stream in subunit format, and the python implementation of that is testtools. testtools uses the standard unittest.TestLoader. This loader will let you discover tests from one specific directory, or run tests from multiple fully specified test modules. I am accustomed to nose, which lets you specify an arbitrary number of directories and modules, so I modified the TestLoader to bend it to my will.

import os
import unittest

class TestLoader(unittest.TestLoader):
    """Test loader that extends unittest.TestLoader to:

    * support names that can be a combination of modules and directories
    """

    def loadTestsFromNames(self, names, module=None):
        """Return a suite of all tests cases found using the given sequence
        of string specifiers. See 'loadTestsFromName()'.
        """
        suites = []
        for name in names:
            if os.path.isdir(name):
                top_level = os.path.split(name)[0]
                suites.extend(self.discover(name, top_level_dir=top_level))
            else:
                suites.extend(self.loadTestsFromName(name, module))
        return self.suiteClass(suites)

Then it just became a matter of borrowing from the subunit runner script and instantiating SubunitTestProgram with the new loader.

SubunitTestProgram(module=None, argv=sys.argv, testRunner=SubunitTestRunner,
        stdout=sys.stdout, testLoader=TestLoader())

No comments:

Post a Comment