pytest运行方式及前置后置封装详解

一、Pytest 优点认知

1.可以结合所有的自动化测试工具

2.跳过失败用例以及失败重跑

3.结合allure生产美观报告

4.和Jenkins持续集成

5.很多强大的插件

  1. pytest-html:生产html测试报告

  2. pytest-xdist:多线程运行

  3. pytest-ordering:改变用例执行顺序

  4. pytest-rerunfailures:失败用例重爬

  5. allure-pytest:美观测试报告

一般项目中,会使用requerments.text文档保存插件名称,进行批量一次性安装

复制代码
pip install -r requerments.txt
二、运行方式

1.主函数运行方式:main方法运行

2.命令运行方式
pytest -vs

-v:更加详细信息

-s:调试信息

-n=处理:多线程运行

--reruns=数字:失败用例重跑

--reruns=数字:失败用例重跑

--html=./report.html:生成html报告

用例分组运行

1.进行用例分组:

2.用例进行注解:

复制代码
#@pytest.mark.分组名称  如下:

@pytest.mark.smoke
  1. [pytest]

  2. ##运行命令,例如: -vs -m "smoke"分组执行名称都是固定的

  3. addopts = -vs

  4. #测试用例文件目录

  5. testpaths = ./testcases

  6. python_files = test_*.py

  7. python_classes = Test*

  8. python_functions = test_*

  9. ##分组

  10. markers =

  11. smoke:maoyan

  12. case:gongneng

三、前置后置,夹具
1.简单区分:直接调用方法,但是接口过多时,比较麻烦
  1. def setup(self):

  2. print("每个用例执行之前,都执行一遍")

  3. def teardown(self):

  4. print("每个用例执行之后,都执行一遍")

2.实现部分前置:如只想之一个用例进行前置,如登录时需要连接数据库。

需要使用装置器:

参数介绍:

@pytest.fixture(scope="作用域",params="数据驱动",autouse="是否自动执行",ids="自定义参数",name="重命名")

作用域:可以函数、类、模块、包、session

使用方法:

1.需要前置的功能函数上进行标注装置器

2.别的方法函数之间调用装置器

如下:一个文件里面进行部分前置唤醒

  1. import time

  2. import pytest

  3. import requests

  4. #实现装置器标注前置,进行标注,yieid进行唤醒返回

  5. @pytest.fixture(scope="function")

  6. def conn_getbase():

  7. print("连接数据库成功")

  8. yield

  9. print("关闭数据库成功")

  10. class TestSendRequsets:

  11. #过多接口时,比较麻烦冗余

  12. # def setup(self):

  13. # print("每个用例执行之前")

  14. #

  15. # def teardown(self):

  16. # print("每个用例执行之后")

  17. def test_getImgCode(self):

  18. # 接口url

  19. t = time.time()

  20. timess = str(int(round(t * 1000)))

  21. times = str(int(t))

  22. url = "http://124.71.230.185:9002/jeecg-boot/sys/randomImage/" + "" + timess

  23. # 参数

  24. data = {

  25. "_t": times,

  26. }

  27. # # get请求

  28. rep = requests.request('get', url, params=data)

  29. print(rep.text)

  30. # 标注为smoke分组用例

  31. @pytest.mark.smoke

  32. def test_Login(self,conn_getbase):

  33. # post请求

  34. url = "http://124.71.230.185:9002/jeecg-boot/sys/login"

  35. # 参数

  36. data = {

  37. "captcha": "Gkak!@#2021",

  38. "checkKey": 1637811815838,

  39. "password": "123456",

  40. "remember_me": 1,

  41. "username": "admin"

  42. }

  43. rep = requests.request('post', url, json=data)

  44. statues = rep.json()["success"]

  45. message = rep.json()["message"]

  46. if statues:

  47. print("")

  48. else:

  49. # raise Exception(message)

  50. print(message)

  51. if __name__ == '__main__':

  52. pytest.main();

3.封装灵活调用

一般情况下:@pytest.fixture()会和conftest.py文件一块使用

conftest.py名称是固定的,功能如下:

1.用处是多个py文件之间共享前置配置。

2.里面的方法在调用时,不需要导入,可以之间调用

3.可以都多个conftest.py文件,也可以有不同的层级

conftest.py文件详情请看下一章

实现:

1.目录下之间创建conftest.py文件

2.把上面的这段代码之间粘贴到conftest.py文件中

  1. # 前置函数

  2. import pytest

  3. @pytest.fixture(scope="function")

  4. def conn_getbase():

  5. print("连接数据库成功")

  6. yield

  7. print("关闭数据库成功")

相关推荐
程序猿追5 小时前
kotlin-retry 接入鸿蒙后,怎样确认它真的会停止重试
开发语言·kotlin·harmonyos
我是人✓5 小时前
Java文件上传三方式对比:从传统到SpringBoot
java·开发语言
智能直播5 小时前
网络RTMP拉流不卡顿、声音不同步,一文讲透缓冲区、时间戳
开发语言·ffmpeg·php·音视频·视频编解码
墨云安全6 小时前
内网资产无法暴露到公网,怎么做安全检测?2026 年内网渗透测试的落地路径
开发语言·安全·网络安全·渗透测试·php
摇滚侠6 小时前
《计算机是如何工作的:人人都能懂的计算机软硬件工作原理》第 9 章 机器码与汇编语言 阅读笔记 9
java·开发语言·笔记
Cc.Y6 小时前
Java零基础入门:深入Java核心:文件IO与序列化全解
java·开发语言·python
jimy18 小时前
c++的 Type Traits (一)
开发语言·c++
Web极客码9 小时前
Python Ollama 本地 Agent 入门与订单查询实践
开发语言·jvm·python
kimnoic9 小时前
PHP 8.2的DNF类型怎么用才规范
开发语言·php
_Evan_Yao9 小时前
前后端联调怎么排查问题?从 Network 到后端日志,一步步定位错误
开发语言·后端·团队开发·需求分析