473,960 Members | 5,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"finally" for unit test

hi!

I have a unittest framework that tests a single function that in turn
works with files (takes input and outputs in the same file, no return
values).
In the unittest class I assign a member with all the names of my
testfiles and a testdirectory. The tests call the function (which
opens and writes to the file) and then opens the file to see if
everything is in order. The problem now is that after each testrun I
have to copy "fresh" files into the testdirectory, since of course the
function already run on all the files and made the changes. So I
implemented a buffering in the unittest functions: buffer the file,
call the function, make the test, write the buffered file back. This
works fine for unittests that do not fail. If a unittest fails though
the function stops and the writing back is never done. Is there
something like a finally for unittest functions? Or could I use
another approach to buffer and write back my files (for each unittest
function)?
thanks!
gabriel

Mar 23 '07 #1
8 3801
On Fri, 23 Mar 2007 04:18:59 -0700, killkolor wrote:
The problem now is that after each testrun I have to copy "fresh" files
into the testdirectory, since of course the function already run on all
the files and made the changes. So I implemented a buffering in the
unittest functions: buffer the file, call the function, make the test,
write the buffered file back. This works fine for unittests that do not
fail. If a unittest fails though the function stops and the writing back
is never done. Is there something like a finally for unittest functions?
Or could I use another approach to buffer and write back my files (for
each unittest function)?
A simple approach would be to copy the test files *before* running the
tests, instead of after. That way it doesn't matter if the tests fail or
not: the next run will simply replace the test files with known good
copies, regardless.

--
Steven.

Mar 23 '07 #2
killkolor wrote:
I have a unittest framework that tests a single function that in turn
works with files (takes input and outputs in the same file, no return
values).
In the unittest class I assign a member with all the names of my
testfiles and a testdirectory. The tests call the function (which
opens and writes to the file) and then opens the file to see if
everything is in order. The problem now is that after each testrun I
have to copy "fresh" files into the testdirectory, since of course the
function already run on all the files and made the changes. So I
implemented a buffering in the unittest functions: buffer the file,
call the function, make the test, write the buffered file back. This
works fine for unittests that do not fail. If a unittest fails though
the function stops and the writing back is never done. Is there
something like a finally for unittest functions?
TestCase.tearDo wn()

http://docs.python.org/lib/testcase-....html#l2h-5002
Or could I use
another approach to buffer and write back my files (for each unittest
function)?
Rather than restoring the file I would just delete it and use

TestCase.setUp( ) to make a fresh copy.

Peter
Mar 23 '07 #3
"killkolor" <ga**********@g mail.comwrote:
I have a unittest framework that tests a single function that in turn
works with files (takes input and outputs in the same file, no return
values).
I would want to split that function into two:

a) one which does the processing, but not working with a file,
b) and one which handles reading and calls the processing function and then
writes the file. The function it calls would be passed in as an optional
parameter and defaults to function (a)

Then you need two sets of tests:

one to test the action of reading data from a file and then rewriting the
output in-place, but you just pass in a mock function for the processing
which can assert that it saw the expected inputs.

and as many tests as you want for the processing, but no annoying files to
get in the way.

If you really cannot have the processing without reading from a file, then
let it take two files, input and output as parameters, and have the (b)
function choose between calling it with separate input/output files or
creating a temporary file which is renamed to replace the original on
completion. Then at least you can read directly from the test inputs
without ever needing to write them.
Mar 23 '07 #4
I went with the TestCase.setUp( ) function.
Thanks a lot!

Mar 23 '07 #5
gabrielIs there something like a finally for unittest functions?

TestCase instances have setUp() and tearDown() methods:

http://docs.python.org/dev/lib/testcase-objects.html

Skip
Mar 23 '07 #6
On 23 Mar 2007 12:19:15 GMT, Duncan Booth <du**********@i nvalid.invalidw rote:
"killkolor" <ga**********@g mail.comwrote:
>I have a unittest framework that tests a single function that in turn
works with files (takes input and outputs in the same file, no return
values).

I would want to split that function into two:

a) one which does the processing, but not working with a file,
Better to make it work with "a file", in the sense that it works with
file-like objects so you can feed and leech it using StringIO.
b) and one which handles reading and calls the processing function and then
writes the file. The function it calls would be passed in as an optional
parameter and defaults to function (a)
/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyn dns.org R'lyeh wgah'nagl fhtagn!
Mar 23 '07 #7
Jorgen Grahn <gr********@sni pabacken.dyndns .orgwrote:
On 23 Mar 2007 12:19:15 GMT, Duncan Booth
<du**********@i nvalid.invalidw rote:
>"killkolor" <ga**********@g mail.comwrote:
>>I have a unittest framework that tests a single function that in
turn works with files (takes input and outputs in the same file, no
return values).

I would want to split that function into two:

a) one which does the processing, but not working with a file,

Better to make it work with "a file", in the sense that it works with
file-like objects so you can feed and leech it using StringIO.
I'm assuming, perhaps wrongly, that something which takes input from a file
and produces output into the same file is doing something beyond the simple
interface supported by StringIO. So I was guessing either that something
like FileInput is involved, or seeking and modifying in-place.

Anyway, the principle is the same: you make sure no function does more than
one job, and you make sure that all functions are testable independently.
Mar 23 '07 #8
On Mar 23, 5:18 am, "killkolor" <gabriel.h...@g mail.comwrote:
I have .. a single function that ..
works with files (takes input and outputs in the same file, no return
values).
That function could cause problems. If your function reads in the
whole file, modifies the data, and then overwrites the file, what
would happen if something unforeseen happened and your program crashed
after writing one line to the file? All the data in memory would go
poof! and your file would contain 1 line it--effectively erasing the
entire contents of the file.

Mar 23 '07 #9

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

Similar topics

77
5418
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for the moment. I'd be *very* grateful if people with any interest in multi-threading would read it (even just bits of it - it's somewhat long to go through the whole thing!) to check for accuracy, effectiveness of examples, etc. Feel free to mail...
10
6118
by: Joe Thompson | last post by:
Hi, This is probably a simple question but I'm curious. What is the purpose of the "finally" statement? There must be a good reason to use it that I'm just not getting... How is this: method1 {
5
1747
by: Max Ivanov | last post by:
Hi all! When and where I should use try-except-finally statement? What is the difference between: -------- try: A... except: B.... finally: C...
0
10111
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
11351
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10834
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
10028
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
8398
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...
0
7559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
5090
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
4677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3695
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.