直接上代码,看看到底用脚本写测试用例到底有多简单。



lsystem = function (arg)
    print("cmd:\e\[31m"..arg.."\e\[0m")
    local date = io.popen(arg, r)
    local str = date:read"*a"
    date:close()
    return str
end
--测试mac地址的显示是否正确
function test_ipshow()
    date = lsystem("ip addr show eth1")
    assert_match("00:0a:e4:36:06:24",date)
end
简单到无法再简单的程序,只需要两句话,
第一句是执行命令。
第二句是判断返回内容是否正确。


优势一:
如果要使用Cunit进行同样的测试,当然也基本上也都是这几行语句,但是最麻烦的就是需要对内存进行管理。
优势二:
同时使用c语言的字符串匹配没有脚本语言强大,至少支持正则匹配。
优势三:
不需要编译,直接修改,解释执行。