Boost logo

Boost Users :

Subject: Re: [Boost-users] testing command-line interface using Boost.Test
From: Olaf Meeuwissen (olaf.meeuwissen_at_[hidden])
Date: 2011-10-10 20:59:16


legalize+jeeves_at_[hidden] (Richard) writes:

> boost-users_at_[hidden] spake the secret code
> <87obydywlr.fsf_at_[hidden]> 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

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net