• Allure测试框架


    一、Allure介绍

        Allure是一个轻量级,灵活的,支持多语言的测试报告工具;多平台的,奢华的report框架;

       可以为dev/qa提供详尽的测试报告、测试步骤、log;

       也可以为管理层提供high level 统计报告;

       java语言开发的,支持pytest、javascript,php,ruby等

       可以集成jenkins

    二、Allure安装(windows/mac通用安装方法):

          https://github.com/allure-framework/allure2/releases   下载allure2.7.zip包

          解压-->进入bin目录-->运行allure.bat

          把bin目录加入path环境变量

        1、windows下安装 Allure工具:

           (1) 安装JDK1.8+ ,配置环境变量

             (https://jingyan.baidu.com/article/db55b609fa946e4ba20a2f56.html

           (2) 安装Allure

              下载Allure的zip安装包

               解压到后,进入bin目录,运行allure.bat

               添加allure到环境变量PATH

               (https://www.cnblogs.com/haohuixiang/p/12627551.html)

        2、Mac可以使用brew安装:

                brew install allure

        官网:http://allure.qatools.ru/ 

        文档:http://docs.qameta.io/allure/#

    三、pytest-allure插件

          安装allure-pytest插件:pip install allure-pytest

    四、allure报告生成

          在测试执行期间收集结果

               pytest[测试文件] -s -q --alluredir=./result/   (--alluredir这个选项,用于指定存储测试结果的路劲)

               如: pytest test_alluredemo1.py --alluredir=./result/5

         查看测试报告

              方法一:测试完成后查看实际结果,在线看报告,会直接打开默认浏览器展示当前报告

                          allure serve ./result/  (注意这里的serve书写)

                         如: allure serve ./result/5

               方法二:从结果生成报告,这是一个启动tomcat服务,需要两个步骤:生成报告,打开报告

                          生成报告:

                                   allure generate ./result/ -o ./report/ --clean(注意路劲--clean)

                                   如:allure generate ./result/5 -o ./report/5 --clean

                                 

                         打开报告:

                                 allure open -h 127.0.0.1 -p 8883 ./report/

                                如:allure open -h 127.0.0.1 -p 8883 ./report/5

    五、allure特性分析

          场景:

                 希望在报告中看到测试功能,子功能或场景,测试步骤,包括测试附加信息

         解决:

                 @Feature 、 @story  、 @step 、@attach

        步骤:

               import  allure

               功能上加@allure.feature("功能名称")

              子功能上加@allure.story("子功能名称")

               步骤上加@allure.step("步骤细节")

                @allure.attach("具体文本信息"),需要附加的信息,可以是数据、文本、图片、视频、网页

               如果只测试登录功能运行是时候可以加限制过滤:

                      pytest 文件名 --allure-features 购物车功能 --allure-stories 加入购物车     (注意这里--allure_features中间是下划线)

    六、按feature、story运行

           1、@allure.feature 与 @allure.store的关系

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

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

            feature与story类似于父子关系

                  @allure.feature("测试登录模块")

                  class TestLogin:

                          @allure.story("测试成功的登录场景")

                          def test_001(self): ....

                        

                         @allure.story("测试失败的登录场景")

                          def test_002(self): ...

           2、allure特性-step

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

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

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

               用法:

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

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

                             def  test_003(self):

                                        with allure.step("点击我的自选tab"): ....

                                       with allure.step("点击股票选项"): 

                                        time.sleep(random.randint(1,3))

        3、前端自动化测试

             场景:

                    前端自动化测试经常需要添加图片或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)

    七、allure+pytest+selenium 实战演示

    import allure
    import pytest
    from selenium import webdriver
    import time
     
    @allure.testcase("http://www.baidu.com ")
    @allure.feature("百度搜索")
    @pytest.mark.parametrize("test_data1",["allure","pytest","unittest"])
    def test_steps_demo(test_data1):
    with allure.step("打开百度网页"):
    driver=webdriver.Chrome("C:/Users/YingChao/Downloads/chromedriver_win32/chromedriver.exe")
    driver.get("http://www.baidu.com")
    driver.maximize_window()
     
    with allure.step(f"输入搜索词:{test_data1}"):
    driver.find_element_by_id("kw").send_keys(test_data1)
    time.sleep(2)
    driver.find_element_by_id("su").click()
    time.sleep(2)
     
    with allure.step("保存图片"):
    driver.save_screenshot("./result/b.png")
    allure.attach.file("./result/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()

              

              

              

  • 相关阅读:
    Asp.net 动态为TreeView创建结点
    JQuery 获取鼠标位置
    几个常见的“算法”小程序
    C# 99乘法表
    C#打印一个等腰倒三角形
    JQuery点击行(tr)实现checkBox选中,反选时移除和添加样式.
    使用FIFO策略缓存对象
    Firefly
    when you say nothing at all
    linux tar 压缩解压命令
  • 原文地址:https://www.cnblogs.com/hl-2030/p/14949762.html
Copyright © 2020-2023  润新知