Re: testing -- what to do for testing code with behaviour dependantupon which files exist?

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


  1. > "Brian van den Broek" <> wrote in message
    > news:...
    >
    >>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.


    Hi all,

    I had a busy weekend after posting, so didn't get a chance to
    follow-up until now. Thanks to all who responded.

    Python's my first real language, and I am a mere hobbyist, so the
    suggestions for search terms provided by Terry (and implicitly by
    Andre who suggested a mock strategy) are most welcome! Sometimes the
    hardest part of googling is knowing what terms to search for.

    Grig suggested the tempfile module; I had rejected that initially, as
    the code I am testing displays an error message containing the bad
    file path, so I thought I needed to know the path name at time of
    coding the tests. But, I've since learned that doctest can be made to
    consider only leading portions of a line's content. So, this might
    well work for me. Thanks for reminding me of the approach.

    Jeremy suggested using a directory name akin to
    "C:\onlyanidiotwouldhavethisdirecotrynameonadrive". That is what I had
    settled on before I posted. Somehow it feels unhappy and inelegant.
    But, I'm a bit less uncomfortable with it seeing that others have done
    so, too.

    Anyway, thanks again for the suggestions :) Best to all,

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

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

    On Mon, 04 Apr 2005 17:02:20 -0400, Brian van den Broek wrote:
    > Jeremy suggested using a directory name akin to
    > "C:\onlyanidiotwouldhavethisdirecotrynameonadrive". That is what I had
    > settled on before I posted. Somehow it feels unhappy and inelegant.
    > But, I'm a bit less uncomfortable with it seeing that others have done
    > so, too.


    To be clear, I would actually suggest
    "onlyanidiotwouldhavethisdirecotrynameonadrive"... note the lack of C:\,
    which would be platform specific, as would any other root specification.
    Take advantage of the fact that every system I know of makes relative
    directories easy, and also note you can work out what directory the
    "current file" is in with a combination of __file__ and os.getcwd() (and
    that while that won't work if someone changes the working directory,
    that's bad form and IIRC breaks some other things in Python as well, so
    don't do that).

    Making it a relative directory may make it look just as bad, but it is in
    some sense somewhat less inelegant; at that point, if someone is creating
    that directory in the test directory of your app, they're just fooling
    with you, and you don't really have to worry about people who maliciously
    make your unit tests fail under most circumstances... :)
    Jeremy Bowers, Apr 4, 2005
    #2
    1. Advertising

  3. Jeremy Bowers said unto the world upon 2005-04-04 17:26:
    > On Mon, 04 Apr 2005 17:02:20 -0400, Brian van den Broek wrote:
    >
    >>Jeremy suggested using a directory name akin to
    >>"C:\onlyanidiotwouldhavethisdirecotrynameonadrive". That is what I had
    >>settled on before I posted. Somehow it feels unhappy and inelegant.
    >>But, I'm a bit less uncomfortable with it seeing that others have done
    >>so, too.

    >
    >
    > To be clear, I would actually suggest
    > "onlyanidiotwouldhavethisdirecotrynameonadrive"... note the lack of C:\,


    Quite so. My apologies for mis-characterizing your suggestion. And
    thanks for the additional information that mis-characterization
    provoked :)

    Best,

    Brian vdB

    > which would be platform specific, as would any other root specification.
    > Take advantage of the fact that every system I know of makes relative
    > directories easy, and also note you can work out what directory the
    > "current file" is in with a combination of __file__ and os.getcwd() (and
    > that while that won't work if someone changes the working directory,
    > that's bad form and IIRC breaks some other things in Python as well, so
    > don't do that).
    >
    > Making it a relative directory may make it look just as bad, but it is in
    > some sense somewhat less inelegant; at that point, if someone is creating
    > that directory in the test directory of your app, they're just fooling
    > with you, and you don't really have to worry about people who maliciously
    > make your unit tests fail under most circumstances... :)
    Brian van den Broek, Apr 5, 2005
    #3
    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. Y.S.
    Replies:
    3
    Views:
    984
    strajan
    Sep 17, 2003
  2. LT
    Replies:
    7
    Views:
    2,073
    Phlip
    Jul 25, 2004
  3. Brian van den Broek
    Replies:
    3
    Views:
    820
    Jeremy Bowers
    Apr 3, 2005
  4. mathieu

    Portable way of testing if a function exist

    mathieu, Dec 6, 2008, in forum: C Programming
    Replies:
    6
    Views:
    2,099
    David Thompson
    Dec 29, 2008
  5. Markus Mohr
    Replies:
    7
    Views:
    228
    Thomas 'PointedEars' Lahn
    Nov 28, 2003
Loading...

Share This Page