• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

接口自动化框架篇pytest + allure生成测试报告

武飞扬头像
静姐说测试
帮助1

Allure 是一款轻量级、支持多语言的开源自动化测试报告生成框架,由Java语言开发,可以集成到 Jenkins。 pytest 测试框架支持Allure 报告生成。

pytest也可以生成junit格式的xml报告和HTML报告,命令如下:

  1.  
    pytest test_demo.py --junitxml=report.xml
  2.  
    pytest test_demo.py --html=report.html #需要安装插件:pip install pytest-html

Allure 报告更加灵活美观,本文介绍如何使用pytest 生成 allure测试报告

环境安装

安装allure

  1. allure包下载:Releases · allure-framework/allure2 · GitHub
  2. 解压 -> 进入bin目录 -> 运行allure.bat,
  3. 把bin目录加入PATH环境变量

学新通

allure官网 : Qameta Software - Allure Report

allure文档 : Allure Framework

安装 allure-pytest插件

pip install allure-pytest

生成Allure报告

运行

pytest [测试文件] -s -q --alluredir=./result #--alluredir用于指定存储测试结果的路径)

查看测试报告

方式一:直接打开默认浏览器展示报告

allure serve ./result/

方式二:从结果生成报告

  • 生成报告

    allure generate ./result/ -o ./report/ --clean (覆盖路径加--clean)

  • 打开报告

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

实例代码:Allure Framework

test_allure.py:

  1.  
    import pytest
  2.  
     
  3.  
    def test_success():
  4.  
    """this test succeeds"""
  5.  
    assert True
  6.  
     
  7.  
    def test_failure():
  8.  
    """this test fails"""
  9.  
    assert False
  10.  
     
  11.  
    def test_skip():
  12.  
    """this test is skipped"""
  13.  
    pytest.skip('for a reason!')
  14.  
     
  15.  
    def test_broken():
  16.  
    raise Exception('oops')
学新通

方法1

执行测试用例:

pytest test_allure.py --alluredir=./result/1

学新通

打开报告:

  1.  
    > allure serve ./result/1
  2.  
    Generating report to temp directory...
  3.  
    Report successfully generated to C:\Users\10287\AppData\Local\Temp\6968593833275403330\allure-report
  4.  
    Starting web server...
  5.  
    2020-10-25 20:59:42.368:INFO::main: Logging initialized @4873ms to org.eclipse.jetty.util.log.StdErrLog
  6.  
    Server started at <http://169.254.57.162:60084/>. Press <Ctrl C> to exit
  7.  
     

学新通

方法2

  1.  
    allure generate ./result/1 -o ./report/1/ --clean
  2.  
    allure open -h 127.0.0.1 -p 8883 ./report/1

浏览器访问地址 http://127.0.0.1:8883/ ,会显示跟上图一样的报告。

allure特性—feature, storry, step

可以在报告中添加用例描述信息,比如测试功能,子功能或场景,测试步骤以及测试附加信息:

  • @allure.feature(‘功能名称’):相当于 testsuite
  • @allure.story(’子功能名称‘):对应这个功能或者模块下的不同场景,相当于 testcase
  • @allure.step('步骤'):测试过程中的每个步骤,放在具体逻辑方法中
    • allure.step('步骤') 只能以装饰器的形式放在类或者方法上面
    • with allure.step:可以放在测试用例方法里面
  • @allure.attach('具体文本信息')
    • 附加信息:数据,文本,图片,视频,网页

