Taffy Web开发,Python Flask实践详解
1. 前言
2. 实现细节
2.1 安装相关lib
pip install flask pip install flask-bootstrap pip install flask-wtf pip install nose pip install nose-html-reporting
reload(sys) sys.setdefaultencoding("utf-8")
在文件最下方_format_output方法中修改return o.decode(‘latin-1’)为return o.decode(‘utf-8’)
2.2 下载Bootstrap
3) 解压下载的bootstrap-3.3.7.zip,其中dashboard模板目录为bootstrap-3.3.7\docs\examples\dashboard
2.3 Taffy_Web项目基本结构
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方法
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视图函数
@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 示例视频
3.2 示例图片
3.2.1 首页
3.2.2 用例
3.2.3 报告