【问题标题】:#include in Expect script#include 在 Expect 脚本中
【发布时间】:2014-07-07 06:50:30
【问题描述】:

所以我在使用期望脚本制作测试用例时遇到了麻烦,我有 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

【问题讨论】:

  • 是的。向我们展示您拥有的东西,我们可以帮助您重构。就你现在的问题而言,这就是你能得到的尽可能多的帮助。
  • 我已经更新了我的问题,希望这次我解释得更好。

标签: linux unix scripting automation expect


【解决方案1】:

Expect 实际上是 Tcl,其中包含一些对 pty 和 fork() 的绑定。在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 2011-10-25
    • 2014-05-04
    相关资源
    最近更新 更多