一、安装allure
allure
文档:
https://docs.qameta.io/allure/#
allure 下载地址:
https://repo1.maven.org/maven2/io/qameta/allure/allure-commandline/2.13.8/
allure安装
(1)需要先安装java 环境
(2) windows下载zip包,解压后把bin目录加入环境变量
Mac可以用brew安装
brew install allure
(3)安装 allure-pytest库
pip install allure-pytest
转载: https://blog.csdn.net/z_erduo/article/details/116332523?spm=1001.2014.3001.5502
二、生产报告
main.py
import pytest,os
cur_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
result_path = cur_path + '/web/autotest/ui/result'
result_port = cur_path + '/web/autotest/ui/result_port'
if not os.path.exists(result_path):os.makedirs(result_path)
if not os.path.exists(result_port):os.makedirs(result_port)
pytest.main([
'--alluredir',
result_path,
'test_suites/test_login.py',
])
os.system("allure generate --clean "+result_path+" --report-dir "+result_port) #转换为html
执行成功后result_path路径下会生成报告文件
result_report会生成html报告文件
打开报告方式:
方式一: 执行命令:
allure serve result_path路径
浏览器会自动打开报告
方式二:进入result_report路径
使用pycharm打开result_report文件,打开index.html浏览器
ps:如果直接在文件夹右键打开index.html无法生成报告
三、增强报告的可观性:
allure特性
在报告中看到测试功能,子功能,或场景,测试步骤包括测试附加信息
使用方法 参数值 参数说明
@allure.epic() epic描述
@allure.feature() 模块名称
@allure.story() 子模块名称
@allure.title() 用例的标题
@allure.testcase() 测试用例的链接地址
@allure.issue() 缺陷 对应缺陷管理系统里面的链接
@allure.description() 用例描述 测试用例的描述
@allure.step() 操作步骤 测试用例的步骤
@allure.severity() 用例等级 blocker,critical,normal,minor,trivial
@allure.link() 链接 定义一个链接,在测试报告展现
@allure.attachment() 附件 报告添加附件
1、给登录模块增加登录木块区分和子用例标题
import pytest
import logging
from page_obj.login_page import LoginPage
import allure
@allure.feature("登录模块")
class TestLogin:
@allure.title("成功登录")
def test_login_success(self, app_page):
LoginPage(app_page).login('xxxxxxx', 'xxxxxx')
logging.info("开始断言")
@allure.title("失败登录")
def test_login_error(self, app_page):
LoginPage(app_page).login('xxxxxxx', 'xxxxxx')
logging.info("开始断言")
2、@allure.story
import pytest
import logging
from page_obj.login_page import LoginPage
import allure
@allure.feature("登录模块")
class TestLogin:
@allure.story("手机登录")
@allure.title("成功登录")
def test_login_success(self, app_page):
LoginPage(app_page).login('xxxxxxx', 'xxxxxx')
logging.info("开始断言")
@allure.story("手机登录")
@allure.title("失败登录")
def test_login_error(self, app_page):
LoginPage(app_page).login('xxxxxxx', 'xxxxxx')
logging.info("开始断言")
@allure.story("账号密码登录1")
@allure.title("成功登录")
def test_login_error2(self, app_page):
LoginPage(app_page).login('xxxxxxx', 'xxxxxx')
logging.info("开始断言")
3、添加测试步骤
import pytest
import logging
from page_obj.login_page import LoginPage
import allure
@allure.feature("添加地址")
class Testaddress:
@allure.title("成功添加地址")
def test_address_success(self, app_page):
with allure.step("step1:登录用户"):
print("登录用户")
with allure.step("step2:删除地址"):
print("删除地址")
with allure.step("step3:添加地址"):
print("添加地址")
with allure.step("step4:断言"):
assert 1==1
4、增加失败截图
conftest.py
import pytest, os
from selenium import webdriver
from appium import webdriver
import logging
from py._xmlgen import html
cur_path = os.path.dirname(os.path.realpath(__file__))
import allure
driver = None
@pytest.fixture()
def app_page():
logging.info('----------------测试开始-----------------')
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '8.1.0'
desired_caps['deviceName'] = 'c6e989d7'
desired_caps['appPackage'] = 'xxxx'
desired_caps['appActivity'] = 'xxxxx'
desired_caps['autoGrantPermissions'] = True
desired_caps['automationName'] = 'uiautomator2'
global driver
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
yield driver
logging.info('----------------测试结束-----------------')
driver.quit()
#通过conftest来实现报告的描述
@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
cells.insert(1, html.th('Description')) #html报告中插入一列,列头名为Description
@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
try:
cells.insert(1, html.td(report.description))
except:
pass
# @pytest.mark.hookwrapper
# def pytest_runtest_makereport(item, call):
# outcome = yield
# report = outcome.get_result()
# report.description = str(item.function.__doc__)
# @pytest.mark.hookwrapper
# def pytest_runtest_makereport(item):
# pytest_html = item.config.pluginmanager.getplugin('html')
# outcome = yield
# report = outcome.get_result()
# extra = getattr(report, 'extra', [])
#
# if report.when == 'call' or report.when == "setup":
# xfail = hasattr(report, 'wasxfail')
# if (report.skipped and xfail) or (report.failed and not xfail):
# file_name = report.nodeid.replace("::", "_")+".png"
# screen_img = _capture_screenshot()
# if file_name:
# html = '' % screen_img
# extra.append(pytest_html.extras.html(html))
# report.extra = extra
# def _capture_screenshot():
# '''截图保存为base64'''
# return driver.get_screenshot_as_base64()
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
'''
获取每个用例状态的钩子函数
:param item:
:param call:
:return:
'''
# 获取钩子方法的调用结果
outcome = yield
rep = outcome.get_result()
# 仅获取用例call且执行结果是失败的情况, 不包含 setup/teardown
if rep.when == "call" and rep.failed:
# 添加allure报告截图
if hasattr(driver, "get_screenshot_as_png"):
with allure.step('添加失败截图..'):
allure.attach(driver.get_screenshot_as_png(), "失败截图", allure.attachment_type.PNG)