美文网首页
pytest常用功能

pytest常用功能

作者: 冲锋丘丘人 | 来源:发表于2024-02-18 15:07 被阅读0次

一、用例设计原则

  • 文件名以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常用功能

      本文链接:https://www.haomeiwen.com/subject/yqapadtx.html