[fpc-pascal] FPCUnit article/tutorial online.
Dean Zobec
dezobec at tin.it
Tue Oct 11 23:56:39 CEST 2005
Alan Mead ha scritto:
>>Hello,
>>
>>The Editor of Toolbox Magazine has allowed me to put an article
>>about FPCUnit online.
>>[...]
>>
>>
>
>I found the article and the discussion on this list very helpful.
>Thanks for writing it and making it available.
>
>These are probably stupid questions, but all the examples I've read
>about use silly tests like 1+1=2 or checking that a list is empty.
>I'm having trouble seeing how I would write tests for my code.
>
>
Take a look at some real world tests to see how they were written and
then try to write yours to make practice. When writing test you should
learn to identify the border conditions (strange cases that could give
problems to your code), you know, bugs generally live in the edges :)
like wrong input values, badly formatted data, values in excess of any
reasonable expectation, empty of missing values (null, 0, ""), unordered
values passed in input where the routine expects ordered items ... Ask
yourself some questions as Does the value conform to my expected format?
Is the value within a reasonable range of values? Is something happening
in the right order in time? Are there exactly enough parameters?
Some methods can be tested by applying their inverse: like the result of
y=sqrt(x) could be tested applying y^2 to see if it equals to x. To see
if an item was deleted from a list you perform a search after the deletion.
For some examples of more complex unit tests with fpcunit you can look
at the tests we wrote for the EOS framework: a sort of simple xml
object persistence framework that automatically saves the published
properties of objects and complex structures of objects to xml and
automatically recreates the objects from the xml text (it was used to
transport those objects over http from the server to the client and vice
versa)
Here you can find the code:
http://cvs.sourceforge.net/viewcvs.py/camelos/EOS/fpcEOS/
and here are the related tests:
http://cvs.sourceforge.net/viewcvs.py/camelos/EOS/fpcEOS/tests/
>I have an ugly little hack that reads some logged data and counts
>certain things. The code is fragile so I'd love to add unit
>testing... But the code is mostly procedures for reading and parsing
>the log data. Would the unit tests create fake input and compare it
>to known output? (How do I fake reading data from disk?) For that
>matter, how do I test the data-reading procedures? (Write fake data
>and then see if the routine reads it correctly?)
>
>
Yes exactly, obviously you'll also write strange faked data to see how
your procedures and functions respond. It is much more difficult to
write tests when all the code is already written, so begin with writing
tests for low level simple methods first and when they pass you can
begin to test higher level methods. This is the natural way of
proceeding when you write tests at the same time that you write the code .
You'll see that the tests you'll find here
http://cvs.sourceforge.net/viewcvs.py/camelos/EOS/fpcEOS/tests/
are testing functionality very similar to your case hope you'll find
them useful. Some where even written over legacy code like in your case.
If you have specific questions you can even write me in private mail,
I'll try to help you if I can (here we would be off-topic).
>Or, I've been messing around with SQLite... to add unit tests for
>this code, would I need to create fake databases?
>
>
>And what about the GUI code? How can that be tested?
>
This are two of the cases where testing is most difficult. Some suggest
writing mock objects that mock the real behaviour
http://www.mockobjects.com . But is generally difficult to implement in
case of databases.
For the GUI I'm against testing it using unit testing, it's a waste of time.
If you should need a book to learn more about the testing techniques
from a very practical point of view, with many examples, I suggest this:
Pragmatic Unit Testing
in Java with JUnit
by Andy Hunt and Dave Thomas
Dean
More information about the fpc-pascal
mailing list