2.2 What does a DejaGnu test look like?
Each DejaGnu test is an expect
script; the tests vary widely in
complexity, depending on the nature of the tool and the feature tested.
Here is a very simple GDB test—one of the simplest tests shipped
with DejaGnu (extracted from ‘gdb.t00/echo.exp’):(2)
| # send a string to the GDB stdin:
send "echo Hello world!\n"
# inspect the GDB stdout for the correct reply,
# and determine whether the test passes or fails:
expect {
-re "Hello world.*$prompt $" { pass "Echo test" }
-re "$prompt $" { fail "Echo test" }
timeout { fail "(timeout) Echo test" }
}
|
|
Though brief, this example is a complete test. It illustrates some of
the main features of DejaGnu test scripts:
-
The test case does not start the tested program (GDB in this case);
all test scripts for interactive tools can assume the corresponding tool
is running.
-
Comments start with ‘#’.
-
The main commands you use to control a tested program are
send
(to give it commands) and expect
(to analyze its responses).
-
The
expect
command uses a list of pairs; a pattern (regular
expression if ‘-re’ specified), followed by an action to run if the
pattern matches output from the program. Only the action for the
first matching pattern will execute.
-
Test cases use the commands
pass
and fail
to record the
test outcome.