軟件測試中基于Lua腳本的自動(dòng)化測試框架設計
自動(dòng)化測試是把以人為驅動(dòng)的測試行為轉化為機器執行的一種過(guò)程。通常,在設計了測試用例并通過(guò)評審之后,由測試人員根據測試用例中描述的規程一步步執行測試,得到實(shí)際結果與期望結果的比較。在此過(guò)程中,為了節省人力、時(shí)間或硬件資源,提高測試效率,便引入了自動(dòng)化測試的概念。
一、自動(dòng)化測試背景
1. 被測對象為嵌入式系統中使用Lua腳本做膠合的一個(gè)個(gè)模塊接口。需要編寫(xiě)Lua腳本調用這些接口對接口進(jìn)行測試,運行環(huán)境為嵌入式系統中并非PC機。
2. 測試腳本能夠起到回歸測試及自動(dòng)判斷測試結果和輸出測試報告
二、實(shí)現方法
主要參考XUnit框架機制實(shí)現測試套的封裝,其封裝的對象如下:
1. 測試環(huán)境
2. 自動(dòng)化判斷
3. 測試日志
4. 測試執行情況統計
5. 測試報告
三、測試框架
1. InitTestFrame() --初始化測試框架 ,只能執行一次,否則會(huì )影響測試結果統計 2. SetCurrModule("CurrModuleName") --當前測試模塊的名字 3. WriteCaseName("CurrCaseName") --當前測試用例的名字 4. WriteCaseStep("CurrStepName") --當前測試步驟的名字 5. ret = AssertResult("sExpects","RealResult") --自動(dòng)比較(選用) 6. WriteReport(ret,"sRealResult") --將測試結果寫(xiě)入測試報告文件中 7. GetStatistic() --獲取測試執行情況統計 |
四、實(shí)現代碼
1. 環(huán)境變量
--定義不同的環(huán)境變量,便于腳本的移植 if TestEntironment == nil then --如果沒(méi)有定義TestEntironment Win32 = 1 Symbian = 2 TestEntironment = Win32 --TestEntironment = Symbian End if TestEntironment == Win32 then reportfile = "..\\TestCode\\TestReport.txt" --測試報告文件 else reportfile = "c:\\TestCode\\TestReport.txt" --測試報告文件 end |
2. 初始化測試框架
--初始化測試框架 function InitTestFrame() --定義存儲各模塊測試執行情況的表 tRunStatistic = {} tRunStatisticIndex = 0 --tRunStatistic的索引 CurrNGModuleIndex = 0 CurrNGCaseIndex = 0 --定義存儲執行失敗用例的表 tRunNG = {} end |
3. 測試套封裝
function WriteCaseName(sCaseName) --標記測試用例名,寫(xiě)入測試報告文件 CurrCase = sCaseName local h = io.open(reportfile,"a") io.output(h) local sWriteStr = "\n【" .. sCaseName .."】" .. "\n" if TestEntironment == Win32 then print(sWriteStr) end io.write(sWriteStr) io.close(h) end function WriteCaseStep(sStep) --標記測試步驟,寫(xiě)入測試報告文件 CurrStep = sStep local h = io.open(reportfile,"a") io.output(h) local sWriteStr = " |--" .. sStep .. "\n" if TestEntironment == Win32 then print(sWriteStr) end io.write(sWriteStr) io.close(h) end function SetCurrModule(sModuleName) CurrModule = sModuleName temp = {Module = sModuleName,iRunCaseNum = 0,iOKCaseNum = 0,iNGCaseNum = 0} tRunStatisticIndex = tRunStatisticIndex + 1 table.insert(tRunStatistic,tRunStatisticIndex,temp) end |
4. 自動(dòng)化判斷
--自動(dòng)比較期望結果與測試結果 function AssertResult(sExpects,RealResult) if sExpects == RealResult then return "OK" else return "NG" end end |
5. 測試日志
function WriteMsg(sMsg) local h = io.open(reportfile,"a") io.output(h) local sWriteStr = sMsg .. "\n" if TestEntironment == Win32 then print(sWriteStr) end io.write(sWriteStr) io.close(h) end |
6. 測試報告
--將測試結果寫(xiě)入測試報告文件 function WriteReport(sAssertResult,sRealResult) local h = io.open(reportfile,"a") io.output(h) local sWriteStr = " " .. sAssertResult .." (RealResult:" .. sRealResult .. ")\n" if TestEntironment == Win32 then print(sWriteStr) end io.write(sWriteStr) io.input(h) io.close(h) AddRunStatistic(sAssertResult) end |
7. 測試執行統計
function AddRunStatistic(sAssertResult) --統計測試執行情況 tRunStatistic[tRunStatisticIndex].iRunCaseNum = tRunStatistic[tRunStatisticIndex].iRunCaseNum + 1 if sAssertResult == "OK" then tRunStatistic[tRunStatisticIndex].iOKCaseNum = tRunStatistic[tRunStatisticIndex].iOKCaseNum + 1 else tRunStatistic[tRunStatisticIndex].iNGCaseNum = tRunStatistic[tRunStatisticIndex].iNGCaseNum + 1 --將失敗的插入tRunNG if (tRunNG[CurrNGModuleIndex]~= nil)and(tRunNG[CurrNGModuleIndex][1] == CurrModule) then --存在Module記錄 if (tRunNG[CurrNGModuleIndex] [2][CurrNGCaseIndex][1]~= nil)and(tRunNG[CurrNGModuleIndex][2] [CurrNGCaseIndex][1] == CurrCase) then --存在Case記錄 --添加Step項 table.insert(tRunNG[CurrNGModuleIndex][2][CurrNGCaseIndex][2],CurrStep) else --增加Case項 table.insert(tRunNG[CurrNGModuleIndex][2],{CurrCase,{CurrStep}}) CurrNGCaseIndex = CurrNGCaseIndex + 1 end else --增加Module項 table.insert(tRunNG,{CurrModule,{{CurrCase,{CurrStep}}}}) CurrNGModuleIndex = CurrNGModuleIndex + 1 CurrNGCaseIndex = 1 --復位1 end end end --統計測試用例執行情況 function GetStatistic() WriteMsg("\nTestcase run statistic:") WriteMsg("**********************************************************************") WriteMsg("【ModuleName】".." 【Run】".." 【OK】".." 【NG】") WriteMsg("----------------------------------------------------------------------") for i = 1,table.getn(tRunStatistic) do --打印格式 s1 = "" for j = 1,24 - string.len(tRunStatistic[i].Module) do s1 = s1 .." " end s2 = "" for j = 1,17 - string.len(tRunStatistic[i].iRunCaseNum) do s2 = s2 .. " " end s3 = "" for j = 1,16 - string.len(tRunStatistic[i].iOKCaseNum) do s3 = s3 .. " " end WriteMsg(i..":"..tRunStatistic[i].Module..s1..tRunStatistic[i].iRunCaseNum..s2..tRunStatistic[i].iOKCaseNum..s3..tRunStatistic[i] .iNGCaseNum) end WriteMsg("**********************************************************************") --記錄執行失敗用例 GetRunNGCase() end --記錄執行失敗用例 function GetRunNGCase() WriteMsg("NG case info:") if table.getn(tRunNG)==0 then WriteMsg("No NG case,are you sure your case is perfect?") end for i = 1,table.getn(tRunNG) do WriteMsg(tRunNG[i][1]) --Module Name for j = 1,table.getn(tRunNG[i][2]) do WriteMsg(" |--"..tRunNG[i][2][j][1]) --Case Name for k = 1,table.getn(tRunNG[i][2][j][2]) do WriteMsg(" |--"..tRunNG[i][2][j][2][k]) -- Step Name end end end end |
五、使用方法
1. 測試用例
function db_read_case() WC("db_read_case"); WS("Step1") h = db.open(U(Sdir .. "dbComm")) --WM(h) --讀數據 讀取全部 for i = 1,TEST_RECORD do writeField = string.char(0x15) for j = 1,20 do writeField = writeField .. string.char(i+j) end readField = db.read(h,i,0,512) --被測接口 ret = AR(writeField,readField) if(ret == "NG")then WM("error:".. i) break end end WR(ret,"nil") --關(guān)閉打開(kāi)的數據庫 db.close(h) end --測試用例執行 InitTestFrame() WriteMsg("Database API test begin ...") SetCurrModule("Database") CreateEntironment() --創(chuàng )建測試環(huán)境 db_read_case() DestroyEntironment()--清除測試環(huán)境 WriteMsg("Database API test end!\n") GetStatistic() |
2. 測試報告
**************************************************** Tester :vince zhang Test Date:03/27/08 15:19:06 Database API test begin ... 【db_read_case】 |--Step1 OK (RealResult:nil) |--Step2 OK (RealResult:nil) Database API test end! Testcase run statistic: ********************************************************************** 【ModuleName】 【Run】 【OK】 【NG】 ---------------------------------------------------------------------- 1:Database 57 49 8 ********************************************************************** NG case info: Database |--db_read_case |--Step1 |--db_update_case |--Step4 |
文章來(lái)源于領(lǐng)測軟件測試網(wǎng) http://kjueaiud.com/