473,396 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
Jul 18 '05 #1
3 1281
* 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/> #
Jul 18 '05 #2
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

Jul 18 '05 #3
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
"THISDIRECTORYCANTPOSSIBLYEXISTANDIFITDOESYOURENUT S" ought to do OK.

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
0
by: Jonathan Allen | last post by:
We have found that our method of testing does not match traditional text-book methodologies. I decided to write a short white paper on it so that I could get your opinions. Does anyone else use...
0
by: Mairead O' Donovan | last post by:
From: davidkeaveny@gmail.com (CtrlAltDel) Newsgroups: microsoft.public.dotnet.datatools Subject: Unit-testing applications that use the Microsoft.Patterns.EnterpriseLibrary.Data Date: 15 Apr 2005...
5
by: Alan Mackenzie | last post by:
I've recently moved onto a C++ project with a large number of directories (several hundred) containing an even larger number of C++ source files. There are vastly more ways in C++ to obfuscate a...
2
by: Lawrence San | last post by:
I'm trying to test some simple JavaScript meant to speed up the display of my Web pages for readers using modems, but I have a fast DSL connection and I'm having trouble visualizing how effective...
6
by: Niklaus | last post by:
Hi, Can someone point out what is wrong with this code ? How can i make it better optimize it. When run it gives me seg fault in linux. But windows it works fine(runs for a long time). Do we...
1
by: Kiran | last post by:
Hi, I am looking for a way to setup my databases for my unit test case to run. basically my class needs some test environment to me available before I can test it, and if I have to write code to...
24
by: David | last post by:
Hi list. What strategies do you use to ensure correctness of new code? Specifically, if you've just written 100 new lines of Python code, then: 1) How do you test the new code? 2) How do...
4
by: David | last post by:
Hi list. Do test-driven development or behaviour-driven development advocate how to do higher-level testing than unit testing? types of testing: unit integration system
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.