tcltest part 5: Capture the Standard Output

The Problem

Not all functions return a value. Some of them produce output to the standard output (stdout) and we want to test that.

The Solution

We can capture the stdout for testing using the -output option to the test command. Furthermore, we can also use the -match option to deal with cases when we cannot test for exact output. In this installment, we create a directory called tcltest_part5, which contains 3 files: hello.tcl (the file under test), all.tcl (the test driver), and stdout.test (the test cases).

# hello.tcl
proc greeting {name} {
    puts "Hello, $name"
proc showWeekDay {} {
    set now [clock seconds]
    puts [clock format $now -format "Today is %A"]

File hello.tcl contains two functions, greeting and showWeekDay. These are the functions we want to test.

# all.tcl
package require tcltest
namespace import ::tcltest::*
configure -verbose {skip start}
eval ::tcltest::configure $::argv

File all.tcl is unchanged from the previous installment.

# stdout.test
source hello.tcl
package require tcltest
namespace import ::tcltest::*
test simple_capture {
    Capture the output of the greeting command
} -body {
    greeting world!
} -output "Hello, world!\n"
test regexp_capture {
    Capture the output, which can be Monday ... Sunday, we want to be
    able to match any of those values.
} -body {
} -match regexp -output "Today is (Mon|Tue|Wednes|Thurs|Fri|Satur|Sun)day"

File stdout.test contains two tests: one for greeting and one for showWeekDay. Let’s take a look at the first test: simple_capture.

  • Line 7-9 contain the test decoration which should be familiar by now.
  • Line 10 calls the greeting function, which will prints to stdout.
  • Line 11 specifies what we expect of the output. Note that the expected output includes the trailing newline to match puts’ behavior.

The second test is more interesting. It deals with cases when we don’t know the exact output. In this case, the output can only be of 7 possibilities: Monday to Sunday. However, in practice, sometimes the possibilities are infinite and we need to deal with these situations. The test command provides the -match option to allow testing the output in a fuzzy way.

  • Line 18 invokes the showWeekDay command which prints to the stdout.
  • Line 19 states the expected output using regular expression format as opposed to the default of exact match.


tcltest makes it easy to capture and test the standard output. tcltest can also test the standard error with the -errorOutput option. So far, we have been dealing with positive tests. In the next installment, we will discuss negative testing strategies such as testing for situation which a function might crash.

2 thoughts on “tcltest part 5: Capture the Standard Output

  1. vo minh luan

    hi, can u help me resolve my problem:

    spawn mysql -uADMIN -pPASSWORD otpcmd
    expect “*mysql”
    send “select * from queued_messages where ContentData like ‘%$mobileText%’;\r”
    Now, i wanna check a condition: after query “select * from queued_messages where ContentData like ‘%$mobileText%’;\r”, a response like “Empty set” -> sleep 3 and requery “select * from queued_messages where ContentData like ‘%$mobileText%’;\r” 20 times, then time out and exit mysql if query > 20 times. While query 20 times, if get expected result: ContentData contains string “$mobileText”, exit loop and exit mysql. Sorry for my bad english, also i’m as newbie expect script.

  2. Hai Post author

    Hello Luan, please post your question at and you will get the appropriate help. I understand that English is a challenge for us, but if you try hard and express your problem in a clear and precise manner, some one will spend some time helping you out.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s