關於自動化測試框架的設計,筆者在前面的隨筆里面有介紹和總結,這里結合實際的項目例子做個demo,環境部署參考筆者的的隨筆《python3+Robot Framework+PyCharm環境部署及執行腳本》,拿開源的項目管理平台禪道為例。
一、針對項目特點確定選用的開源的測試框架,這里因為要做WEB UI自動化,選擇robot framework +selenium2library+databaselibrary,例外一些輔助的第三方庫,PyMySQL等等,其他的在做項目遇到現有的庫解決不了的情況下,再去想辦法,包括自己在robot framework的框架基礎上面開發第三方庫。
二、分析項目的測試模塊,設計數據庫表,因為筆者打算把測試用例數據放在MySQL數據庫管理。這里以登錄功能為例,筆者的是設計的數據庫表t_login如下所示:
id | 主鍵,不為null | 測試用例編號 |
flag | 不為null | 缺省0,0表示執行,1表示不執行 |
account | ||
passwd | ||
expected | 不為null | 期望結果 |
expEx1 | 期望結果擴展字段 |
三、筆者把目錄分為3塊,keywords,actionwords,testcase。
keywords:顆粒度最小的關鍵字單元。
actionwords:業務關鍵字,封裝的好可以減少testcese的代碼量。
testcase:測試用例腳本。
下面直接上代碼,結構一目了然
keywords-》登錄關鍵字.txt
*** Settings *** Library Selenium2Library *** Keywords *** 登錄系統成功 [Arguments] ${url} ${account}=admin ${passwd}=Aa123456 ${browser}=chrome ${expAcc}=admin [Documentation] url:請求地址 ... account:賬號 ... passwd:密碼 ... browser:測試瀏覽器 ... expAcc:校驗參數 open browser ${url} ${browser} maximize browser window wait until element is visible id=account input text id=account ${account} input text name=password ${passwd} click button id=submit wait until page contains ${expAcc} ${page_title} get title run keyword if '${page_title}' == '我的地盤 - 禪道' return from keyword true ... ELSE return from keyword flase 登錄系統失敗 [Arguments] ${url} ${account}=admin ${passwd}=Aa123456 ${browser}=chrome [Documentation] url:請求地址 ... account:賬號 ... passwd:密碼 ... browser:測試瀏覽器 open browser ${url} ${browser} maximize browser window wait until element is visible id=account input text id=account ${account} input text name=password ${passwd} click button id=submit alert should be present text=登錄失敗,請檢查您的用戶名或密碼是否填寫正確。 timeout=2 ${page_title} get title should be equal ${page_title} 用戶登錄 - 禪道 close all browsers 退出系統 click element xpath=//*[@class='user-name'] click element xpath=//a[contains(text(),'退出')] wait until element is visible id=account ${page_title} get title close all browsers run keyword if '${page_title}' == '用戶登錄 - 禪道' return from keyword true ... ELSE return from keyword flase
keywords-》MySQL讀取測試數據.txt
*** Settings *** Library DatabaseLibrary *** Variables *** ${dbapiModuleName} pymysql ${db_connect_string} database='test', user='root', password='root', host='127.0.0.1', port=3306 *** Keywords *** 登錄MySQL讀取測試用例 [Arguments] ${tableName} ${caseId} [Documentation] dbapiModuleName:mysql數據庫驅動的名稱 ... db_connect_string:MySQL數據庫連接信息 ... tableName:數據庫表名 ... caseId:測試用例編號 Connect To Database Using Custom Params ${dbapiModuleName} ${db_connect_string} @{queryResults} Query SELECT * FROM ${tableName} where id = ${caseId} log many @{queryResults} ${flag} set variable ${queryResults[0][1]} run keyword if '${flag}' == '0' return from keyword ${queryResults} ... ELSE return from keyword '跳過該用例'
actionwords-》登錄操作.txt
*** Settings *** Resource ../keywords/登錄關鍵字.txt Resource ../keywords/MySQL讀取測試數據.txt *** Variables *** ${test_url} http://127.0.0.1/zentao/user-login.html ${browser} chrome *** Keywords *** 登錄測試 [Arguments] ${caseId} ${testcase} 登錄MySQL讀取測試用例 t_login ${caseId} ${bool} evaluate isinstance(${testcase}, str) run keyword if '${bool}' == 'True' pass execution if ${testcase} == '跳過該用例' ${testcase} ${account} set variable ${testcase[0][2]} ${passwd} set variable ${testcase[0][3]} ${excepted} set variable ${testcase[0][4]} ${expAcc} set variable ${testcase[0][5]} ${actual_result} run keyword if '${excepted}' == 'true' 登錄系統成功 ${test_url} ${account} ${passwd} ${browser} ${expAcc} ... ELSE 登錄系統失敗 ${test_url} ${account} ${passwd} ${browser} run keyword if '${actual_result}' == 'true' 退出系統
testcase-》登錄禪道.txt
*** Settings *** Resource ../actionWords/登錄操作.txt *** Test Cases *** 登錄管理員賬戶admin 登錄測試 1 登錄測試主管賬號xiajie 登錄測試 2 登錄測試主管賬號xiajie,不輸入密碼,登錄失敗 登錄測試 3 登錄測試主管賬號xiajie,密碼錯誤(數據庫設置的跳過) 登錄測試 4
實際上,寫測試用例就很輕松了,結合robot framework的關鍵字驅動,引入MySQL做持久層數據管理,實現數據驅動測試,直接在MySQL中新增測試數據,測試用例腳本直接調用業務關鍵字+測試用例編號,腳本易讀性非常的好。
最后的執行完成后的測試報告效果: