
legalize+jeeves@mail.xmission.com (Richard) writes:
boost-users@lists.boost.org spake the secret code <87obydywlr.fsf@avasys.jp> thusly:
[testing of command-line utilities]
IMO, you are approaching this all wrong. How are your unit tests going to run fast (in <100ms) if they are doing a fork/exec/system/whatever? Plus you're introducing lots of environmental dependencies on PATH, etc., that can make your tests fragile and give false failures.
What I do instead is move all the entire implementation of the utility to a static library and the main() for the utility executable is just a simple delegator that calls the function in the library that does all the work.
Then you can write a test executable against the static library and test everything in your utility. Instead of writing assertions against the exepcted cout/cerr of a child process, just use a stringstream that you pass into the utility library while the main() delegator passes in cout and cerr.
This approach gives fast execution of unit tests, lets me easily write assertions against expected output and error messages and avoids the fragility of depending on the external execution environment.
Hadn't thought of that approach. Thanks for the suggestion. -- Olaf Meeuwissen, LPIC-2 FLOSS Engineer -- AVASYS CORPORATION FSF Associate Member #1962 Help support software freedom http://www.fsf.org/jf?referrer=1962