[Python-Dev] Unit Test Guide
Brett Cannon
brett at python.org
Thu Feb 21 22:14:21 CET 2008
On Thu, Feb 21, 2008 at 7:43 AM, Giampaolo Rodola' <gnewsg at gmail.com> wrote:
> 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')
> ...
>
test.test_support contains all of the various test-helping code that
is not in unittest or doctest.
>
>
> > 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.
A more appropriate doc is on the todo list to write.
-Brett
More information about the Python-Dev
mailing list