六、Allure测试报告定制

Allure介绍

  • allure是一个轻量级,灵活的,支持多语言的测试报告工具;多平台的,奢华的report框架;
  • 可以为dev/qa提供详尽的的测试报告、测试步骤、log;也可以为管理理层提供high level统计报告;
  • Java语言开发的,支持pytest,JaveScriipt,PHP, ruby等
  • 可以集成到Jenkins

Allure报表预览

image.png
image.png

Allure安装


针对 Pytest 测试框架安装

  • 针对 Pytest 测试框架还需安装 allure-pytest 第三方库:pip install allure-pytest
  • 用法:pytest --alluredir=/tmp/my_allure_results,将测试报告文件存放到/tmp/my_allure_results 目录下
  • 查看报告:allure serve /tmp/my_allure_results
  1. 测试文件 test_pytest.py 文件如下:
import pytest

def test_success():
    """this test succeeds"""
    assert True


def test_failure():
    """this test fails"""
    assert False


def test_skip():
    """this test is skipped"""
    pytest.skip('for a reason!')


def test_broken():
    raise Exception('oops')
  1. 在pycharm的terminal中切换到当前路径,运行pytest test_allure.py --alluredir=./results/1,就能在目录中生成测试报告文件如下,一个测试用例对应一个 .json 文件:
    image.png
  2. 在terminal中运行allure_practice>allre serve ./results/1 就能自动打开网页查看报告
    image.png

