Best way to test command line tools?

I recommend structuring your command line tool's code so that the command line utility is a client to a library of functions and/or classes.

Rather than simply using std::cout to print output, have the libraries function take an ostream reference that defaults to std::cout. When you are testing, provide a std::stringstream to collect the output.

Finally, simply compare your utility's output with expected results using your favorite unit testing framework.

(I apologize for the C++ specific example... I'm sure there are ways to do similar things in other languages too).


You can write tests that resemble an interactive shell session using Cram. It has flexible test specification format that allows you to match output using Perl regex or shell-like wildcards. Cram will replay commands from the test, compare output to the reference, and report differences.