微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pytest+allure 生成报告基础

参考链接安装和入门 — 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

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")

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