關於自動化測試框架的設計,筆者在前面的隨筆裏面有介紹和總結,這裏結合實際的項目例子作個demo,環境部署參考筆者的的隨筆《python3+Robot Framework+PyCharm環境部署及執行腳本》,拿開源的項目管理平臺禪道爲例。html
1、針對項目特色肯定選用的開源的測試框架,這裏由於要作WEB UI自動化,選擇robot framework +selenium2library+databaselibrary,例外一些輔助的第三方庫,PyMySQL等等,其餘的在作項目遇到現有的庫解決不了的狀況下,再去想辦法,包括本身在robot framework的框架基礎上面開發第三方庫。python
2、分析項目的測試模塊,設計數據庫表,由於筆者打算把測試用例數據放在MySQL數據庫管理。這裏以登陸功能爲例,筆者的是設計的數據庫表t_login以下所示:mysql
id | 主鍵,不爲null | 測試用例編號 |
flag | 不爲null | 缺省0,0表示執行,1表示不執行 |
account | ||
passwd | ||
expected | 不爲null | 指望結果 |
expEx1 | 指望結果擴展字段 |
3、筆者把目錄分爲3塊,keywords,actionwords,testcase。sql
keywords:顆粒度最小的關鍵字單元。chrome
actionwords:業務關鍵字,封裝的好能夠減小testcese的代碼量。數據庫
testcase:測試用例腳本。api
下面直接上代碼,結構一目瞭然瀏覽器
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中新增測試數據,測試用例腳本直接調用業務關鍵字+測試用例編號,腳本易讀性很是的好。
最後的執行完成後的測試報告效果: