Re: Dos and don'ts in C++ unit testing?
"Rune Allnor" <allnor@tele.ntnu.no> wrote:
"Daniel T." <danie...@earthlink.net> wrote:
"Rune Allnor" <all...@tele.ntnu.no> wrote:
I'm a hobby C++ programmer who have reached the point where
strain of testing and debugging code are about to outweigh the
fun and usefulness of my projects.
As is said in most books (e.g. "Pragmatic programmer"), "code a
little, test a lot."
However, this might be very good advice, but how does one go
about actually achieving this? There have to be things that are
smart to do, and others that are not at all smart to do? Books
that address such problems? Those I have found, deal with C# or
Java, not C++.
It sounds like you are asking about coding styles that will help
mitigate bugs rather than asking how to test code. Is that correct?
Well, yes, at least partially.
I learned a lot from some thread here a few weeks ago, where the
claim was that C++ class and method names ought to reflect the
responsibilities of objects and actions/purposes of the methods,
basically providing a self-documenting C++ code. A very small
"trick" at first glance, but incredible effective when tried in a
project above a certain scale.
Some other ideas along the same lines may help then...
1) When you have a loop that does something to a bunch of objects, put
it in its own function.
2) When you have an 'if' statement with a complex predicate, create a
const function that returns bool, name the function after what the
predicate is supposed to test, and put the complex bit in it.
3) Count how many of the following you have in your function: 'if',
'while', 'for', '||', '&&', '?:'. If you have more than five of them in
the same function, then break up the function into separate parts. (To
learn more about this google "Cyclomatic Complexity".)
I am on a hobby budget (i.e. $ == 0) which means that I have to
manually do (or at least prepare) what test frameworks automatically
do in "real" projects.
Professionals have to manually set-up that stuff too. We just get paid
to do it. :-)
I hoped to get some answers to how to design code in order to
facilitate writing a "test suite" for my code (does that make
sense?), and then some clues about how to actually design the test
suite itself.
So you have heard the mantra, "write code that is easy to test" and you
are wondering how to do that?
As far as I can tell, "test suites" are computer
programs that test other computer programs. This seems very
intriguing, but I can't find any clues about how to write those test
suites.
I sent you an email with a simple testing framework and example of use.
Good luck!
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]