473,323 Members | 1,570 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,323 software developers and data experts.

"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 3735
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.tearDown()

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**********@gmail.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**********@invalid.invalidwrote:
"killkolor" <ga**********@gmail.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.dyndns.org R'lyeh wgah'nagl fhtagn!
Mar 23 '07 #7
Jorgen Grahn <gr********@snipabacken.dyndns.orgwrote:
On 23 Mar 2007 12:19:15 GMT, Duncan Booth
<du**********@invalid.invalidwrote:
>"killkolor" <ga**********@gmail.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...@gmail.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
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...
10
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: ...
5
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.