473,795 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_f iles is a list of file paths
non_existent_fi les = [ x for x in self.universe_f iles if
not os.path.isfile( x) ]
if non_existent_fi les:
raise Files_dont_exis tError, non_existent_fi les

where Files_dont_exis tError 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 1295
* Brian van den Broek wrote:
The relevant part of the validation method code looks like:

# self.universe_f iles is a list of file paths
non_existent_fi les = [ x for x in self.universe_f iles if
not os.path.isfile( x) ]
if non_existent_fi les:
raise Files_dont_exis tError, non_existent_fi les

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
"THISDIRECTORYC ANTPOSSIBLYEXIS TANDIFITDOESYOU RENUTS" 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
13462
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 comments on past experience, research articles, comments on the matter would be much appreciated. I suspect something like C would be the best based on comments I received from the VB news group. Thanks for the help in advance James Cameron
0
1778
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 this method? If so, what the heck is it called? Do you think it would work in other projects, or did we just luck out? Jonathan Allen *****************
0
377
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 08:06:31 -0700 I've written a web application that uses the M.P.E.D namespace to do all of its data layer tasks (and a nifty bit of code M.P.E.D is too!). However, unit-testing is proving a bit of a problem, as the database factory complains...
5
5404
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 program than there are in C, and it seems somebody laid a bet on the original development team to prove that this is the case. ;-( Unfortunately, I have no contact with the original programmers, who live and work on a different continent. In...
2
1671
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 the code is. Does anybody know of software that will simulate modem speed over a DSL connection -- i.e., temporarily slow down the speed of my connection, or the speed at which the browser uses the connection? (I author on a Mac but test on a PC...
6
2652
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 have something like stack size growing enormously and then saying you can't access ,so a segfault ? It would be helpful if someone can run the code and give me the output. It takes a long time on my PC.
1
5437
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 create that test environment.... then its will be a big task by itself. Instead one possible solution I can think of is to use UI and set up the test environment and back up the database either as MS Access database or text(CVS) files and...
24
2529
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 you ensure that the code will work correctly in the future? Short version:
4
1942
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
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10437
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10164
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.