A simple way to semi-automate testing of a CLI (Command Line Interface) app is by using input/output re-direction. Here are the high-level steps:
- First, you feed the app with a sequence of test inputs that is stored in a file while redirecting the output to another file.
- Next, you compare the actual output file with another file containing the expected output.
Let's assume you are testing a CLI app called AddressBook
. Here are the detailed steps:
Store the test input in the text file
input.txt
.Store the output you expect from the SUT in another text file
expected.txt
.Run the program as given below, which will redirect the text in
input.txt
as the input toAddressBook
and similarly, will redirect the output ofAddressBook
to a text fileoutput.txt
. Note that this does not require any changes inAddressBook
code.java AddressBook < input.txt > output.txt
The way to run a CLI program differs based on the language.
e.g., In Python, assuming the code is inAddressBook.py
file, use the command
python AddressBook.py < input.txt > output.txt
If you are using Windows, use a normal MS-DOS terminal (i.e.,
cmd.exe
) to run the app, not a PowerShell window.
Next, you compare
output.txt
with theexpected.txt
. This can be done using a utility such as Windows'FC
(i.e. File Compare) command, Unix'sdiff
command, or a GUI tool such as WinMerge.FC output.txt expected.txt
Note that the above technique is only suitable when testing CLI apps, and only if the exact output can be predetermined. If the output varies from one run to the other (e.g. it contains a time stamp), this technique will not work. In those cases, you need more sophisticated ways of automating tests.