帮助文档
地址:https://docs.qameta.io/allure/
1.1.alure报告生成步骤
最终我们会生成一个 html 格式的报告,中间我们需要操作两步来进行。
安装
pip install allure-pytest
使用步骤
--alluredir report
[pytest]
# 添加行参数
addopts = -s --alluredir report
# 文件搜索路径
testpaths = ./scripts
# 文件名称
python_files = test_*.py
# 类名称
python_classes = Test*
# 方法名称
python_functions = test_*
2.编写好测试脚本后,正常的在命令行中运行 pytest 即可。
3.程序运行结束后,我们会在项目中得到一个json文件
1.2.将 xml 转成 html
使用步骤
在保证项目中的 report 目录下有 xml 文件的时候,执行以下步骤。
allure generate report/ -o report/html --clean
1.3 参数和命令详解
应用场景
修改 xml 所在的目录名称和 index.html 所在的目录名称
疑问和解答
2.1添加测试步骤
应用场景
一套登录流程需要至少三个步骤,输入用户名,输入密码,点击登录。我们可以通过添加测试步骤,让这些步骤在报告中进行体现
使用方式
在 page 中的所有方法上加上 @allure.step(title=“测试步骤001”) 装饰器即可
核心代码
page/setting_page.py
@allure.step(title="点击搜索")
def click_search(self):
self.click(self.search_button)
page/search_page.py
@allure.step(title="输入文字")
def input_key_word(self, text):
self.input(self.search_edit_text, text)
@allure.step(title="点击返回")
def click_back(self):
self.click(self.back_button)
2.2添加测试描述
应用场景
我们在输入文字的时候想要在报告中展示输入的内容,可以使用添加测试描述的方式
不可以使用添加步骤,因为步骤使用的是装饰器,在传递过来的参数之上,无法使用
使用方式
在需要增加描述的方法之前,调用 allure.attach(‘描述’, ‘我是测试步骤001的描述~~~’,
allure.attach_type.TEXT)
核心代码
page/search_page.py
import allure
from selenium.webdriver.common.by import By
from base.base_action import BaseAction
class SearchPage(BaseAction):
search_edit_text = By.ID, "android:id/search_src_text"
back_button = By.CLASS_NAME, "android.widget.ImageButton"
@allure.step(title="输入内容")
def input_key_word(self, text):
allure.attach(text,'关键字', allure.attachment_type.TEXT)
self.input(self.search_edit_text, text)
allure.attach(self.driver.get_screenshot_as_png(),"截图", allure.attachment_type.PNG)
@allure.step(title="点击返回")
def click_back(self):
self.click(self.back_button)
2.3 图片描述
应用场景
和文字描述的显示位置相同,如果我们想将某些操作过后的结果展现在报告上,可以使用添加图片描述的方法。
使用方式
和文字描述的方法是一样的,但是参数不同。在操作完成之后增加代码
allure.attach("截图",self.driver.get_screenshot_as_png(), allure.attach_type.PNG)
核心代码
import allure
from selenium.webdriver.common.by import By
from base.base_action import BaseAction
class SearchPage(BaseAction):
search_edit_text = By.ID, "android:id/search_src_text"
back_button = By.CLASS_NAME, "android.widget.ImageButton"
@allure.step(title="输入内容")
def input_key_word(self, text):
allure.attach(text,'关键字', allure.attachment_type.TEXT)
self.input(self.search_edit_text, text)
allure.attach(self.driver.get_screenshot_as_png(),"截图", allure.attachment_type.PNG)
@allure.step(title="点击返回")
def click_back(self):
self.click(self.back_button)
2.4添加严重级别
应用场景
在工作中,我们会向开发人员提交很多 bug ,不同的 bug 优先级也应当不同,打开程序就崩溃,和关于软件的页面错了一个字。就这两者而言,崩溃肯定更严重,也更需要开发人员优先修复。那么,我可以将这些 bug 的优先级展示在报告当中。
使用方式
在测试脚本中,增加装饰器
@pytest.allure.severity(pytest.allure.severity_level.BLOCKER)
参数有五个,也对应不同的优先级,只需要将最后一个词替换即可。
示例
from base.base_driver import init_driver
from page.page import Page
import pytest
import time
import allure
class TestSearch:
def setup(self):
self.driver = init_driver()
self.page = Page(self.driver)
@allure.severity("BLOCKER")
@pytest.mark.parametrize("args", ["hello1", "xiaoming"])
def test_search(self, args):
self.page.setting.click_search()
self.page.search.input_key_word(args)
time.sleep(3)
self.page.search.click_back()
@allure.severity("CRITICAL")
def test_001(self):
assert 1
@allure.severity("NORMAL")
def test_002(self):
assert 0
@allure.severity("MINOR")
def test_003(self):
assert 1
@allure.severity("TRIVIAL")
def test_004(self):
assert 0
def teardown(self):
time.sleep(3)
self.driver.quit()