Allure测试框架

1、allure介绍

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

2、allure安装

     (1) windows/mac通用安装方法
               https://github.com/allure-framework/allure2/releases 下载allure2.7.zip包
               解压->进入bin目录->运行allure.bat
               把bin目录加入PATH环境变量
     (2) Mac 可以使用brew安装
               brew install allure
     (3) 官网:http://allure.qatools.ru/
     
(4)
文档: https://docs.qameta.io/allure/#

3、使用allure2生成精美报告

     (1) 安装allure-pytest插件
               pip install allure-pytest
     (2) 运行
               在测试执行期间收集结果
                    pytest [测试文件] -s -q --alluredir=./result/(-alluredir这个选项用于指定存储测试结果的路径)
               查看测试报告
                    方式一:测试完成后查看实际报告,在线看报告,会直接打开默认浏览器展示当前报告
                         allure serve ./result/ (注意这里的serve书写)
                    方式二: 从结果生成报告,这是一个启动tomcat的服务,需要两个步骤:生成报告,打开报告
                         生成报告:allure generate ./result/ -o ./report/ --clean (注意:覆盖路径加–clean)
                         打开报告:allure open -h 127.0.0.1 -p 8883 ./report/

4、allure常用的特性

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

import allure
import pytest


@allure.feature('登陆模块')
class TestLogin():
    @allure.story('登陆成功')
    def test_login_success(self):
        print('这是登陆:测试用例,登陆成功')
        pass

    @allure.story('登陆失败')
    def test_login_success_a(self):
        print('这是登陆:测试用例,登陆成功')
        pass

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

    @allure.story('密码缺失')
    def test_login_failure(self):
        with allure.step('点击用户名'):
            print('输入用户名')
        with allure.step('点击密码'):
            print('输入密码')
        print('点击登陆')
        with allure.step('点击之后登陆失败'):
            assert '1' == 1
            print('登陆失败')

        pass

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

if __name__ == '__main__':
    pytest.main()

5、allure特性-feature/story

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

6、allure特性-step

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

7、allure特性-issue,testcase

      关联测试用例(可以直接给测试用例的地址链接)
      关联bug
           执行的时候需要几个参数
             --allure-link-pattern=issue:http://www.mytesttracker.com/issue/{}
      代码

# --allure-link-pattern=issue:http://www.mytesttracker.com/issue/{}
@allure.issue('140', '这是一个issue')
def test_with_issue_link():
    pass
TEST_CASE_LINK = 'https://github.com/qameta/allure-integrations/issues/8#issuecomment-268313637'
@allure.testcase(TEST_CASE_LINK, '登陆用例')
def test_with_testcase_link():
    print('这是一条测试用例的链接,链接到')
    pass
@allure.link("http://wwww.baidu.com", name="链接")
def test_with_link():
    print("这是一条加了链接的测试")
    pass

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

      场景
            通常测试有P0、冒烟测试、验证上线测试。按重要性级别来分别执行的,比如上线要把主流程和重要模块都要跑一遍。
      解决
            通过附加pytest.mark标记
            通过allure.feature,allure.story
            也可以通过allure.severity来附加标记
      级别
            Trivial:不重要,Minor:不太重要,Normal:正常问题,Critical:严重,Blocker:阻塞
在这里插入图片描述

      步骤
            在方法,函数和类上面加
                  @allure.serverity(allure.serverity_level.TRIVIAL)
            执行时
                  pytest -s -v 文件名 --allure-serverities normal,critical

import allure

def test_with_no_severity_labe():
    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_critcal_severity(self):
        pass

      配置pycharm
在这里插入图片描述

9、前端自动化测试——截图

      场景
            前端自动化测试经常需要附加图片或者html,在适当的地方,适当的时机截图
      解决
            @allure.attach显示许多不同类型的提供的附件,可以补充测试,步骤或测试结果。
      步骤
          在测试报告里附加网页:
            allure.attach(body(内容),name,attachment_type,extension):
            allure.attache(’<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)

 def test_inside_the_normal_severity_test_class_with_overriding_critcal_severity(self):
        allure.attach.file("./result/b.png", attachment_type=allure.attachment_type.PNG)
        pass

10、实战

在这里插入图片描述

  • 8
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值