目录:
TEXT = ("text/plain", "txt")
CSV = ("text/csv", "csv")
TSV = ("text/tab-separated-values", "tsv")
URI_LIST = ("text/uri-list", "uri")
HTML = ("text/html", "html")
XML = ("application/xml", "xml")
JSON = ("application/json", "json")
YAML = ("application/yaml", "yaml")
PCAP = ("application/vnd.tcpdump.pcap", "pcap")
PNG = ("image/png", "png")
JPG = ("image/jpg", "jpg")
SVG = ("image/svg-xml", "svg")
GIF = ("image/gif", "gif")
BMP = ("image/bmp", "bmp")
TIFF = ("image/tiff", "tiff")
MP4 = ("video/mp4", "mp4")
OGG = ("video/ogg", "ogg")
WEBM = ("video/webm", "webm")
PDF = ("application/pdf", "pdf")
test_order.py
import allure
class TestWithAttach:
def test_pic(self):
allure.attach.file('./img/111.jpg',
name="这是一个图片",
attachment_type=allure.attachment_type.JPG,
extension="jpg")
终端运行:
?
?
?
import allure
class TestWithAttach:
def test_pic2(self):
with open("./img/111.jpg", mode="rb") as f:
file = f.read()
allure.attach(file, "页面截图", attachment_type=allure.attachment_type.JPG)
?终端运行:
?
log_util.py
import logging
import os
from logging.handlers import RotatingFileHandler
# 绑定绑定句柄到logger对象
logger = logging.getLogger(__name__)
# 获取当前工具文件所在的路径
root_path = os.path.dirname(os.path.abspath(__file__))
# 拼接当前要输出日志的路径
log_dir_path = os.sep.join([root_path, f'/logs'])
if not os.path.isdir(log_dir_path):
os.mkdir(log_dir_path)
# 创建日志记录器,指明日志保存路径,每个日志的大小,保存日志的上限
file_log_handler = RotatingFileHandler(os.sep.join([log_dir_path, 'log.log']), maxBytes=1024 * 1024, backupCount=10 , encoding="utf-8")
# 设置日志的格式
date_string = '%Y-%m-%d %H:%M:%S'
formatter = logging.Formatter(
'[%(asctime)s] [%(levelname)s] [%(filename)s]/[line: %(lineno)d]/[%(funcName)s] %(message)s ', date_string)
# 日志输出到控制台的句柄
stream_handler = logging.StreamHandler()
# 将日志记录器指定日志的格式
file_log_handler.setFormatter(formatter)
stream_handler.setFormatter(formatter)
# 为全局的日志工具对象添加日志记录器
# 绑定绑定句柄到logger对象
logger.addHandler(stream_handler)
logger.addHandler(file_log_handler)
# 设置日志输出级别
logger.setLevel(level=logging.INFO)
test_order.py?
import allure
from utils.log_util import logger
@allure.epic("需求1")
@allure.feature("功能模块1")
class TestEpic:
@allure.story("子功能1")
@allure.title("用例1")
def test_case1(self):
logger.info('这是TestEpic 第一条用例')
print("用例1")
@allure.story("子功能2")
@allure.title("用例2")
def test_case2(self):
logger.debug('这是TestEpic 第二条用例')
print("用例2")
@allure.story("子功能2")
@allure.title("用例3")
def test_case3(self):
logger.warning('这是TestEpic 第三条用例')
print("用例3")
@allure.story("子功能1")
@allure.title("用例4")
def test_case4(self):
logger.error('这是TestEpic 第四条用例')
print("用例4")
终端运行:
?
?
?
日志展示在 Test body 标签下,标签下可展示多个子标签代表不同的日志输出渠道:
禁用日志,可以使用命令行参数控制 --allure-no-capture
pytest --alluredir ./results --clean-alluredir --allure-no-capture
?
allure.attach(body, name, attachment_type, extension),参数解释:
import allure
class TestWithAttach:
def test_html(self):
allure.attach('<head></head><body> a page </body>',
'附件是HTML类型',
allure.attachment_type.HTML)
def test_html_part(self):
allure.attach('''html代码块''',
'附件是HTML类型',
allure.attachment_type.HTML)
终端运行:
?
?
?
import allure
class TestWithAttach:
def test_video(self):
allure.attach.file("./mp4/111.mp4",
name="视频",
attachment_type=allure.attachment_type.MP4,
extension="mp4")
?
?