使用 Allure2 生成精美报告

  • 安装allure-pytest插件
    • pip install allure-pytest
  • 运行:
    • 在测试执行期间收集结果
      • pytest [测试文件] -s -q --alluredir=./ result/(--alluredir 这个选项用于指定存储测试结果的路径)
  • 查看测试报告
    • 方式一:测试完成后查看实际报告,在线看报告,会直接打开默认浏览器展示当前报告
      • allure serve ./result/ (注意这里的serve书写)
    • 方式二:从结果生成报告,这是一个启动 tomcat 的服务,可以永久打开,需要两个步骤:生成报告,打开报告
      • 生成报告
        • allure generate ./result/ -o ./report/ --clean(注意:覆盖路径加 --clean )
      • 打开报告
        • `alure open -h 127.0.0.1 -p 8883 ./report/
  • 关闭报告:在terminal中使用ctrl+c
使用上面的例子
  1. 在pycharm的terminal中切换到当前路径,运行pytest test_allure.py --alluredir=./results/1
  2. 在terminal 中运行allure generate ./results/1 -o ./report/1 --clean,会在report目录下生成报告文件如下,包括了.json .txt .html .js .css .ico等文件:
    image.png

Allure常用特性

  • 场景:
    • 希望在报告中看到测试功能,子功能或场景,测试步骤,包括测试附加信息
  • 解决:
    • @Feature,@story,@step,@attach
  • 步骤:
    • import allure
    • 功能上加 @allure.feature('功能名称')
    • 子功能上加@allure.story('子功能名称')
    • 步骤上加@allure.step('步骤细节')
    • @allure.attach('具体文本信息),需要附加的信息,可以是数据,文本,图片,视频,网页
    • 如果只测试登录功能运行的时候可以加限制过滤:
      • pytest [文件名] --allure_features '购物车功能' --allure-stories '加入购物车' (注意这里—allure_features中间是下划线)
  • 实例test_feature_story.py代码如下
import pytest
import allure

# 标识整个模块都用来进行登录
@allure.feature('登录模块')
class TestLogin():

    @allure.story('登录成功')
    def test_login_success(self):
        print("这是登录:测试用例,登陆成功")
        pass

    @allure.story('登录失败')
    def test_login_fail(self):
        print("这是登录:测试用例,登陆失败")
        pass

    @allure.story('用户名缺失')
    def test_login_lostname(self):
        print("用户名缺失")
        pass

    @allure.story('密码缺失')
    def test_login_lostsec(self):

        # 对一些关键步骤进行标记
        with allure.step("点击用户名"):
            print("输入用户名")
        with allure.step("点击密码"):
            print("输入密码")
        print("点击登录")
        with allure.step("点击登录之后登录失败"):
            assert "1" == 1
            print("登录失败")
        pass

    @allure.story("登录失败")
    def test_login_failure(self):
        pass
  1. Terminal中输入pytest test_feature_story.py --alluredir=./results/2,生成报告文件
  2. Terminal中输入allure serve ./results/2,打开测试报告
    image.png

    image.png
  3. 指定 feature 运行,执行pytest -v test_feature_story.py --allure-features '登录模块'-v 为打印详细信息
  4. 指定 story 运行,执行pytest -v test_feature_story.py --allure-stories '登录成功'

Allure特性-feature/story

  • 注解@allure.feature 与@allure.store的关系
    • feature相当于一个功能,一个大的模块,将case分类到某个feature中,报告中Behaviors 中显示,相当于testsuite
  • story相当于对应这个功能或者模块下的不同场景、分支功能,属于feature之下的结构,报告在
    features中显示,相当于testcase
  • feature与story类似于父子关系

Allure特性-step

  • 测试过程中每个步骤,一般放在具体逻辑方法中
  • 可以放在关键步骤中,在报告中显示
  • 在app, web自动测试当中,建议每切换到一个新的页面当做一个step用法:
    • @allure.step() 只能以装饰器的形式放在类或者方法上面
    • with allure.step() 可以放在测试用例方法里面,但测试步骤的代码需要被该语句包含

Allure特性-issue,testcase

  • 关联测试用例(可以直接给测试用例的地址链接)
  • 关联bug
  1. 加入链接示例:@allure.link("www.baidu.com")
import allure

@allure.link("www.baidu.com")
def test_with_link():
    print("这是一条加了链接的测试")
    pass
  • 执行结果报告


    image.png
  1. 给链接取别名:@allure.link("www.baidu.com", name="百度一下")
import allure

@allure.link("www.baidu.com", name="百度一下")
def test_with_link():
    print("这是一条加了链接的测试")
    pass
  • 执行结果报告


    image.png
  1. 加入测试用例的链接:@allure.testcase(TST_CASE_LINK, '登录用例')
import allure

TST_CASE_LINK ='https://github.com/qameta/allure-integrations/issues/8#issuecomment-268313637'
@allure.testcase(TST_CASE_LINK, '登录用例')
def test_with_testcase_link():
    print("这是一条测试用例的链接,链接到测试用例里面")
    pass
  • 执行结果报告


    image.png
  1. 通过传入的bugID生成bug链接:@allure.issue('BugID', 链接名'),运行时指定链接地址
import allure

# 测试bug链接,前面为bugID
"""
在运行时加入bug链接: --allure-link-pattern=issue:http://www.mytesttracker.com/issue/{}
中括号里面就会传bugID
"""
@allure.issue('140', '这是一个issue')
def test_with_issue_link():
    pass
  • 在terminal中运行时需要加入bug链接:pytest test_link_issue.py --allure-link-pattern=issue:http://www.mytesttracker.com/issue/{} --alluredir=./results/6,花括号内会自动传入BugID
  • 报告运行结果


    image.png

按重要性级别进行一定范围的测试

  • 场景
    • 通常测试有P0、冒烟测试、验证上线测试。按重要性级别来分别执行的,比如上线要把主流程和重要模块都跑一遍
  • 解决:
    • 通过附加py.test.mark标记
    • 通过allure.featureallure.story
    • 也可以通过allure.severity来附加标记
      • 级别:Trivial不重要,Minor不太重要,Normal正常问题,Critical严重,Blocker阻塞
  • 步骤:
    • 在方法、函数和类上面加
      • @allure.severity(allure.severity_level.TRIVIAL)
    • 执行时
      • py.test -s -v [文件名] --allure-severities norma,critical
  • 缺陷等级
名称 描述
Blocker级别 中断缺陷(客户端程序无响应,无法执行下一步操作)
Critical级别 临界缺陷(功能点缺失)
Normal级别 普通缺陷(数值计算错误)
Minor级别 次要缺陷(界面错误与UI需求不符)
Trivial级别 轻微缺陷(必输项无提示,或者提示不规范)
  1. 执行严重级别为 normal和critical的测试用例:pytest -v test_severity.py --allure-severities normal,critical --alluredir=./results/7
import allure
def test_with_no_severity_label():
    pass

@allure.severity(allure.severity_level.TRIVIAL)
def test_with_trivial_severity():
    pass

@allure.severity(allure.severity_level.NORMAL)
def test_with_normal_severity():
    pass

@allure.severity(allure.severity_level.NORMAL)
class TestClassWithNormalSeverity(object):
    def test_inside_the_normal_severity_test_class(self):
        pass

    @allure.severity(allure.severity_level.CRITICAL)
    def test_inside_the_normal_severity_test_class_with_overriding_critical_severity(self):
        pass
  • 执行结果报告


    image.png

在pycharm中配置默认的allure参数

配置以后直接鼠标右键-> run 'pytest for ...' 即可


image.png

前端自动化测试-截图

  • 场景:
    • 前端自动化测试经常需要附加图片或html,在适当的地方,适当的时机截图
  • 解决:
    • @allure.attach显示许多不同类型的提供的附件,可以补充测试,步骤或测试结果。
  • 步骤:
    • 在测试报告里附加网页:
      • allure.attach(body(内容), name, attachment_type, extension)
      • allure.attach('<head></head><body>首页</body>,'这是错误页的结果信息',allure.attachment_type.HTML)
    • 在测试报告里附加图片:
      • allure.attach.file(source, name, attachment_type, extension)
      • allure.attach.file("./ result/b.png" , attachment_type=allure.attachment_type.PNG)
  • 代码示例
import allure
import pytest

def test_attach_text():
    allure.attach("这是一个纯文本", attachment_type=allure.attachment_type.TEXT)

def test_attach_htlm():
    allure.attach("<body>这是一段html body块</body>", 'html测试块', attachment_type=allure.attachment_type.HTML)

def test_attach_photo():
    allure.attach(r"D:\Programs\DevOps\Python_Practice\allure_practice\resource\1.png",
                  name="这是一个图片", attachment_type=allure.attachment_type.PNG)
  • 运行结果报告


    image.png
  • 运行结果图片展示出来,是因为调用的方法allure.attach(r"D:\Programs\DevOps\Python_Practice\allure_practice\resource\1.png", name="这是一个图片", attachment_type=allure.attachment_type.PNG)有误,应该调用allure.attach.file() 方法
  • 修改后的代码:
import allure
import pytest

def test_attach_text():
    allure.attach("这是一个纯文本", attachment_type=allure.attachment_type.TEXT)

def test_attach_htlm():
    allure.attach("<body>这是一段html body块</body>", 'html测试块', attachment_type=allure.attachment_type.HTML)

# 调用图片要使用file方法
def test_attach_photo():
    allure.attach.file(r"D:\Programs\DevOps\Python_Practice\allure_practice\resource\1.png",
                  name="这是一个图片", attachment_type=allure.attachment_type.PNG)
  • 修改后的运行结果


    image.png

实战:pytest+allure+selenium

  • 代码
import time
import allure
import pytest
from selenium import webdriver

@allure.testcase("http://www/github.com", "测试用例地址")
@allure.feature("百度搜索")
@pytest.mark.parametrize('test_data', ['allure', 'pytest', 'unittest'])
def test_steps_demo(test_data):

    with allure.step("打开百度网页"):
        driver = webdriver.Chrome()
        driver.maximize_window() # 浏览器最大化
        driver.get("http://www.baidu.com")

    with allure.step("输入搜索词"):
        driver.find_element_by_id("kw").send_keys(test_data)
        time.sleep(2)
        driver.find_element_by_id("su").click()
        time.sleep(2)

    with allure.step("保存图片"):
        driver.save_screenshot("./resource/b.png")
        allure.attach.file("./resource/b.png", attachment_type=allure.attachment_type.PNG)
        allure.attach('<head></head><body>首页</body>','Attach with HTML type',allure.attachment_type.HTML)

    with allure.step("关闭浏览器"):
        driver.quit()
  • 运行结果


    image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,841评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,415评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,904评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,051评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,055评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,255评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,729评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,377评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,517评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,420评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,467评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,144评论 3 317
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,735评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,812评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,029评论 1 256
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,528评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,126评论 2 341

推荐阅读更多精彩内容