سؤال

I am unit testing mercurial integration and have a test class which currently creates a repository with a file and a clone of that repository in its setUp method and removes them in its tearDown method.

As you can probably imagine, this gets quite performance heavy very fast, especially if I have to do this for every test individually.
So what I would like to do is create the folders and initialize them for mercurial on loading the class, so each and every unittest in the TestCase class can use these repositories. Then when all the tests are run, I'd like to remove them. The only thing my setUp and tearDown methods then have to take care of is that the two repositories are in the same state between each test.

Basically what I'm looking for is a python equivalent of JUnit's @BeforeClass and @AfterClass annotations.

هل كانت مفيدة؟

المحلول

I've now done it by subclassing the TestSuite class, since the standard loader wraps all the test methods in an instance of the TestCase in which they're defined and puts them together in a TestSuite. I have the TestSuite call the before() and after() methods of the first TestCase. This of course means that you can't initialize any values to your TestCase object, but you probably want to do this in your setUp anyway.

The TestSuite looks like this:

class BeforeAfterSuite(unittest.TestSuite):
    def run(self, result):
        if len(self._tests) < 1:
            return unittest.TestSuite.run(self, result)

        first_test = self._tests[0]
        if "before" in dir(first_test):
            first_test.before()
        result = unittest.TestSuite.run(self, result)
        if "after" in dir(first_test):
            first_test.after()
        return result

For some slightly more finegrained control I also created the custom TestLoader which makes sure the BeforeAfterSuite is only used to wrap test-method-TestCase objects in, which looks like this:

class BeforeAfterLoader(unittest.TestLoader):
    def loadTestsFromTestCase(self, testCaseClass):
        self.suiteClass = BeforeAfterSuite
        suite = unittest.TestLoader.loadTestsFromTestCase(self, testCaseClass)
        self.suiteClass = unittest.TestLoader.suiteClass
        return suite

Probably missing here is a try/except block around the before and after which could fail all the testcases in the suite or something.

نصائح أخرى

from the Python unittest documentation :

setUpClass() :

A class method called before tests in an individual class run. setUpClass is called with the class as the only argument and must be decorated as a classmethod():

@classmethod
def setUpClass(cls):
...

New in version 2.7.

tearDownClass() :

A class method called after tests in an individual class have run. tearDownClass is called with the class as the only argument and must be decorated as a classmethod():

@classmethod
def tearDownClass(cls):
    ...
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top