测试用例 test_feature_story_step.py:

  1.  
    import pytest
  2.  
    import allure
  3.  
     
  4.  
    @allure.feature("登录")
  5.  
    class TestLogin():
  6.  
    @allure.story("登录成功")
  7.  
    def test_login_success(self):
  8.  
    print("登录成功")
  9.  
    pass
  10.  
     
  11.  
    @allure.story("密码错误")
  12.  
    def test_login_failure(self):
  13.  
    with allure.step("输入用户名"):
  14.  
    print("输入用户名")
  15.  
    with allure.step("输入密码"):
  16.  
    print("输入密码")
  17.  
    print("点击登录")
  18.  
    with allure.step("登录失败"):
  19.  
    assert '1' == 1
  20.  
    print("登录失败")
  21.  
    pass
  22.  
     
  23.  
    @allure.story("用户名密码错误")
  24.  
    def test_login_failure_a(self):
  25.  
    print("用户名或者密码错误,登录失败")
  26.  
    pass
  27.  
     
  28.  
     
  29.  
    @allure.feature("注册")
  30.  
    class TestRegister():
  31.  
    @allure.story("注册成功")
  32.  
    def test_register_success(self):
  33.  
    print("测试用例:注册成功")
  34.  
    pass
  35.  
     
  36.  
    @allure.story("注册失败")
  37.  
    def test_register_failure(self):
  38.  
    with allure.step("输入用户名"):
  39.  
    print("输入用户名")
  40.  
    with allure.step("输入密码"):
  41.  
    print("输入密码")
  42.  
    with allure.step("再次输入密码"):
  43.  
    print("再次输入密码")
  44.  
    print("点击注册")
  45.  
    with allure.step("注册失败"):
  46.  
    assert 1 1 == 2
  47.  
    print("注册失败")
  48.  
    pass
学新通

用例执行、生成报告

  1.  
    pytest test_feature_story.py --alluredir=./result/2
  2.  
    allure generate ./result/2 -o ./report/2/ --clean
  3.  
    allure open -h 127.0.0.1 -p 8883 ./report/2

报告:

学新通

allure特性—link, issue, testcase

可以在测试报告中添加链接、bug地址、测试用例地址。

关联bug需要在用例执行时添加参数:

test_allure_link_issue.py:

  1.  
    import allure
  2.  
     
  3.  
    @allure.link("http://www.百度.com", name="百度 link")
  4.  
    def test_with_link():
  5.  
    pass
  6.  
     
  7.  
    @allure.issue("140","this is a issue")
  8.  
    def test_with_issue_link():
  9.  
    pass
  10.  
     
  11.  
    TEST_CASE_LINK = 'https://github.com'
  12.  
    @allure.testcase(TEST_CASE_LINK, 'Test case title')
  13.  
    def test_with_testcase_link():
  14.  
    pass

用例执行:

  1.  
    pytest test_allure_link_issue.py --allure-link-pattern=issue:http://www.bugfree.com/issue/{} --alluredir=./result/3
  2.  
    allure serve ./result/3

报告:

学新通

点击 this is a issue,页面会跳转到bug页面:http://www.bugfree.com/issue/140

allure特性—severity

有时候在上线前,由于时间关系,我们只需要把重要模块测试一遍,在这样的场景下我们怎么实现呢?主要有三种方法:

  1. 可以使用pytest.mark来标记用例,Pytest测试框架(一):pytest安装及用例执行 介绍了这种方法。

    1.  
      @pytest.mark.webtest # 添加标签
    2.  
      @pytest.mark.sec
    3.  
      pytest -m "webtest and not sec"
  2. 通过 allure.feature, allure.story来实现

    1.  
      pytest test_feature_story_step.py --allure-features "登录" //只运行登录模块
    2.  
      pytest test_feature_story_step.py --allure-stories "登录成功" //只运行登录成功子模块
  3. 通过 allure.severity按重要性级别来标记,有5种级别:

    • Blocker级别:阻塞

    • Critical级别:严重

    • Normal级别:正常

    • Minor级别:不太重要

    • Trivial级别:不重要

