七、Allure报告

一、Allure 的简介和使用

帮助文档
地址:https://docs.qameta.io/allure/
1.1.alure报告生成步骤
最终我们会生成一个 html 格式的报告,中间我们需要操作两步来进行。

  1. 生成xml
  2. 将 xml 转成 html

安装
pip install allure-pytest

使用步骤

  1. 将 pytest 配置文件中的命令行参数加上如下代码
--alluredir report
[pytest]
# 添加行参数
addopts = -s --alluredir report
# 文件搜索路径
testpaths = ./scripts
# 文件名称
python_files = test_*.py
# 类名称
python_classes = Test*
# 方法名称
python_functions = test_*

2.编写好测试脚本后,正常的在命令行中运行 pytest 即可。
3.程序运行结束后,我们会在项目中得到一个json文件
七、Allure报告_第1张图片
1.2.将 xml 转成 html

  1. https://bintray.com/qameta/generic/allure2 下载 allure-2.6.0.zip
  2. 解压缩到一个目录(不经常动的目录)
  3. 将压缩包内的 bin 目录配置到 path 系统环境变量
  4. 在命令行中敲 allure 命令,如果提示有这个命令,即为成功

使用步骤
在保证项目中的 report 目录下有 xml 文件的时候,执行以下步骤。

  1. 进入 report 上级目录执行命令
allure generate report/ -o report/html --clean
  1. report 目录下会生成 html 文件夹,html 下会有一个 index.html ,右键用浏览器打开即可。

七、Allure报告_第2张图片
七、Allure报告_第3张图片
1.3 参数和命令详解
应用场景
修改 xml 所在的目录名称和 index.html 所在的目录名称

疑问和解答

  1. addopts = -s --alluredir report 中的 --alluredir report 是什么意思?
    –alluredir 后面的 report 为 xml 输出的目录名
    如果希望目录名叫 result 那么可以将命令行参数改为 --alluredir result
  2. allure generate report/ -o report/html --clean 是什么意思?
    report/ 表示 xml 所在的目录
    -o 表示 output 输出
    report/html 表示将 index.html 报告生成到哪个文件夹

二、Allure 与 pytest 结合

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)

七、Allure报告_第4张图片

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)

七、Allure报告_第5张图片
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)

七、Allure报告_第6张图片
2.4添加严重级别
应用场景
在工作中,我们会向开发人员提交很多 bug ,不同的 bug 优先级也应当不同,打开程序就崩溃,和关于软件的页面错了一个字。就这两者而言,崩溃肯定更严重,也更需要开发人员优先修复。那么,我可以将这些 bug 的优先级展示在报告当中。
使用方式
在测试脚本中,增加装饰器

@pytest.allure.severity(pytest.allure.severity_level.BLOCKER)

参数有五个,也对应不同的优先级,只需要将最后一个词替换即可。

  1. BLOCKER 最严重
  2. CRITICAL 严重
  3. NORMAL 普通
  4. MINOR 不严重
  5. TRIVIAL 最不严重

示例

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()

七、Allure报告_第7张图片

你可能感兴趣的:(移动端自动化,python,开发语言,后端)