lsekfe 发表于 2021-5-26 09:56:52

如何通过python生成Allure报告

前言
  自动化执行结果都需要有个报告来展示,常用的报告有HTMLTestRunner,pytest-html,Allure。这三种报告各有不同,本文主要介绍如何通过python来生成Alluer报告。

  Allure
  Alluer属于一种开源的测试框架,可以基于多种语言进行使用,如java的junit,python的pytest。alluer生成的报告内容比较美观,目前测试中常用的测试报告。Allure的环境是基于java环境的,前提条件需要将java的环境安装完成(下载JDK,配置环境变量)。
  下载地址:https://github.com/allure-framework/allure2
  下载完成进行解压,并将allure路径下的bin文件进行放入到环境变量中。打开终端查看是否配置完成,,返回了版本号,说明已经配置完成了。

E:\>allure --version
2.13.1





  由于我们是通过pytest进行生成alluer报告的,也需要进行安装alluer在pytest中的第三方插件allure-pytest
  安装插件:pip install allure-pytest
  先编写几个测试用例:

import pytest

class TestCase:
    def test_01(self):
      print('---用例01---')
      assert 1
    def test_02(self):
      print('---用例02---')
      assert 1
    def test_03(self):
      print('---用例03---')
      assert 0
if __name__ == '__main__':
    pytest.main(['-s'])





  生成报告
  用例编写完成后,想要生成allure报告,需要输入命令。

--alluredir ./report/result
# 其中./report/result表示将生成的报告存放目录





  注意:这里通过allure-pytest只是帮助我们将测试结果创建出来,但是无法生成HTML格式且生成的测试数据不会清空,而是以追加的形式。
  执行完成上述命令,可以看到在当前目录中生成了report文件,文件下展示的有一些json文件,这些文件属于我们执行的测试结果内容。
http://www.51testing.com/attachments/2021/05/15326825_202105201645521Hu0V.png

  想要生成对应的测试报告,需要再次在终端中输入对应命令。

  方法一:

allure generate 生成测试结果数据 -o 生成报告的路径 --clean
# --clean表示:如果已经存在生成报告路径文件夹时,再次使用会提示添加--clean参数来重写
# 如以下编写用例命令
allure generate report/result/ -o report/html --clean





  打开生成的报告路径中的HTML文件,就可以看到allure报告内容了。
http://www.51testing.com/attachments/2021/05/15326825_202105201654281kmhI.png

  如果不想去打开html报告的话,也可以通过打开终端执行命令进行自动打开alluer报告。

allure open生成报告的路径地址
E:\auto_test\test_01>allure generate report/result/ -o report/html --clean
Report successfully generated to report\html
# 这样就会自动打开allure报告信息





  方法二:
  有人说上面的方法有点复杂,那安静在介绍一种,当pytest携带allure的参数执行完测试结果后,直接打开终端输入命令就可以打开报告内容。

E:\auto_test\test_01>allure serve report/result
Generating report to temp directory...
Report successfully generated to C:\Users\HAIYAN~1\AppData\Local\Temp\6715359970756727599\allure-report
Starting web server...
# 命令中allure serve 表示确定一个allure的服务,后面跟的是报告的路径内容



http://www.51testing.com/attachments/2021/05/15326825_202105201654282d0vE.png


  allure中增加描述信息
  光看页面美观不能展示allure强大,allure也可以对报告中增加一些测试详情,如用例标题哈,测试步骤等内容。

  用例标题
  在allure中增加用例标题,需要在代码中导入allure模块。
  使用方法直接在需要装饰的用例上使用装饰器。

import allure

class TestCase:

    @allure.title('登录用例')
    def test_01(self):
      print('---用例01---')
      assert 1

    def test_02(self):
      print('---用例02---')
      assert 1

    @allure.title('退出登录用例')
    def test_03(self):
      print('---用例03---')
      assert 1





  通过上述代码可以看出用例1和用例3中增加了标题内容,我们直接执行生成测试报告。先生成测试结果,在启动allure直接打开报告内容。
http://www.51testing.com/attachments/2021/05/15326825_202105201654283Dl7P.png

  通过打开的报告发现,标题内容已经成功添加上去了。
http://www.51testing.com/attachments/2021/05/15326825_202105201654284KsD4.png

  测试模块中增加标签
  allure可以在测试模块中增加测试标签内容。
  通过装饰器的方法来放在对应的测试用例中,这里allure.step()由于需要标记在测试步骤中,由于加入到测试用例中,使用了with的方法来操作。

import pytest
import allure
class TestCase:
    @allure.title('登录用例')
    def test_01(self):
      '''登录用例_操作步骤'''
      with allure.step('输入正确的用户名'):
            print('输入用户名')
      with allure.step('输入正确的密码'):
            print('输入密码')
      with allure.step('点击登录'):
            print('点击登录!')
      assert 1
    @allure.title('退出登录用例')
    def test_02(self):
      '''退出登录_操作步骤'''
      with allure.step('点击退出按钮'):
            print('成功点击退出按钮')
      assert 1
if __name__ == '__main__':
    pytest.main(['-s'])





  直接通过生成报告,通过报告内容可以看出在用例中添加的详细内容,已经全部都展示出来了。
http://www.51testing.com/attachments/2021/05/15326825_202105201654285Evgx.png

  总结
  通过整篇文章相信对allure也有了一定的了解,可以看出allure确实是比unittest和pytest-html强大很多。希望本篇文章对您有所帮助。

千里 发表于 2021-6-2 11:56:29

allure是必须掌握的知识点
页: [1]
查看完整版本: 如何通过python生成Allure报告