Re: Dos and don'ts in C++ unit testing?
Francis Glassborow wrote:
In article <1171546178.649654.103620@a34g2000cwb.googlegroups.com>,
James Kanze <james.kanze@gmail.com> writes
What the OP said, "code a little, test a lot" made me think it meant
you couldn't write 100 lines without spending a long time testing them.
There is another way f viewing that injunction.
Write a little code
test it
add a little more code
test it
add a little more code
test it
etc.
and as the code grows so do the tests because you want to make sure that
your code retains its earlier behaviour.
That's not a bad procedure, as long as it's understood what you
mean by it. It doesn't mean, for example, neglecting design or
specification.
Often, a component will be small enough to qualify directly as a
"little code", and what you are suggesting really comes down to
unit test each component before attacking the next. Other
times, components will be larger; it's quite reasonable to
implement only part of the component, then the unit tests for
that part.
The process of writing tests should intermingle with the process of
writing application code. If you ever write some code and cannot think
how to test it, think again because if the behaviour of the code is not
testable the code is not reliable.
On the other hand, there are things that you cannot easily test,
especially in low level code. Or threading. How do you test
something that depends on when the OS does a context switch? If
you know of some way to effectively test thread safety, I'm all
ears, but to date, I'm unaware of anything reliable.
For novice code the test is often a matter of running the code but as
soon as you start writing classes you should have a test harness that
tests each member function (and I generally write a member function then
test it before I progress to writing the next one.
That's one way of doing it. Although I think you often have to
group them: it's hard to test a setter without having written
the getter, and vice versa.
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]