gpt4 book ai didi

linux - #include 在 Expect 脚本中

转载 作者:太空狗 更新时间:2023-10-29 12:32:06 26 4
gpt4 key购买 nike

所以我在用 expect 脚本制作测试用例时遇到了麻烦,我有 10 个测试用例,它们都以相同的“功能”开始和结束,比如登录和注销或关闭一些标志,是否有可能包含或执行它们远离我的脚本,比如 spawn login.exp 或者更好地将它们放在函数中?

TC01.exp

#!/usr/bin/expect -f
set timeout 5

#example of getting arguments passed from command line..
#not necessarily the best practice for passwords though...
set server [lindex $argv 0]
set user [lindex $argv 1]
set pass [lindex $argv 2]
set no [lindex $argv 3]
set counter 0

# connect to server via ssh, login, and su to root
send_user "connecting to $server\n"
spawn ssh $user@$server

#login handles cases:
# login with keys (no user/pass)
# user/pass
# login with keys (first time verification)
expect {
"> " { }
"$ " { }

"assword: " {
send "$pass\n"
expect {
"> " { }
"$ " { }
"assword: " {
send_user "\nLogin failed\n"
incr counter 1
exit 5

}
}
}
"(yes/no)? " {
send "yes\n"
expect {
"> " { }
"$ " { }
}
}
default {
send_user "Login failed\n"
incr counter 1
exit
}
}


#TEST CASE HERE


#login out
send "exit\n"

expect {
"> " {}
default {}
}


if { $counter > 0 } {
send_user "\nTestCase finished with some errors!\nFAILED!!!\nERRORS $counter\n";
exit 4;
}

send_user "\nTestCase finished with SUCCESS!\nERRORS: $counter\n";

所以我想将 login 和 count_error 作为函数,这样我就可以像这样创建我的测试用例:

TC01.exp

#!/usr/bin/expect -f
set timeout 5
set server [lindex $argv 0]
set user [lindex $argv 1]
set pass [lindex $argv 2]
set no [lindex $argv 3]
set counter 0


login($server, $user, $pass)

#TestCase

Errors($counter)
exit

最佳答案

Expect 实际上是 Tcl,带有一些对 pty 和 fork() 的绑定(bind)。在 http://tcl.tk 上都有很好的描述。Tcl 中的函数是用 proc 完成的(看 here )例如:

lib.tcl

proc login {server user pass} {
# Your expect code goes here
return $errorCount
}
proc errors {errorCount} {
if {$errorCount > 0} {
# Your error handling code here
}
}

测试:

#!/usr/bin/env expect
source lib.tcl
set errors [login $server $user $pass]
# your test case here
errors $errors

关于linux - #include 在 Expect 脚本中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23735573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com