修改单元测试:使用 pytest 和 pytest-html 进行报表展现

This commit is contained in:
jerrylizilong 2018-11-16 17:52:18 +08:00
parent 0416024885
commit a77801c26c
5 changed files with 202 additions and 56 deletions

163
app/static/assets/style.css Normal file
View File

@ -0,0 +1,163 @@
body {
font-family: Helvetica, Arial, sans-serif;
font-size: 12px;
min-width: 1200px;
color: #999;
}
h1 {
font-size: 24px;
color: black;
}
h2 {
font-size: 16px;
color: black;
}
p {
color: black;
}
a {
color: #999;
}
table {
border-collapse: collapse;
}
/******************************
* SUMMARY INFORMATION
******************************/
#environment td {
padding: 5px;
border: 1px solid #E6E6E6;
}
#environment tr:nth-child(odd) {
background-color: #f6f6f6;
}
/******************************
* TEST RESULT COLORS
******************************/
span.passed, .passed .col-result {
color: green;
}
span.skipped, span.xfailed, span.rerun, .skipped .col-result, .xfailed .col-result, .rerun .col-result {
color: orange;
}
span.error, span.failed, span.xpassed, .error .col-result, .failed .col-result, .xpassed .col-result {
color: red;
}
/******************************
* RESULTS TABLE
*
* 1. Table Layout
* 2. Extra
* 3. Sorting items
*
******************************/
/*------------------
* 1. Table Layout
*------------------*/
#results-table {
border: 1px solid #e6e6e6;
color: #999;
font-size: 12px;
width: 100%
}
#results-table th, #results-table td {
padding: 5px;
border: 1px solid #E6E6E6;
text-align: left
}
#results-table th {
font-weight: bold
}
/*------------------
* 2. Extra
*------------------*/
.log:only-child {
height: inherit
}
.log {
background-color: #e6e6e6;
border: 1px solid #e6e6e6;
color: black;
display: block;
font-family: "Courier New", Courier, monospace;
height: 230px;
overflow-y: scroll;
padding: 5px;
white-space: pre-wrap
}
div.image {
border: 1px solid #e6e6e6;
float: right;
height: 240px;
margin-left: 5px;
overflow: hidden;
width: 320px
}
div.image img {
width: 320px
}
.collapsed {
display: none;
}
.expander::after {
content: " (show details)";
color: #BBB;
font-style: italic;
cursor: pointer;
}
.collapser::after {
content: " (hide details)";
color: #BBB;
font-style: italic;
cursor: pointer;
}
/*------------------
* 3. Sorting items
*------------------*/
.sortable {
cursor: pointer;
}
.sort-icon {
font-size: 0px;
float: left;
margin-right: 5px;
margin-top: 5px;
/*triangle*/
width: 0;
height: 0;
border-left: 8px solid transparent;
border-right: 8px solid transparent;
}
.inactive .sort-icon {
/*finish triangle*/
border-top: 8px solid #E6E6E6;
}
.asc.active .sort-icon {
/*finish triangle*/
border-bottom: 8px solid #999;
}
.desc.active .sort-icon {
/*finish triangle*/
border-top: 8px solid #999;
}

27
app/test/run_unittest.py Normal file
View File

@ -0,0 +1,27 @@
import os,platform,datetime,time
import pytest
from app import config
from app.db import test_unittest_manage
currentPath = os.path.dirname(os.path.abspath(__file__))
def run_all():
nowTime = time.strftime("%Y-%m-%d_%H_%M_%S", time.localtime())
reportName = 'unittest_' + str(nowTime)
reportFileName = reportName + '.html'
test_folder = config.unittestPath
if platform.system() == 'Windows':
reportNameFull = config.reportPath+'\\'+reportFileName
else:
reportNameFull = config.reportPath + '/' + reportFileName
start_time = datetime.datetime.now()
pytest.main([test_folder,'--html=%s' %reportNameFull,'-o log_cli=true -o log_cli_level=INFO'])
end_time = datetime.datetime.now()
# 插入数据库
test_unittest_manage.test_unittest_manage().new_unittest_case(reportName, start_time, end_time, reportFileName)
# run_test()

View File

@ -1,52 +0,0 @@
import datetime
import platform
import time
import unittest
import HTMLReport
from app import config
from app.db import test_unittest_manage
from app.test import log
def run_all():
# 通过配置项,读取报告的存储路径 reportPath
if platform.system() == 'Windows':
reportPath = config.reportPathWin
currentPath=config.unittestPathWin
else:
reportPath = config.reportPathLinux
currentPath = config.unittestPathLinux
# 获取当前路径
# currentPath = os.getcwd()
log.log().logger.info('reportPath is %s: ' %reportPath)
log.log().logger.info('currentPath is %s: ' %currentPath)
nowTime = time.strftime("%Y-%m-%d_%H_%M_%S", time.localtime())
reportName = 'unittest_'+str(nowTime)
reportFileName = reportName+'.html'
suite = unittest.TestSuite()
loader = unittest.TestLoader()
# 执行当前 test 路径下的所以 test 类
suite.addTests(loader.discover(currentPath))
# 测试用例执行器
runner = HTMLReport.TestRunner(report_file_name=reportName, # 报告文件名默认“test”
output_path=reportPath, # 保存文件夹名默认“report”
verbosity=3, # 控制台输出详细程度,默认 2
title='测试报告', # 报告标题,默认“测试报告”
description='无测试描述', # 报告描述,默认“无测试描述”
thread_count=1, # 并发线程数量(无序执行测试),默认数量 1
sequential_execution=True # 是否按照套件添加(addTests)顺序执行,
# 会等待一个addTests执行完成再执行下一个默认 False
)
# 执行测试用例套件
start_time =datetime.datetime.now()
runner.run(suite)
end_time = datetime.datetime.now()
#插入数据库
test_unittest_manage.test_unittest_manage().new_unittest_case(reportName, start_time, end_time, reportFileName)
run_all()

View File

@ -1,4 +1,4 @@
from flask import Blueprint,render_template, jsonify, request,session
from flask import Blueprint,render_template, jsonify, request,redirect,url_for
from app import log, config
from app.core import hubs
from app.view import viewutil,user
@ -75,14 +75,20 @@ def run_unittest():
result = jsonify({'code': 500, 'msg': 'should be get!'})
return result
else:
from app.test import test_run_all
from app.test import run_unittest
log.log().logger.info('start run unittest')
test_run_all.run_all()
run_unittest.run_all()
result = jsonify({'code': 200, 'msg': 'success!'})
return result
@mod.route('/assets/style.css', methods=['GET'])
def get_media():
return redirect(url_for('static',filename='assets/style.css'))
# 节点管理
@mod.route('/testhubs')
@user.authorize

View File

@ -20,6 +20,8 @@ protobuf==3.6.0
pyecharts==0.5.5
pyecharts-javascripthon==0.0.6
pyecharts-jupyter-installer==0.0.3
pytest==3.7.1
pytest-html=1.19.0
requests==2.19.1
retrying==1.3.3
selenium==3.13.0