test_allure_severity.py:

  1.  
    import allure
  2.  
    import pytest
  3.  
     
  4.  
    def test_with_no_severity_label():
  5.  
    pass
  6.  
     
  7.  
    @allure.severity(allure.severity_level.TRIVIAL)
  8.  
    def test_with_trivial_severity():
  9.  
    pass
  10.  
     
  11.  
    @allure.severity(allure.severity_level.NORMAL)
  12.  
    def test_with_normal_severity():
  13.  
    pass
  14.  
     
  15.  
    @allure.severity(allure.severity_level.NORMAL)
  16.  
    class TestclassWithNormalSeverity(object):
  17.  
    def test_inside_the_normalseverity_test_class(self):
  18.  
    pass
  19.  
     
  20.  
    @allure.severity(allure.severity_level.CRITICAL)
  21.  
    def test_inside_the_normal_severity_test_class_with_overriding_critical_severity(self):
  22.  
    pass
学新通

用例执行:

  1.  
    pytest test_allure_severity.py --alluredir=./result/4 --allure-severities normal,critical
  2.  
    allure serve ./result/4

结果:

学新通

allure.attach()

可以在报告中附加文本、图片以及html网页,用来补充测试步骤或测试结果,比如错误截图或者关键步骤的截图。

test_allure_attach.py:

  1.  
    import allure
  2.  
    import pytest
  3.  
     
  4.  
    def test_attach_text():
  5.  
    allure.attach("纯文本", attachment_type=allure.attachment_type.TEXT)
  6.  
     
  7.  
    def test_attach_html():
  8.  
    allure.attach("<body>这是一段htmlbody块</body>", "html页面", attachment_type=allure.attachment_type.HTML)
  9.  
     
  10.  
    def test_attach_photo():
  11.  
    allure.attach.file("test.jpg", name="图片", attachment_tye=allure.attachment_type.JPG)

用例执行:

  1.  
    pytest test_allure_attach.py --alluredir=./result/5
  2.  
    allure serve ./result/5

结果:

学新通

pytest selenium allure报告

测试步骤:

  1. 打开百度
  2. 搜索关键词
  3. 搜索结果截图,保存到报告中
  4. 退出浏览器

test_allure_百度.py:

  1.  
    import allure
  2.  
    import pytest
  3.  
    from selenium import webdriver
  4.  
    import time
  5.  
     
  6.  
    @allure.testcase("http://www.github.com")
  7.  
    @allure.feature("百度搜索")
  8.  
    @pytest.mark.parametrize('test_data1', ['allure', 'pytest', 'unittest'])
  9.  
    def test_steps_demo(test_data1):
  10.  
    with allure.step("打开百度网页"):
  11.  
    driver = webdriver.Chrome("D:/testing_tools/chromedriver85/chromedriver.exe")
  12.  
    driver.get("http://www.百度.com")
  13.  
     
  14.  
    with allure.step("搜索关键词"):
  15.  
    driver.find_element_by_id("kw").send_keys(test_data1)
  16.  
    time.sleep(2)
  17.  
    driver.find_element_by_id("su").click()
  18.  
    time.sleep(2)
  19.  
     
  20.  
    with allure.step("保存图片"):
  21.  
    driver.save_screenshot("./result/b.png")
  22.  
    allure.attach.file("./result/b.png", attachment_type=allure.attachment_type.PNG)
  23.  
    allure.attach('<head></head><body>首页</body>', 'Attach with HTML type', allure.attachment_type.HTML)
  24.  
     
  25.  
    with allure.step("退出浏览器"):
  26.  
    driver.quit()
学新通

用例执行:

  1.  
    pytest test_allure_百度.py --alluredir=./result/6
  2.  
    allure serve ./result/6

结果:

学新通

--THE END--


最后: 为了回馈铁杆粉丝们,我给大家整理了完整的软件测试视频学习教程,朋友们如果需要可以自行免费领取 【保证100%免费】

学新通

软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

学新通

学新通

这篇好文章是转载于:编程之路

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 编程之路
  • 本文地址: /boutique/detail/tanhhgbbbh
系列文章
更多 icon
同类精品
更多 icon
继续加载