pytest常用功能

一、用例设计原则

  • 文件名以test_*.py文件和*_test.py
  • 以test_开头的函数
  • 以Test开头的类,test_开头的方法,并且不能带有__init__ 方法
  • 所有的包pakege必须要有__init__.py文件
  • 断言使用assert

二、执行用例规则

  1. 执行某个目录下所有的用例

pytest 文件名/

  1. 执行某一个py文件下用例

pytest 脚本名称.py

  1. -k 按关键字匹配

pytest -k "MyClass and not method"

  1. 按节点运行

每个收集的测试都分配了一个唯一的nodeid,它由模块文件名和后跟说明符组成
来自参数化的类名,函数名和参数,由:: characters分隔。

运行.py模块里面的某个函数

pytest test_mod.py::test_func

运行.py模块里面,测试类里面的某个方法

pytest test_mod.py::TestClass::test_method

三、测试报告

pytest --html=report.html
执行完之后,在当前目录会生成一个report.html的报告文件,显示效果如下

pytest --html=./report/report.html
直接执行"pytest --html=report.html"生成的报告会在当前脚本的同一路径,如果想指定报告的存放位置,放到当前脚本的同一目录下的report文件夹里

pytest --html=report.html --self-contained-html
上面方法生成的报告,css是独立的,分享报告的时候样式会丢失,为了更好的分享发邮件展示报告,可以把css样式合并到html里

四、ini配置文件

pytest里面有些文件是非test文件

  • pytest.ini pytest的主配置文件,可以改变pytest的默认行为
  • conftest.py 测试用例的一些fixture配置
  • init.py 识别该文件夹为python的package包
  • tox.ini 与pytest.ini类似,用tox工具时候才有用
  • setup.cfg 也是ini格式文件,影响setup.py的行为

五、allure测试报告

安装
命令:
生成json格式的报告
pytest --alluredir ./report/allure_raw
先清除原有的json报告,然后生成新的
pytest --clean-alluredir --alluredir ./report/allure_raw
整合json报告,生成html测试报告(在pycharm执行可能会报错,需要cd到下一层级目录执行对应命令,比如:allure serve allure_raw
allure serve report/allure_raw

六、allure命令

https://www.jianshu.com/p/dae3ea83d1e3
浏览器打开allure报告的两种方式:

# 在控制台执行
pytest --alluredir=../reports --clean-alluredir
# 打开allure报告
allure serve ../reports
# 生成allure的html报告
allure generate ../reports/ -o ../reports/html --clean
打开allure报告
allure open -h 127.0.0.1 -p 8083 ../reports/html
# 主函数中执行
pytest.main(["-s", "test_web_project_manager.py", "--alluredir=../reports", "--clean-alluredir"])
os.system('allure serve ../reports')

py文件执行测试用例

https://www.cnblogs.com/yoyoketang/p/14032069.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、pytest单元测试框架 1、什么是单元测试框架 单元测试框架是指在软件测试开发当中,针对软件的最小单位(函数...
    JaydenGoh阅读 545评论 0 2
  • 导包 import unittest 测试类以Test开头 测试方法名称必须以test开头 pytest命令行运行...
    xiaohan_zhang阅读 420评论 0 0
  • 前提:需要安装pytest和pytest-html(生成html测试报告) pip install pytest ...
    5888eb1818d9阅读 299评论 0 0
  • 前提:需要安装pytest和pytest-html(生成html测试报告) pip install pytest ...
    zflain阅读 175评论 0 0
  • 一、命名规则Pytest单元测试中的类名和方法名必须是以test开头,执行中只能找到test开头的类和方法,比un...
    老友_9e40阅读 283评论 0 0