TCL/EXPECT自動化測試指令碼執行個體四 — 批命令執行

來源:互聯網
上載者:User
在測試過程中,在具體測試某一個功能點時,往往需要為此進行大量的配置。為了簡化測試過程,我們可以把所有的配置命令放在一個文字檔中,然後使用測試指令碼來執行這些命令。這樣就不需要再手工進行配置了,費時費力。
基於如上考慮,編寫了下面的指令碼tCmd.exp。這個指令碼被我們前面介紹過的test.exp指令碼調用。

# $Id$

# This file is used to execute specific commands list in a file

proc execCmdFile {cmdFile} {
    global g_dbgFlag g_prompt

    # enable debug
    set g_dbgFlag 1

    # login
    set spawn_id [login $g_devip $g_user $g_passwd]
    if {$spawn_id == 0} {
        errLog "login $g_devip failed"
        return 0
    }

    # open cmdFile
    set cmdFd [open $cmdFile r]

    while true {
        # get a line
        if {![getLine $cmdFd line]} {
            dbgLog "reached eof"
            break
        }

        # split the line
        set ln [split $line ","]
        set cmd [string trim [lindex $ln 0]]
        set out [string trim [lindex $ln 1]]

        if {$cmd == ""} continue
        if {$out == ""} set out $g_prompt

        # send cmd line
        exp_send "$cmd/n"
        dbgLog "send $cmd"

        # expect output
        dbgLog "expect $out"
        expect {
            timeout {
                errLog "TIMEOUT: while exec /"$cmd/""
                continue
            }
            -ex "$out" {
                continue
            }
        } ;# end expect
    }

    # close cmdFile
    close $cmdFd
}

# if no cmdFile, use default
if {$cmdFile == ""} {
    set cmdFile "cmdFile.txt"
}

execCmdFile $cmdFile

有了這個指令碼,我們可以使用"./test.exp -cinterface.txt cmd"來執行interface.txt中的命令。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.