最近一直在自己研究想借助playwright和allure框架进行自动化测试,生成一个完美的自动化测试报告,自己先尝试写简单代码,生成报告
文章开始先介绍一下allure特性:,可以在代码中借助这些特性,美化自己的报告
@allure.feature(‘功能名称’)
@allure.stiry(‘子功能名称’)
@allure.step(‘步骤细节’)
feature 对应模块
story 对应子模块
@allure.link(“链接地址”,name=“描述”)
@allure.testcase(“用例地址”,“描述”)
@allure.issue(“bug id”,“描述”)
@allure.attach(‘具体文本信息’),需要附加的信息,可以提供数据、文本、图片、视频、网页
示例:
# 链接地址
@allure.link("http://www.baidu.com", name="这是一个链接")
def test_with_link():
print("这是一条加了链接的用例")
# 用例地址
TEST_CASE_LINK = "http://www.baidu.com"
@allure.testcase(TEST_CASE_LINK, "登录用例")
def test_with_link_2():
print("这是一条加了链接的登录用例")
# bug链接 bug id
@allure.issue("9663", "这是一个issue")
def test_with_link_3():
print("这是一条加了链接的登录用例")
环境配置:pycharm,allure-pytest,本地先要安装好jdk,allure
下面是运用allure对测试用例运行失败进行截图,获取测试报告的简单代码:
conftest.py:配置失败时进行截图
import allure
import pytest
from selenium import webdriver
@pytest.fixture()
def browser():
global driver
driver = webdriver.Chrome()
yield driver
driver.quit()
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport():
# 获取到用例执行之后的result对象
out = yield
"""
setup:
call: 测试执行中
teardown:
wasxfail: 标记失败的用例
"""
report = out.get_result()
# 判断call阶段(测试用例的执行阶段),如果你的用例被标记为失败,那么就完成截图
if report.when == "call":
xfail = hasattr(report, "wasxfail")
# report.skipped: 用例跳过
# report.failed:用例失败
if (report.skipped and xfail) or (report.failed and not xfail):
with allure.step("添加失败截图... ..."):
allure.attach(driver.get_screenshot_as_png(), "失败截图", allure.attachment_type.PNG)
test_demo.py 测试用例:
import time
from selenium.webdriver.common.by import By
def test_baidu(browser):
driver=browser
driver.get("http://www.baidu.com")
driver.find_element(By.ID,"kw").send_keys("巴黎奥运会")
driver.find_element(By.ID,"su").click()
time.sleep(3)
assert driver.title=="12微软"
main.py运行此代码,可以获取测试报告
import os
import pytest
def run():
pytest.main(['--alluredir', './result', '--clean-alluredir'])
os.system('allure generate ./result/ -o ./report/ --clean')
if __name__ == '__main__':
run()
生成的测试报告是这样子的: