1.生成日志代码
# 编写初始化日志的代码
# 1 首先定义一个初始化日志的函数
def init_logging():
# 2 在函数中,设置日志器
logger = logging.getLogger()
# 3 设置日志等级
logger.setLevel(logging.INFO)
# 4 设置控制台处理器
sh = logging.StreamHandler()
# 5 设置文件处理器(文件处理的作用是设置保存日志的文件地址的:需要使用项目根目录定位到日志文件)
log_path = app.BASE_DIR + "/log/xxx.log"
# app.BASE_DIR是定义全局变量BASE_DIR,通过BASE_DIR定位到项目根目录
fh = logging.handlers.TimedRotatingFileHandler(log_path,
when='M',
interval=1,
backupCount=3,
encoding='utf-8')
# 6 设置格式化器
fmt = '%(asctime)s %(levelname)s [%(name)s] [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s'formatter = logging.Formatter(fmt)
# 7 将格式化器添加到文件处理器和控制台处理当中
sh.setFormatter(formatter)
fh.setFormatter(formatter)
# 8 将文件处理器和控制台处理器添加到日志器当中
logger.addHandler(sh)
logger.addHandler(fh)
2.生成测试报告
# 1 导包
import unittest
import time
import HTMLTestRunner_PY3
# 2 创建测试套件
suite = unittest.TestSuite()
# 3 将测试用例添加到测试套件
suite.addTest(unittest.makeSuite(类名(需要导入)))
suite.addTest(unittest.makeSuite(类名(需要导入)))
# 4 定义测试报告的目录和报告名称
report_path = app.BASE_DIR + "/report/xxx{}.html".format(time.strftime('%Y%m%d %H%M%S'))
# report_path = app.BASE_DIR + "/report/xxx.html"
# 5 使用HTMLTestRunner_PY3生成测试报告
with open(report_path, mode='wb') as f:
# 实例化HTMLTestRunner_PY3
runner = HTMLTestRunner_PY3.HTMLTestRunner(f, verbosity=1, title="xxx系统接口测试",
description="我们的xxxx的接口测试报告")
# 使用实例化的runner运行测试套件,生成测试报告
runner.run(suite)
3.生成漂亮报告
import unittest
import time
from lib.BeautifulReport import BeautifulReport
suite = unittest.TestLoader().discover("./po_day08", pattern="test*.py")
file_name = "test-{}.html".format(time.strftime("%Y%m%d%H%M%S"))
BeautifulReport(suite).report(filename=file_name, description="学习测试", log_path="./report")
4.使用命令行生成postman测试报告
newman run "D:\shouwang\WeChat Files\qzl18317941093\FileStorage\File\2020-06\数据驱动_测试手机号码归属地.postman_collection.json" -e
"D:\shouwang\WeChat Files\qzl18317941093\FileStorage\File\2020-06\测试环境.postman_environment.json" -d
"D:\shouwang\WeChat Files\qzl18317941093\FileStorage\File\2020-06\test_phone_sp.json" -r html --reporter-html-export report.html
5.邮箱报告模板(解决没有样式的问题)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次构建日志</title>
</head>
<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">
<div>
<h2>项目信息</h2>
<ul>
<li>项目名称:${PROJECT_NAME}</li>
<li>详细测试报告:< a href=" ">${PROJECT_URL}HTML_20Report/</ a></li>
<li>触发原因:${CAUSE}</li>
<li>项目Url:< a href="${PROJECT_URL}">${PROJECT_URL}</ a></li>
</ul>
<hr/>
<h2>构建日志</h2>
<div>${JELLY_SCRIPT,template="html"}</div>
<hr/>
</div>
</body>
</html>
6.使用postman数据驱动时,需要在Pre-request Scripts下输入
# 数据驱动 在 Pre-request Scripts
// 提取请求体到代码中
var request_body = data.request_body
// 使用js的json模块把读取的数据转化为字符串的请求体
var str_body = JSON.stringify(request_body)
// 把转化后的请求体,存到环境变量中
pm.environment.set("local_request_body", str_body)