Testing Terminal Apps

Sometimes you need to test a terminal application, which reads user inputs from terminal and prints results to terminal.  These tasks are very common in introductory programming courses. Simple testing tool can help here, and students can learn good practices – automatic testing – from the very beginning.  I’ve been looking around and does not find anything, simple enough, that ie can be used by beginner and provide basic actions – for  testing output of program and supplying inpu to itt.  So I created such tool – simpletest. (written in Python, using pexpect)

You just need to create test script as text file, where lines are started by > (for expected output) or  < (for supplying input into program).  Here is trivial example of the test script:

$ tests/test2.sh
> Your wish:\\
< mys
> mys
? 0

For this script:


read -p "Your wish:" WISH
echo $WISH


And here is result of running it:

$ stest tests/test2.txt 
0 $ tests/test2.sh
1 > Your wish:\\
2 < mys
3 > mys
4 ? 0
5 ---
All OK :-)

And here other scripts, which yields  error:

$ stest tests/echo-err.txt 
1 $ echo hi
2 > hou
PROGRAM ERROR at line 2 (> hou): Program ended before expected output, with this output: hi

Usage is very simple,  key advantage is that user can just copy interaction with program from terminal to text editor, quickly amened it and test script is ready.

There are couple more features – checking return code (?),  running program ($), line continuation (\\) and sending control control characters, which together provide everything one needs to create simple test cases. More in tool readme file.

Tool can be easily installed via pip ( if you do not have pip on your system you can install with sudo apt-get install python-pip on Debian/Ubuntu,  more details about installing pip):

# you'll need git to runn this
# sudo apt-get install git # on debian/ubuntu
sudo pip install git+https://github.com/izderadicka/simpletest.git#egg=simpletest


Leave a Reply

Your email address will not be published. Required fields are marked *