A simple way to semi-automate testing of a
- First, we feed the app with a sequence of test inputs that is stored in a file while redirecting the output to another file.
e.g.,java AddressBook < input.txt > output.txt - Next, we compare the actual output file with another file containing the expected output.
e.g.,FC output.txt expected.txt
Let us assume we are testing a CLI app called AddressBook (Example: se-edu/addressbook-level1). Here are the detailed steps:
-
Store the test input in the text file
input.txt.add Valid Name p/12345 valid@email.butNoPrefix add Valid Name 12345 e/valid@email.butPhonePrefixMissing -
Store the output we expect from the SUT in another text file
expected.txt.Command: || [add Valid Name p/12345 valid@email.butNoPrefix] Invalid command format: add Command: || [add Valid Name 12345 e/valid@email.butPhonePrefixMissing] Invalid command format: add -
Run the program as given below, which will redirect the text in
input.txtas the input toAddressBookand similarly, will redirect the output of AddressBook to a text fileoutput.txt. Note that this does not require any code changes toAddressBook.java AddressBook < input.txt > output.txtA CLI program takes input from the keyboard and outputs to the console. That is because those two are default input and output streams, respectively. But you can change that behavior using
<and>operators. For example, if you runAddressBookin the DOS prompt, the output will be shown in the console, but if you run it like this,java AddressBook > output.txtthe Operating System then creates a file
output.txtand stores the output in that file instead of displaying it in the console. No file I/O coding is required. Similarly, adding< input.txt(or any other filename) makes the OS redirect the contents of the file as input to the program, as if the user typed the content of the file one line at a time.📎 Resources:
-
Next, we compare
output.txtwith theexpected.txt. This can be done using a utility such as WindowsFC(i.e. File Compare) command, Unixdiffcommand, 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 we need more sophisticated ways of automating tests.
CLI App: An application that has a Command Line Interface. i.e. user interacts with the app by typing in commands.