I currently have a test class with a setUp method for creating a temp file and a tearDown method for deleting a temp file.

However, only some of the the test_ methods in that class require this functionality.

Is possible to either ignore those 2 methods for the test cases which do not require it or to only call those 2 methods for test cases which do require it?

It feels a bit 'wrong' to call these 2 methods for every test even if the temp file is not getting used in each one.

3 Answers 3


It feels a bit 'wrong' to call these 2 methods for every test even if the temp file is not getting used in each one.

So the code will be simpler when you let the temp file beeing created and deleted in all cases? And I guess the performance hit is negligible? Then what you are trying is just a form of premature optimization. Don't do this (at least, not as long the performance differerence has no reasonable impact on your workflow).

  • you make a very good point, they are only tests after all and speed is not an issue. the code is easier to read this way. thank you
    – Cybran
    Mar 13, 2014 at 11:56
  • Not really; this is just a case of not knowing that you can group your tests. Mar 13, 2014 at 11:56
  • @MartijnPieters: IMHO its only a (weak) sign that it might be a good idea to group the tests - but without actually knowing the tests, this is hard to tell.
    – Doc Brown
    Mar 13, 2014 at 12:00
  • I can always refactor quite easily later if speed becomes an issue
    – Cybran
    Mar 13, 2014 at 12:02
  • @Cybran: you got me ;-) commonsense4commonpeople.net/2009/11/…
    – Doc Brown
    Mar 13, 2014 at 12:10

Just put those specific tests into a new TestCase class:

class TestsWithTemporaryFile(TestCase):
    def setUp(self):
         # set up temporary file

    def tearDown(self):
         # clean up temporary file

    def test_foo(self):
         # ...

    def test_bar(self):
         # ...

class TestsWithoutTemporaryFile(TestCase):
    def test_spam(self):
         # ...

    def test_eggs(self):
         # ...

The test runner will find both test cases; nothing says you need to put all your tests into one TestCase.


Martijn Pieters' solution of splitting tests by class does not always work for me because I split tests by criteria other than what setUp and tearDown they need.

So for this kind of situation I typically use a context manager like this:

import contextlib
def temp_dir():
    tmpdir = tempfile.mkdtemp(dir="whatever")
        yield tmpdir

And then just use it where I need it:

def test_whatever(self):
    with temp_dir() as whatever:

I create a temporary directory to stuff all of my temporary files into. This way the directory gets a random name but the files in it have constant names. I find it easier to manage if I need to perform diagnosis on the temporary files. I just need to get the name of the temporary directory instead of having to figure out the name of multiple files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.