testing -- what to do for testing code with behaviour dependant uponwhich files exist?

Discussion in 'Python' started by Brian van den Broek, Apr 2, 2005.

  1. Hi all,

    I'm just starting to employ unit testing (I'm using doctest), and I am
    uncertain how to handle writing tests where the behaviour being tested
    is dependant on whether certain file paths point to actual files.

    I have a class which takes, in its __init__, a list of file paths to
    process. The class has a method to validate that the paths passed in
    are appropriate ones for the class. One portion of the validation code
    ensures that the passed in file paths actually exist.

    The relevant part of the validation method code looks like:

    # self.universe_files is a list of file paths
    non_existent_files = [ x for x in self.universe_files if
    not os.path.isfile(x) ]
    if non_existent_files:
    raise Files_dont_existError, non_existent_files

    where Files_dont_existError is a custom exception with an informative
    error message nicely printing a listing of all the file paths which
    didn't point to extant files.

    I can test the custom error class just fine, but I don't see how to
    test the validation method itself.

    My problem is that I want to test how it behaves both when it is sent
    only existing file paths, and when it is sent at least 1 non-existent
    one. But it seems like that in order to test that, I have to know in
    advance details of what files exist on the system running the tests.
    And, it won't do to pick file paths with the right properties with
    respect to my computer, as I am writing this code to share with
    someone else, and so I'd have to know details of his file system, too.
    (If it matters, I am using Python 2.4.1 on windows, and I am sending
    the code to someone running Python 2.2 on Linux.)

    So, how does one handle such cases with tests?

    Thanks for any suggestions. Best,

    Brian vdB
     
    Brian van den Broek, Apr 2, 2005
    #1
    1. Advertising

  2. Re: testing -- what to do for testing code with behaviour dependant upon which files exist?

    * Brian van den Broek wrote:

    > The relevant part of the validation method code looks like:
    >
    > # self.universe_files is a list of file paths
    > non_existent_files = [ x for x in self.universe_files if
    > not os.path.isfile(x) ]
    > if non_existent_files:
    > raise Files_dont_existError, non_existent_files
    >
    > I can test the custom error class just fine, but I don't see how to
    > test the validation method itself.


    The logic is simple -- you don't want to test os.path.isfile, so mock it.
    Just encapsulate the os.path.isfile call in an own method, which can be
    overridden by your test.

    nd
    --
    # André Malo, <http://pub.perlig.de/> #
     
    =?UTF-8?B?QW5kcsOp?= Malo, Apr 2, 2005
    #2
    1. Advertising

  3. Re: testing -- what to do for testing code with behaviour dependant upon which files exist?

    Can't you use the tempfile module to generate unique names for
    non-existent files and directories? Take a look at
    http://www.python.org/doc/lib/module-tempfile.html -- it works on all
    supported platforms.

    Grig
     
    Grig Gheorghiu, Apr 3, 2005
    #3
  4. Re: testing -- what to do for testing code with behaviour dependant upon which files exist?

    On Sat, 02 Apr 2005 15:30:13 -0500, Brian van den Broek wrote:
    > So, how does one handle such cases with tests?


    When I had a similar situation, I created a directory for testing that was
    in a known state, and tested on that. If you can test based on a relative
    directory, that should work OK.

    Non-existant paths shouldn't be too hard to come up with; hardcoding a
    constant relative dir of
    "THISDIRECTORYCANTPOSSIBLYEXISTANDIFITDOESYOURENUTS" ought to do OK.
     
    Jeremy Bowers, Apr 3, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Tonelli

    image dependant on resolution

    Michael Tonelli, Jul 21, 2004, in forum: HTML
    Replies:
    11
    Views:
    701
    Andy Dingley
    Jul 23, 2004
  2. KiwiBrian

    Dependant files query

    KiwiBrian, Oct 15, 2004, in forum: HTML
    Replies:
    2
    Views:
    475
    Toby Inkster
    Oct 16, 2004
  3. Bas
    Replies:
    2
    Views:
    5,957
    Roedy Green
    May 4, 2006
  4. Jeff Smith
    Replies:
    1
    Views:
    321
    Jeff Smith
    Jul 29, 2004
  5. Brian van den Broek
    Replies:
    2
    Views:
    408
    Brian van den Broek
    Apr 5, 2005
Loading...

Share This Page