关于python:如何用pytest生成allure报告

4次阅读

共计 1509 个字符,预计需要花费 4 分钟才能阅读完成。

  1. 装置 pytest
    pytest 是 python 的一个第三方单元测试框架,在这里用于生成原始的执行后果。
    命令行或者终端中输出 pip install pytest 即可进行装置。

  1. 装置 allure-pytest
    allure-pytest 是 python 的一个第三方库。用于连贯 pytest 和 allure,使它们能够配合在一起应用。
    allure-pytest 基于 pytest 的原始执行后果生成实用于 allure 的 json 格局后果。该 json 格局后果能够用于后续实用 allure 生成 html 后果。
    命令行或者终端中输出 pip install allure-pytest 即可进行装置。

  1. 装置 allure
    这个工具次要用来把测试用例的运行后果转换成 html 格局
    能够去 GitHub 上下载:https://github.com/allure-fra…
    在这里插入图片形容


    下载后解压,把 bin 目录增加至 path 环境变量

    代码演示

    代码如下,test_allure_demo.py

     # encoding: utf-8 
     """
     @File    : test_allure_demo.py
     @Author  : 灵枢
     @Time    : 2020/4/13 5:05 PM
     @Desc    :  
     """
     import allure
     
     @allure.step("步骤 1:关上百度")
     def step_1():
     print("111")
     
     @allure.step("步骤 2:输出关键字")
     def step_2():
     print("222")
     
     @allure.feature("搜寻")
     class TestEditPage():
     @allure.story("百度搜寻")
     def test_1(self):
     '''这是测试百度搜寻'''
     step_1()
     step_2()
     print("百度一下,你就晓得")
     
     @allure.story("谷歌搜寻")
     def test_2(self):
     '''这是测试谷歌搜寻'''
     assert 1 == 2, "搜寻失败"
    

在 PyCharm 的 Terminal 窗口运行:
先切换到测试代码的目录下,而后执行命令:

pytest test_allure_demo.py --alluredir ./report
  1. 生成报告
    a. 关上终端 terminal,切到测试文件所在目录。
    b. 生成 json 格局运行后果

    运行命令 pytest --alluredir=report

    命令中的 –alluredir=report 指明了生成的 json 后果文件寄存的目录为当前目录下的 report 文件夹
    基于 pytest 捕捉到的测试用例,每个用例的执行后果会生成一个 json 文件。如下图所示:

    c. 应用 allure 生成最终的测试报告

     运行命令 allure generate report。

    这个命令会将 report 文件夹下的 json 文件渲染成网页后果,不便观看。生成的网页后果默认保留在以后文件夹下的 allure-report 文件夹内。


  1. 关上生成的网页报告
    allure-report 文件夹, 这里的 index.html 就是最终的后果页面。但间接通过浏览器关上这个文件是看不到理论内容的,这是因为理论内容须要 allure 进行渲染后能力看到。间接关上 index.html,浏览器窗口如下图所示:

想要看到理论内容,须要应用 allure 内置的命令。allure 应用了两种形式来渲染页面。别离是 allure open 和 allure serve。前者用于在本地渲染和查看后果,后者用于在本地渲染后对外展现后果。这里咱们应用 allure open。运行命令 allure open allure-report 即可主动关上浏览器展现渲染好的后果。这里的 allure-report 为 allure generate 生成的后果所在目录。

正文完
 0