Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to test command line tools? [closed]

Tags:

We have a large collection of command-line utilities that we write ourselves and use frequently. At the moment, testing them is very cumbersome and consequently, we don't do as much testing as we aught to.

I am wondering if anyone can suggest good techniques or tools for doing a good job of this kind of thing.

This is UNIX.

like image 248
Tom Duckering Avatar asked Dec 09 '08 15:12

Tom Duckering


People also ask

What is CLI testing?

The CLI tests are integration tests - they test the CLI as a standalone application. Originally an attempt was made to write tests with java and junit. But jline hangs when testing the CLI in the same JVM as Junit.

How do you test a tool?

Windows users - Touchpad settings Or, press Windows key + I to open Settings, then click Devices, Touchpad. In the Touchpad window, make sure the Touchpad On/Off toggle switch is set to On. If it's Off, change it to be in the On position. Test the touchpad to see if it works.

Which command is used on Python terminal to test the installation was successful or not?

If you run the command brew doctor, you can verify that the installation was successful: $ brew doctor Your system is ready to brew. You'll use the python3 command when you configure your text editor, when you start a Python terminal session, and when you run programs from the terminal.

How do I run a Python unit test from the command line?

The command to run the tests is python -m unittest filename.py . In our case, the command to run the tests is python -m unittest test_utils.py .


1 Answers

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).

like image 185
paxos1977 Avatar answered Nov 27 '22 15:11

paxos1977