ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

pytest+allure 生成报告(基础)

2022-02-22 17:06:34  阅读:268  来源: 互联网

标签:allure -- py 生成 pytest test main


参考链接:安装和入门 — pytest documentation

一、关于pytest

0.安装

pip install -U pytest 或者 在pycharm中安装pytest

 -u代表如果安装了升级到最新版

查看版本

pytest --version

1.关于pytest

        按照一定顺序执行文件和类、方法((小写英文--->大写英文--->0-9数字))

  • 测试文件以test_开头(以_test结尾也可以)
  • 测试类以Test开头,并且不能带有 init 方法
  • 测试函数以test_开头
  • 断言使用基本的assert即可

2.运行方式

1)通过主函数启动,可以在运行的文件中写主函数,像这样

        主函数启动需要引入pytest包,不然找不到pytest方法)

import pytest

class TestClass:
    def test_one(self):
        x = "this"
        assert "h" in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, "check")

if __name__ == '__main__':
#    pytest.main()
    pytest.main("test_study.py")

pytest.main()会自动读取当前目录下的所有test开头的.py文件,运行test方法或者类

可以传入不同的参数,让运行更加定制化

pytest.main(['./'])               # 运行./目录下所有(test_*.py  和 *_test.py)
pytest.main (['./subpath1'])    # 运行./subpath1 目录下用例
pytest.main (['./subpath1/test_module1.py'])    # 运行指定模块
pytest.main (['./subpath1/test_module1.py::test_m1_1'])  # 运行模块中的指定用例
pytest.main (['./subpath2/test_module2.py::TestM2::test_m2_02'])  # 运行类中的指定用例
pytest.main (['-k','pp'])         # 匹配包含pp的用例(匹配目录名、模块名、类名、用例名)
pytest.main(['-k','spec','./subpath1/test_module1.py'])     # 匹配test_module1.py模块下包含spec的用例
pytest.main(['-k','pp','./subpath2/test_module2.py::TestM2'])   # 匹配TestM2类中包含pp的用例

2)通过控制台启动

选择pycharm>terminal

  • pytest test_mod.py 执行该模块下的测试类测试方法
  • pytest testing 执行该文件夹下的所有模块
  • pytest test_cgi.py::test_answer,执行test_answer方法
  • pytest test_cgi.py::TestMyClass::test_one,执行TestMyClass类下的test_one方法产
  • 查看详细的输出:-r随意拼接下方字符 例如-rfp  -fa (展示全部的用例情况)pytest -ra study.py
    • f -失败
    • E -错误
    • s -跳过
    • x -xfailed
    • X -xpass
    • p -通过
    • P -通过输出

3.查看通过时长

        通过-vv查看每个用例通过的时长和详细情况

        pytest -vv test_study.py

        获取长度超过1s的最慢10个测试用例持续时间的列表

        pytest --durations=10 --durations-min=1.0 -vv test_study.py

        

4.断言  

pytest 允许您使用标准的python assert 用于验证Python测试中的期望和值,pytest 支持显示最常见的子表达式的值

def f():
    return 3


def test_function():
    assert f() == 4

二、allure

1、安装alluer+ allure-pytest

        1)下载allure 的zip包,下载后放到python文件夹lib下        

        2)进入allure 的bin目录下,复制路径,放到path环境变量

        3)安装alluer-pytest

                pip install allure-pytest

 2、生成报告(terminal)

        1)terminal启动用例,指定数据存在什么文件夹下(目前是存在当前文件夹下的tmp文件夹)

        pytest --alluredir=tmp test_study.py

        2)查看报告

               2.1) 直接打开生成数据图表:allure serve 生成数据文件目录

                注意:如果直接在pycharm中启动命令行,需要定位到allure.bat的详细位置

                D:\python\Lib\allure-2.9.0\bin\allure.bat allure serve tmp

                2.2)生成一个文件夹,存储html,进入report打开html查看报告

                D:\python\Lib\allure-2.9.0\bin\allure.bat generate tmp -o report --clean

                D:\python\Lib\allure-2.9.0\bin\allure.bat open report

3、生成报告(main)

if __name__=="__main__":

# --alluredir  生成allure报告需要的数据文件
    pytest.main(['test_study.py','-s', "--alluredir=tmp"])

# allure generate 生成allure测试报告默认生成一个文件夹allure-result; --clean保证每一次生成的html是最新的数据
    os.system("allure generate tmp --clean")

标签:allure,--,py,生成,pytest,test,main
来源: https://blog.csdn.net/weixin_44867493/article/details/123062129

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有