[Python-Dev] Unit Test Guide

Giampaolo Rodola' gnewsg at gmail.com
Thu Feb 21 16:43:58 CET 2008


On 21 Feb, 12:30, "Virgil Dupras" <hs... at hardcoded.net> wrote:
> Hi devs,
>

> Specifically, I'd like to know about files managements in tests. Is
> every test expected to clean after itself, or is there an automatic
> cleanup mechanism in place?

I have usually seen a lot of tests implemented like this:


from test.test_support import TESTFN, unlink
import unittest

class TestCase(unittest.TestCase):

    def setUp(self):
        self.file = None

    def tearDown(self):
        if self.file is not None:
            self.file.close()
        unlink(TESTFN)

    def test_something(self):
        self.file = open(TESTFN, 'r')
        ...


> Even more specifically, I'd like to create
> a test for the proposed patch inhttp://bugs.python.org/issue2127so I
> need to create a subdir with non-ascii character in it, then connect
> to a db in it. So, do I need to do the cleanup in the test? Is there a
> special path I can write to that will automatically be cleaned up?

I don't think so.
You could create a directory in setUp method by using tempfile.mkdtemp
and then remove it in tearDown.

> I guess I could find the answer in regrtest.py, but frankly, this unit
> is a little bit overwhelming.
>
> If there is no guide, am I the only one to think it would be a good
> idea to have one (Yeah, I could volunteer to write it)?

Don't know whether Lib/test/readme.txt could be considered a guide but
it contains a lot of useful information for developers.


Hope this helps a little


More information about the Python-Dev mailing list