[fpc-pascal] FPCUnit article/tutorial online.

Dean Zobec dezobec at tin.it
Mon Oct 10 20:56:37 CEST 2005


L505 ha scritto:

>Since I don't exactly know what test frameworks are, even after reading about them for the
>past few years, I'm going to ask some risky questions. This is not a flame thrower attempt
>at the test framework advocates, I'm just trying to understand what exactly they do, from a
>"newbie to test frameworks" perspective.
>  
>
I did not understand the full benefits of unit tests till I've tried them.
And it took a lot to get accustomed to them.

>Do test frameworks
> -cause you to spend lots of time writing test frameworks instead of program code?
>  
>
The function of the xUnit testing framework is to let you write tests 
faster and with less effort. It's a matter of personal development 
habits and preferences of course, but the greatest benefit of unit 
testing I've experienced was the reduction of time spent with the 
debugger (I still don't know how to use it effectively with Free Pascal 
and in Lazarus I usually keep it disabled :) A failing test usually 
leads me directly to the problem. The safety net provided by the tests, 
written at the same time as I write the code gives me courage to do the 
refactoring that I find usefull when I review the code to improve the 
design,  or the changes that I need to apply when I do some profiling. 
It assures  me I've not introduced collateral damage.

> -even apply to languages with compilers and strong typing?
>  
>
I has nothing to do with strong typing IMHO, I love strong typing but it 
does not solve all my problems. I'm not a perfect programmer, I'm quite 
successful in making mistakes and I need to be sure that a piece of code 
does exactly what I thought it should do, without making assumptions and 
discover the problem later, when the code has been used in other higher 
level functions or frameworks

> -cause you to write your programs to conform to the test framework, instead of the program
>framework?
>  
>
Not all kind of code can benefit from unit testing, it's no use in my 
opinion to try to test the GUI code, and it's difficult to test some 
parts of web or db applications.
But in my case, when I'm doing unit testing of non visual code in more 
complex frameworks and I find a class that is difficult to test in 
isolation, I've noticed that in most occasions this is due to some flaw 
in my design, often the classes proved to be too tightly coupled or had 
a wrong interface. So, from my experience, writing tests has the side 
effect to improve the design of my code.

> -have any actual statistics of success?
>  
>
The Free Pascal Compiler is the best example of the success of testing, 
have you seen how many test have our compiler gods put in place in their 
testing suite? It's not fpcunit but the effect is the same. It was the 
first think I've noticed when I checked out the code from cvs the first 
time. Then I've noticed that each time a bug was found it was pinned 
down in a test to prevent it to emerge again, and I've begin to learn 
some other good tricks from them.

> -have popularity statistics that prove they are popular (in the Delphi community) as stated
>in the article?
>  
>
I'm quite sure that the folks at Borland don't do a lot of unit testing 
;) after all DUnit and NUnit are open source products :), but yes, in 
the open source Delphi community they are quite popular, see the Jedi 
JCL project that uses DUnit for example:
http://cvs.sourceforge.net/viewcvs.py/jcl/qa/automated/dunit/units/
The developers that took over the InstantObjects framework development 
are now successfully using FPCUnit to begin to understand the complex IO 
framework, pinpointing the code to be able to do some changes safely and 
maybe to pave the way for the  Lazarus port. (See 
http://cvs.sourceforge.net/viewcvs.py/instantobjects/Source/Tests/)

The Mono project is successfully using NUnit too.


> -have any real world examples of how they helped personX fix problemY in significantly less
>time?
>  
>
It's not about fixing a problem in my experience. It's about maintaining 
the same level of productivity when the system grows. To be able to do 
fearless refactoring in the phase of  reviewing the code when 
discovering some "bad smells", to be able to improve the design.
I find it difficult to begin some large refactoring without a solid base 
of tests in place. When I publish some code I prefer to thoroughly test 
it before and when I submit a bug report I found practical to provide a 
failing test that isolates it. Unit Tests are not a panacea to all the 
problems, but they make my work easier.  It helped  me to drastically 
reduce the  time I've spent debugging.  It took some time and effort to 
learn to write good tests but for me it was a good investment.  By doing 
the tests as I write the code I've learned to spend less time in 
developing good tests (it's like mowing a lawn, when the grass is small 
it's easier). It keeps my productivity stable even when the code is 
getting complex and it even helps me to isolate a reported bug in my 
code. Now I'm really having fun in coding.

>Hoping the answers are no, yes, no yes, yes, yes.
>  
>
The answer was quite more articulated and subjective :) there's no use 
in saying that unit testing is good for everyone. If the developer 
enjoys doing it, as I do, it's a good thing in my opinion. But I'm 
convinced that there's not one true way of writing good quality 
software, every one has to find the one best suited to his needs.

Dean




More information about the fpc-pascal mailing list