allure的介绍和使用

一、allure的介绍

 二、allure报告概览

https://demo.qameta.io/allure/#

三、allure报告安装

下图为mac安装后的截图:

 四、使用allure2生成更加精美的测试报告

安装allure-pytest插件:

  pip install allure-pytest  (安装allure生成测试报告)

运行:

在测试执行期间收集结果:

  pytest --allure=指定路径 (指定allure报告数据生成路径)

查看测试报告:

方法1:

  pytest serve 报告路径 (生成html报告,会直接打开在线报告)

方法2:

  allure generate ./result//5 -o ./report/5/ --clean(指定生成报告的路径)

  allure open -h 127.0.0.1 -p 8888 ./report/5 (启动本地服务生成链接查看报告)

 收集测试结果:

 方法1生成测试报告:

 方法2生成测试报告:

 五、allure用例描述

使用方法参数值参数说明
@allure.epic() epic描述 敏捷里面的概念,定义史诗,往下是feature
@allure.feature() 模块名称 功能点的描述,往下是story
@allure.story() 用户故事 用户故事,往下是title
@allure.title(用例的标题) 用例的标题 重命名html报告名称
@allure.step() 操作步骤 测试用例的步骤
@allure.testcase() 测试用例的链接地址 对应功能测试用例系统里面的case
@allure.issue() 缺陷 对应缺陷管理系统里面的链接
@allure.description() 用例描述 测试用例的描述
@allure.severity() 用例等级 blocker,critical,normal,minor,trivial
@allure.link() 链接 定义一个链接,在测试报告展现
@allure.attachment() 附件 报告添加附件

 

1)@allure.feature与@allure.story的关系

feature相当于一个功能,一个大的模块,将case分类到某个feature中,报告在behaviour中显示,相当于testsuite

story相当于这个功能或者模块下的不同场景,分支功能,属于feature之下的结构,报告在features中显示,相当于testcase

feature与story类似于父与子关系

import pytest
import allure
@allure.feature("登录模块")
class Test_login():
    @allure.story("用户名正确,登录成功")
    def test_logina(self):
        print("这是登录,用户名正确,登录成功")
        pass

    @allure.story("密码正确,登录成功")
    def test_loginb(self):
        print("这是登录,密码正确,登录成功")
        pass

 

2)@allure.step()与with allure.step():的区别

测试过程中的每个步骤,一般放在具体逻辑方法中

可以放在关键步骤中,在报告中显示

在app,web自动化测试中,建议每切换到一个新的页面当做一个step

用法:

   @allure.step()只能以装饰器的形式放在类或者方法上面

   with allure.step(): 可以放在测试用例方法里,但测试步骤代码需要被该语句包含

import pytest
import allure
@allure.feature("登录模块")
class Test_login():
    @allure.step("这是测试步骤1")
    def test_logina(self):
        print("这是登录,用户名正确,登录成功")
        with allure.step("这是新的步骤1"):
            print("step步骤1测试")

        with allure.step("这是新的步骤2"):
            print("step步骤2测试")

        pass


if __name__ == '__main__':
    pytest.main("-v -s")

 

 3)allure关联issuce和testcase

关联测试用例(可以直接给测试用例的地址链接)

关联bug:执行的时候需要加参数 --allure-link-pattern=issue:http://www.mytesttracker.com/issue/{}

import pytest
import allure
@allure.feature("登录模块")
class Test_login():
    @allure.issue('140','test with issue link')
    def test_with_issue_link(self):
        print("测试关联bug")
        pass

    @allure.testcase("https://www.baidu.com")
    def test_with_testcase_link(self):
        print("测试关联testcase")
        pass



if __name__ == '__main__':
    pytest.main("-v -s")

 

 

4)给测试用例划分优先级

场景:

  通常测试有P0、冒烟测试、验证上线测试。按重要性级别来分别执行,比如上线要把主流程和重要模块都跑一遍。

解决:

  a、通过附加pytest.mark标记

  b、通过allure.feature,allure.story标记

  c、也可以通过allure.severity来附加标记

   级别:Trivial:不重要   Minor:不太重要  Normal:正常问题  Critical:严重   Blocker:阻塞

步骤:在方法、函数、类上面加上

  @allure.severity(allure.severity_level.TRIVAL)

执行时

 pytest -s -v 文件名 --allure-severities normal,critical

 

 

5)给allure报告添加内容(图片、附件、文本、截图、HTML等)

场景:

  前端自动化测试经常需要附加图片或html,在适当的地方,适当的时机截图

解决:

  @allure.attach显示许多不同类型的提供的附件,可以补充测试、步骤或测试结果

步骤:

import pytest
import allure
@allure.feature("登录模块")
class Test_login():

    def test_attach_html(self):
        print("在测试报告里面附加网页信息")
        allure.attach('<head></head><body>首页</body>', '这是错误页的结果信息', allure.attachment_type.HTML)
        pass


    def test_attach_text(self):
        print("在测试报告里面附加文本信息")
        allure.attach("这是文本信息","文本",allure.attachment_type.TEXT)
        pass

    def test_attach_file(self):
        print("在测试报告里面附加图片")
        allure.attach.file("./result/test.png",attachment_type=allure.attachment_type.PNG)



if __name__ == '__main__':
    pytest.main("-v -s")

 在测试报告里加网页

 在测试报告里附加图片

 在测试报告里添加文本

 

六、实例

1、实战1

import pytest
import allure
@allure.feature("登录模块")
class Test_login():
    @allure.story("用户名正确,登录成功")
    def test_logina(self):
        print("这是登录,用户名正确,登录成功")
        pass

    @allure.story("密码正确,登录成功")
    def test_loginb(self):
        print("这是登录,密码正确,登录成功")
        pass

    @allure.story("用户名错误,登录失败")
    def test_loginc(self):
        print("这是登录,用户名错误,登录失败")
        pass


    @allure.story("密码错误,登录失败")
    def test_logind(self):
        print("密码错误,登录失败")
        pass

if __name__ == '__main__':
    pytest.main("-v -s")

(1)指定运行的feture

pytest test.py -s -q --alluredir=./result/ --allure-features '登录模块'

(2)指定运行的story

pytest test.py -s -q --alluredir=./result/ --allure-stories '用户名正确,登录成功'

 

 

2、实战2

import pytest
import allure
import time
from selenium import webdriver

Testcase_link1 = "https://www.baidu.com"
@allure.testcase(Testcase_link1,"百度,你值得拥有")
@allure.feature("百度搜索")
@pytest.mark.parametrize("search_data",["奔驰","宝马","保时捷"])
def test_search(search_data):
    with allure.step("打开百度网页"):
        driver = webdriver.Chrome("/Users/grace/selenium/driver/88/chromedriver")
        driver.get("https://www.baidu.com")

    with allure.step(f"输入搜索词{Testcase_link1}"):
        driver.find_element_by_id("kw").send_keys(search_data)
        time.sleep(3)
        driver.find_element_by_id("su").click()
        time.sleep(3)

    with allure.step("保存图片"):
        driver.save_screenshot("./result/b.png")
        allure.attach.file("./result/b.png",name="这是保存的图片",attachment_type=allure.attachment_type.PNG)

    with allure.step("关闭浏览器"):
        driver.quit()

if __name__ =='__main__':
    pytest.main("-v -s")

 aallure报告结果:

 

posted on 2021-06-10 15:50  crystal1126  阅读(1781)  评论(0编辑  收藏  举报

导航