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

Jeremy Bowers jerf at jerf.org
Mon Apr 4 17:26:50 EDT 2005


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... :-)



More information about the Python-list mailing list