Re: [Python-Dev] Unit Test Guide

2008-02-21 Thread Giampaolo Rodola'
On 21 Feb, 12:30, Virgil Dupras [EMAIL PROTECTED] 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
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Unit Test Guide

2008-02-21 Thread Nick Coghlan
Virgil Dupras wrote:
 On 2/21/08, Virgil Dupras [EMAIL PROTECTED] wrote:
 Hi devs,

  Being a python dev newbie, I look in http://www.python.org/dev/ for
  some guide to write unit tests in python and I can't find any.
  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? Even more specifically, I'd like to create
  a test for the proposed patch in http://bugs.python.org/issue2127 so 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? If
  not, wouldn't it be a good idea to have one?

The tempfile module provides some useful tools for making individual 
files that clean themselves up, but individual tests are currently 
pretty much left to fend for themselves as far as cleaning up temporary 
directories. This can be done using the setUp/tearDown methods of the 
test case (as Giampaolo described), or more directly using context 
managers (that latter is obviously only common in tests added for 2.6/3.0)

Something to consider would be to relocate the temp_dir() context 
manager from test_cmd_line_script [1] to test.test_support and use that. 
That context manager should be able to clean up for you no matter what 
you put in the temporary directory. The major downside of that approach 
is that test_support would end up depending on yet more modules being 
available for import (shutil and tempfile in this case), which isn't 
hugely desirable for test infrastructure). A way around that may be to 
guard the imports and define a dummy context manager that raises 
TestSkipped if either import fails.

(If you do come up with a patch to relocate temp_dir(), put it up as a 
separate tracker item and add me to the nosy list for it)

 Oops, nevermind I ended up finding it at
 http://docs.python.org/lib/module-test.html and
 http://docs.python.org/lib/module-test.testsupport.html. I didn't
 expect to find it in the Python documentation itself. Sorry. Might I
 suggest adding a link to it in http://www.python.org/dev/?

Sounds like a good idea to me too.

Cheers,
Nick.

[1] 
http://svn.python.org/projects/python/trunk/Lib/test/test_cmd_line_script.py


-- 
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
 http://www.boredomandlaziness.org
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Unit Test Guide

2008-02-21 Thread Virgil Dupras
On 2/21/08, Nick Coghlan [EMAIL PROTECTED] wrote:
 Virgil Dupras wrote:
   On 2/21/08, Virgil Dupras [EMAIL PROTECTED] wrote:
   Hi devs,
  
Being a python dev newbie, I look in http://www.python.org/dev/ for
some guide to write unit tests in python and I can't find any.
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? Even more specifically, I'd like to create
a test for the proposed patch in http://bugs.python.org/issue2127 so 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? If
not, wouldn't it be a good idea to have one?


 The tempfile module provides some useful tools for making individual
  files that clean themselves up, but individual tests are currently
  pretty much left to fend for themselves as far as cleaning up temporary
  directories. This can be done using the setUp/tearDown methods of the
  test case (as Giampaolo described), or more directly using context
  managers (that latter is obviously only common in tests added for 2.6/3.0)

  Something to consider would be to relocate the temp_dir() context
  manager from test_cmd_line_script [1] to test.test_support and use that.
  That context manager should be able to clean up for you no matter what
  you put in the temporary directory. The major downside of that approach
  is that test_support would end up depending on yet more modules being
  available for import (shutil and tempfile in this case), which isn't
  hugely desirable for test infrastructure). A way around that may be to
  guard the imports and define a dummy context manager that raises
  TestSkipped if either import fails.

  (If you do come up with a patch to relocate temp_dir(), put it up as a
  separate tracker item and add me to the nosy list for it)

What do you people think about this issue I just opened?

http://bugs.python.org/issue2156
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Unit Test Guide

2008-02-21 Thread Christian Heimes
Guido van Rossum wrote:
  I don't think so.
  You could create a directory in setUp method by using tempfile.mkdtemp
  and then remove it in tearDown.
 
 Specifically, clean it up with shutil.rmtree()

And make sure you have closed all files before you rmtree() the
directory. Otherwise the unit test *will* fail on Windows.

Christian

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com