[Python-Dev] Unit Test Guide

Virgil Dupras hsoft at hardcoded.net
Thu Feb 21 17:14:16 CET 2008

On 2/21/08, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Virgil Dupras wrote:
>  > On 2/21/08, Virgil Dupras <hsoft at hardcoded.net> 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?


More information about the Python-Dev mailing list