Categories: How-To

Expect command on Linux (software testing) [Basic Guide]

The expect command on Linux and autoexpect can be extremely useful for testing software.

Imagine that you built a program in Shellscript, C, Python, Perl, or any other language that runs in the shell and interacts with the user, asking for parameters, and based on the parameters providing answers.

Well, testing this type of program can be tiring, since you will have to enter some parameters several times until your program is working as expected.

The expect command and autoexpect can be useful for doing this job.

To illustrate, look at the short script that asks for the user’s name, then the month of the year:

$ cat script 
#! /bin/bash
echo “What's Your Name?”
read NAME
echo “What month of the year do you want to see the calendar?”

Read MES
echo “$NAME, here’s the calendar:”
cal $MES 2020

To execute the script, it is necessary to make it executable:

$ chmod +x script

When running it:

$. /script 
What is your name?
Uira
Which month of the year do you want to see the calendar?
01


Unira, here is the calendar: January 2020 if you have to go
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25

26 27 28 29 30 31

Imagine that you’re refining your code, and you have to test the program over and over again. Exhausting to do this manually.

To create a “self-test” of the execution of this script, without having to type the name and month at each execution, the autoexpect command can be used:

$ autoexpect. /script 
autoexpect started, file is script.exp
What is your name?
Uira
Which month of the year do you want to see the calendar?
02


Unira, here is the calendar: February 2020 if you have to go
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15 ;

16 17 18 19 20 21 22
23 24 25 26 27 28 29

autoexpect done, file is script.exp

The autoexpect program creates a file with the name of the script, followed by the extension .exp.

This generated file is a shellscript that uses the expect commands to read the output of your program, and send the counter responses.

Now, to test the small program, simply run the script.exp script:

$. /script.exp 
spawn. /script
What is your name?
Uira
Which month of the year do you want to see the calendar?
02


Unira, here is the calendar: February 2020 if you have to go
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15 ;

16 17 18 19 20 21 22
23 24 25 26 27 28 29

This way, you won’t have to type anything anymore. Just improve and test your software.

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Recent Posts

Sudo command on Linux (privilege scale) [Basic Guide]

The sudo command on Linux executes a given command as if it were another user.…

2 years ago

SS command on Linux (investigate the network) [Basic Guide]

The ss command on Linux is extremely useful for investigating sockets, providing various information about…

2 years ago

Free Linux command (memory usage) [Basic Guide]

Free Linux command shows the amount of total memory in use and available, as well…

2 years ago

Linux while command (loop – while) [Basic Guide]

The shell has structures for testing conditions and executing certain program sequences several times (loop),…

2 years ago

Linux fstab file (disk mount setup) [Basic Guide]

The /etc/fstab file stores the configuration of which devices should be mounted and what is…

2 years ago

Netcat command on Linux (Swiss network knife) [Basic Guide]

The Netcat Command in Linux or nc is a utility used to do “almost anything”…

2 years ago

This website uses cookies.