python生成测试报告_python+allure2生成测试报告

安装allure-pytest

allure-pytest是python的一个第三方库。用于连接pytest和allure,使它们可以配合在一起使用。

allure-pytest基于pytest的原始执行结果生成适用于allure的json格式结果。该json格式结果可以用于后续适用allure生成html结果。

命令行或者终端中输入 pip install allure-pytest 即可进行安装:

1$ pip install allure-pytest

实例

基于pytest写个测试文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20def func(x):

return x + 3

def test_func_0():

src = 0

expect = 3

assert func(src) == expect

def test_func_1():

src = 0.1

expect = 3.1

assert func(src) == expect

def test_func_2():

src = -1

expect = 0

assert func(src) == expect

运行pytest和allure生成报告

打开终端terminal,切到测试文件所在目录。假设文件名为test_demo.py。

生成json格式运行结果

运行命令 pytest –alluredir=report test_demo.py 。

命令中的 –alluredir=report 指明了生成的json结果文件存放的目录为当前目录下的report文件夹

命令的运行结果如下图所示:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23E:\Python\httpRunnerDemo>pytest --alluredir=report testcases/test_user_login.py

============================================================================================ test session starts ============================================================================================

platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1

rootdir: E:\Python\httpRunnerDemo

plugins: allure-pytest-2.8.19, asyncio-0.14.0, cov-2.10.1, html-2.1.1, metadata-1.10.0

collected 3 items

testcases\test_user_login.py ..F [100%]

================================================================================================= FAILURES ==================================================================================================

________________________________________________________________________________________________ test_func_2 ________________________________________________________________________________________________

def test_func_2():

src = -1

expect = 0

> assert func(src) == expect

E assert 2 == 0

E + where 2 = func(-1)

testcases\test_user_login.py:20: AssertionError

========================================================================================== short test summary info ==========================================================================================

FAILED testcases/test_user_login.py::test_func_2 - assert 2 == 0

======================================================================================== 1 failed, 2 passed in 0.19s ========================================================================================

基于pytest捕捉到的测试用例,每个用例的执行结果会生成一个json文件。如下图所示:

使用allure生成最终的测试报告

运行命令 allure generate report。

这个命令会将 report 文件夹下的json文件渲染成网页结果,方便观看。生成的网页结果默认保存在当前文件夹下的 allure-report 文件夹内:

1

2E:\Python\httpRunnerDemo>allure generate report --clean

Report successfully generated to allure-report

--clean表示清除上一次生成的报告内容。

生成结果如下图所示:

如果直接在文件夹中直接打开index.html,是看不到实际内容的,这是因为实际内容需要 allure 进行渲染后才能看到。

可使用pycharm直接打开index.html,即可看到美观的测试报告:

或者使用allure内置的命令。allure使用了两种方式来渲染页面。分别是allure open 和 allure serve。前者用于在本地渲染和查看结果,后者用于在本地渲染后对外展示结果。这里我们使用allure open。运行命令 allure open allure-report即可自动打开浏览器展示渲染好的结果。

1

2

3

4E:\Python\httpRunnerDemo>allure open allure-report

Starting web server...

2020-11-03 14:10:44.604:INFO::main: Logging initialized @605ms to org.eclipse.jetty.util.log.StdErrLog

Server started at . Press to exit

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值