insertyou 发表于 2013-1-26 12:39:35

TCL/EXPECT自动化测试脚本实例二 --

现在介绍一下测试主程序: test.exp。
为了方便加入新的测试项目,主程序采用了一种灵活的机制,它根据需要通过source命令调用相应的子测试程序。这样一来,每个测试点都可以单独放到一个文件中,然后被主程序引用。
先看一下代码:

<div style="margin-left: 40px; color: rgb(0, 0, 255);">#! /usr/bin/expect --

# $Id$
# Usage:
# ./test [-uuser] [-ppassward] [-iip_address] test_001 ...
# or ./test [-uuser] [-ppassward] [-iip_address] [-ccommand_file] cmd
# or ./test [-uuser] [-ppassward] [-iip_address] [-sscript_file] script

source global.exp
source commonLib.exp

# initialize variables
set cmdFile ""
set tList $argv
set execScript ""

# process options
set endOptIndex -1
foreach arg $argv {
if {!*" $arg]} {
break
}

# inc end option index
incr endOptIndex

# get option flag and option value
set optFlg
set optVal
dbgLog "$optFlg $optVal"
if {$optVal == ""} {
dbgLog "option value is null: -$optFlg"
return -1
}

switch $optFlg {
"u" {
set g_user $optVal
dbgLog "user: $g_user"
}
"p" {
set g_passwd $optVal
dbgLog "password: $g_passwd"
}
"i" {
set g_devip $optVal
dbgLog "devip: $g_devip"
}
"c" {
set cmdFile $optVal
dbgLog "cmdFile: $cmdFile"
}
"s" {
set execScript $optVal
dbgLog "execScript: $execScript"
}
default {
puts "unknown option: -$optFlg"
return -1
}
} ;# end switch
} ;# end foreach

# remove options from list
if {$endOptIndex != -1} {
set tList
}

dbgLog "tList is: $tList"

# create log dir
if { ! || ! } {
puts "please create directory \"log\""
return -1
}

# read current time
set clicks
set tstr

# open log file
log_file "log/vLog$tstr.log"

# open brief log file
set g_bLogFd

# start testing
foreach tItem $tList {
switch $tItem {
"sys_001" { ;# test group sys_001
source snmp.exp
}
"cmd" { ;# exec cmd file
source tCmd.exp
}
"script" { ;# exec script file
if {$execScript == ""} {
puts "Please specify script name using -s option"
return -1
}
source $execScript
}

default {
puts "do you want to test \"$tItem\"\?"
}
}
}

close $g_bLogFd
页: [1]
查看完整版本: TCL/EXPECT自动化测试脚本实例二 --