1. 前言
最近為
Taffy自動化測試框架寫了個頁面,主要實現了用例管理、執行,測試報告查看管理、發送郵件及配置等功能。
本頁面適用所有基於taffy/nose框架編寫的自動化測試腳本,或基於unittest等其他單元測試框架編寫的自動化測試腳本亦可(只需進行小小的改動)。
2. 實現細節
頁面使用Python Flask +Bootstrap開發,還有部分JS。
2.1 安裝相關lib
pip install flask pip install flask-bootstrap pip install flask-wtf pip install nose pip install nose-html-reporting
其中nose-html-reporting是nose框架生成html報告插件,需要修改__init__.py編碼方式為utf-8 (示例路徑為C:\Python27\Lib\site-packages\nose_html_reporting\__init__.py),在文件最上方import中添加
reload(sys) sys.setdefaultencoding("utf-8")
在文件最下方_format_output方法中修改return o.decode('latin-1')為return o.decode('utf-8')

2.2 下載Bootstrap
1) 從官網
http://v3.bootcss.com/,下載最新Bootstrap版本
2) 這里我們從
http://v3.bootcss.com/getting-started/#examples中選取了dashboard模版(
http://v3.bootcss.com/examples/dashboard/)

3) 解壓下載的bootstrap-3.3.7.zip,其中dashboard模板目錄為bootstrap-3.3.7\docs\examples\dashboard
2.3 Taffy_Web項目基本結構

1) app文件夾存放的有static靜態文件,templates模版文件 (Bootstrap dashboard模版及css等文件就分別放在這2個目錄下)
2) forms.py為表單文件,這里主要是配置文件用到
3) views.py為視圖文件,主要存放視圖函數
4) config.yml是配置文件
5) run.py是啟動文件,使用python run.py即可啟動整個項目
2.4 核心代碼講解
我們以核心的運行測試用例為例,詳細講解下整體的實現思路。
2.4.1 “運行測試”按鈕通過onclick綁定了一個runCase()的js方法
<button type="button" id="run-case" class="btn btn-success" onclick="runCase()">運行測試</button>
2.4.2 runCase() js方法
實現思路如下:
1) 首先調用getSelect()方法獲取當前選中的測試用例,未選中任何用例時會彈出警告消息
2) 通過Http Post方法調用views.py中定義的runcCase視圖函數(傳入參數為選中文件列表)
3) 最后根據返回結果判斷測試執行成功或失敗
function runCase() { var params = {}; var selects=getSelect(); var button=$("#run-case"); if(selects == "") { alertMessage('未選中任何文件!','warning'); } else { button.attr("disabled", true); params["caseFiles"]=selects; alertMessage("后台運行測試中,請稍候..."); $.post("/runCase",params,function(result){ if (result['desc']=='0' || result['desc']) { alertMessage('測試運行完成,返回碼:<strong>'+result['desc']+'</strong>.\t<a href="report" class="alert-link">點擊查看報告!</a>'); //刷新用例列表 getCase();} else { alertMessage(result['desc'],'danger');} button.removeAttr("disabled"); }); } }
2.4.3 views.py中定義的runcCase視圖函數
實現思路如下:
1) 首先從Post請求參數中獲取需要運行的caseFiles列表,進行格式化處理(文件包含在雙引號“”中,且以空格分隔)
2) 讀取配置文件,生成測試報告路徑及名稱
3) 拼接生成nosetests運行測試用例並導出測試報告命令,使用os.system()運行
4) 最后判斷配置項是否需要測試完成自動發送報告郵件,調用封裝好的sendReportMail()方法
@app.route("/runCase", methods=["GET", "POST"]) def runCase(): if request.method == "POST": # 獲取數組參數 caseFiles = request.form.getlist("caseFiles[]") result = {} try: caseFiles = ' '.join(map(lambda i: '"' + i + '"', caseFiles)).encode('gbk') config = yaml.load(file(CONFIG_FILE, 'r')) # Taffy路徑 taffy_dir = config['taffy_dir'] # 測試報告名稱 report_name = config['report_name'] + '_{0}.html'.format(dt.now().strftime('%Y%m%d_%H%M%S')) # 先判斷文件夾是否存在,不存在則新建 reportDir = os.path.join(taffy_dir, 'Results') if not os.path.exists(reportDir): os.makedirs(reportDir) # 測試報告路徑 report_file = os.path.join(reportDir, report_name) command = 'nosetests -v {0} --with-html --html-report="{1}"'.format(caseFiles, report_file.encode('gbk')) result['desc'] = os.system(command) # 判斷是否自動發送結果郵件 if config['auto_send']: result = sendReportMail(report_file) except Exception as e: result['exception'] = u'用例運行失敗:{0}'.format(e) return jsonify(result)
3. 成果展示
3.1 示例視頻
我錄制一個簡短的操作示例視頻:
http://v.youku.com/v_show/id_XMzIzMzg5MDcwNA==.html
3.2 示例圖片
3.2.1 首頁
3.2.2 用例
3.2.3 報告
3.2.4 配置

4. 源碼下載
5. 參考資料
1) PDF 《FlaskWeb開發:基於Python的Web應用開發實戰》
2) Bootstrap:
http://v3.bootcss.com/
3) Bootstrap教程:
http://www.runoob.com/bootstrap/bootstrap-tutorial.html
4) Bootstrap手冊:
http://www.jqhtml.com/bootstraps-syntaxhigh/index.html
5) JS教程:
http://www.w3school.com.cn/b.asp
6) 代碼高亮插件:
http://prismjs.com/
7) WTForms文檔:
http://pythonhosted.org/Flask-Bootstrap/forms.html
8) Flask-WTF文檔:
http://docs.jinkan.org/docs/flask-wtf/quickstart.html
9) Flash消息文檔:
http://docs.jinkan.org/docs/flask/patterns/flashing.html