Re: Dos and don'ts in C++ unit testing?
Michael Fawcett wrote:
On Feb 18, 5:23 pm, Francis Glassborow <fran...@robinton.demon.co.uk>
wrote:
In article <1171706042.378302.7...@j27g2000cwj.googlegroups.com>, James
Kanze <james.ka...@gmail.com> writes>
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.
True, but if I write such pairs of functions I start questioning my
design (sometimes it is fine, but often it is a symptom of limited
understanding)
What about functions like std::vector's pop_back? How would one test
pop_back without first having items in the vector? And how could one
have items in the vector without first having working (tested)
functions that put items there (e.g. push_back)?
I'm not sure that's a good example. Obviously, the first
function you write (and test) is the constructor; std::vector
has constructors which fill the vector. On the other hand, it's
pretty hard to test whether the constructor worked if you don't
have some accessors; either [] or begin()/end(). And like it or
not, you're going to (probably) test the destructor some as well
with the first tests.
Once you've got these basics (constructor, destructor and
accessors), vector actually lends itself very well to Francis'
approach; write a function, then test it. If that's what you
feel most comfortable with. (I tend to not do it this way, but
it's a personal choice, not something essential.)
--
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! ]