1
class MyTestCaseA(TestCase):
    def setUp(self):
        # some setup here...

    def test_case(self):
        # some test

class MyTestCaseB(TestCase):
    def setUp(self):
        # some setup here...

    def test_case(self):
        # some test

I would need to share data between MyTestCaseA and MyTestCaseB. In my case, I want to share the path to a folder created for each execution of the test (through "manage.py test myapp"). In my case, I will take screenshots of the browser (using selenium) and I want to store all the screenshots taken in my various test cases in the same temporary folder.

If I create a temporary folder in the SetUp function, then each test case will have a different temporary folder.

4

1 に答える 1

1

以下を試してください:

class SharedTempDir(object):
    def prepare(self):
        if not hasattr(SharedTempDir, 'tempdir'):
            # This part will be executed only once.
            SharedTempDir.tempdir = .... make temporary directory ...

class MyTestCaseA(TestCase, SharedTempDir):
    def setUp(self):
        SharedTempDir.prepare(self)
        ...
    def test_blah(self):
        ... use self.tempdir ... or SharedTempDir.tempdir ...

class MyTestCaseB(TestCase, SharedTempDir):
    def setUp(self):
        SharedTempDir.prepare(self)
        ...
于 2013-09-13T13:38:00.787 に答える