当前位置: 首页 > news >正文

pytest 常用的辅助函数和工具函数

pytest 常用的辅助函数和工具函数示例

# @File: my_module.pydef fetch_data():return 'process data'def process_data():data = fetch_data()return data.upper()
import logging
import sys
import pytest#01-------------------------------@pytest.fixture,sample_data 在测试函数中被调用,以提供必要的测试准备工作或资源
@pytest.fixture
def sample_data():arr = [1,2,3]return arrdef test_data(sample_data):res = sample_data[0]assert res == 1#02-------------------------------pytest.raises 测试代码是否引发了预期的异常
def test_raise_divide():with pytest.raises(ZeroDivisionError):1 / 0#03-------------------------------@pytest.mark.xfail 用于标记预期会失败的测试,测试失败不会影响整体测试结果
@pytest.mark.xfail
def test_expect_fail():assert 1 == 2
''' 
#04-------------------------------import pdb; pdb.set_trace() 用于在测试中插入断点,方便调试
def test_debug():## import pdb; pdb.set_trace()assert 1 == 1
'''#05-------------------------------@pytest.mark.usefixtures("setup_1", "setup_2")  在测试函数中应用多个夹具
@pytest.fixture
def setup_1():print('fixture 1')@pytest.fixture
def setup_2():print('fixture 2')@pytest.mark.usefixtures("setup_1", "setup_2")
def test_with_mul_fixtures():assert 1== 1#06-------------------------------@pytest.mark.timeout  设置测试的超时时间,避免测试运行过长时间
@pytest.mark.timeout(1)
def test_long_running():import timetime.sleep(2)#07-------------------------------@pytest.mark.filterwarnings  用于过滤警告信息,控制哪些警告被显示或忽略
@pytest.mark.filterwarnings("ignore::UserWarning")
def test_ignore_warning():import warningswarnings.warn("This is a warning", UserWarning)#08------------------------------ pytest.config  用于获取或修改 pytest 配置,虽然在较新版本中通常使用 pytest 插件系统替代
def test_config():config = pytest.configassert config.option.verbose#09------------------------------@pytest.mark.order()控制测试的执行顺序(需要 pytest-order 插件)
@pytest.mark.order(1)
def test_first():assert 1==1@pytest.mark.order(2)
def test_second():assert 1==1#10------------------------------pytest.capture 的 caplog 用于捕获日志输出并进行断言
def test_logging(caplog):logger = logging.getLogger('test_logger')logger.warning('this is a warning')assert 'this is a warning' in caplog.text#11------------------------------pytest.fixture 的 autouse,  自动使用夹具,而无需在测试函数中显式声明
@pytest.fixture(autouse=True)
def auto_fixture():print('this runs before each test')def test_example():assert True#12------------------------------pytest.mark.skipif 在特定条件下跳过测试
@pytest.mark.skipif(sys.platform=='win64', reason='Requires Unix-like OS')
def test_unix_only_feature():assert True#13------------------------------pytest 的 monkeypatch 用于在测试运行时动态地修改或模拟对象、方法、类等。这可以帮助你隔离测试环境、模拟依赖项,或者控制外部依赖的行为from TestCases.ModelG.my_module import process_datadef mock_fetch_data():return 'mock data'def test_process_data(monkeypatch):# 使用 monkeypatch 来模拟 fetch_data 函数monkeypatch.setattr("TestCases.ModelG.my_module.fetch_data",mock_fetch_data)res = process_data()assert res == 'MOCK DATA'

 

test_data.py::test_data 
test_data.py::test_raise_divide 
test_data.py::test_expect_fail 
test_data.py::test_with_mul_fixtures 
test_data.py::test_long_running 
test_data.py::test_ignore_warning 
test_data.py::test_config 
======= Global initialization =======
this runs before each test
PASSED                                           [  7%]this runs before each test
PASSED                                   [ 15%]this runs before each test
XFAIL                                     [ 23%]
@pytest.mark.xfail
    def test_expect_fail():
>       assert 1 == 2
E       assert 1 == 2

test_data.py:35: AssertionError
this runs before each test
fixture 1
fixture 2
PASSED                              [ 30%]this runs before each test
PASSED                                   [ 38%]this runs before each test
PASSED                                 [ 46%]this runs before each test
FAILED                                         [ 53%]
TestCases\ModelG\test_data.py:69 (test_config)
def test_config():
>       config = pytest.config
E       AttributeError: module 'pytest' has no attribute 'config'

test_data.py:71: AttributeError
this runs before each test
PASSED                                          [ 61%]this runs before each test
PASSED                                         [ 69%]this runs before each test
PASSED                                        [ 76%]this runs before each test
PASSED                                        [ 84%]this runs before each test
PASSED                              [ 92%]this runs before each test
PASSED                                   [100%]

http://www.lryc.cn/news/431117.html

相关文章:

  • 记录Java秋招面经(网上找的)
  • 记录k8s重启之后kubelet无法启动的问题
  • IA——网络操作设备VRP简介
  • Java项目: 基于SpringBoot+mysql企业客户管理系统(含源码+数据库+答辩PPT+毕业论文)
  • 基于STM32设计的智能安防系统(微信小程序)(218)
  • tomcat redis minio nginx windows开机自启
  • Docker构建镜像教程
  • 扑捉一只耿鬼(HTML文件)
  • Address localhost:1099 is already in use:tomcat频繁重启端口占用问题
  • HTTPS SEO优势
  • UE5 C++ 读取图片插件(一)
  • C语言行地址列地址区别,内存的分配
  • Unity 一键修改图片缩放保存为当前的一半大小
  • Identifying User Goals from UI Trajectories论文学习
  • [STM32]从零开始的STM32标准库环境搭建(小白向)
  • 解决 Android 上的 .NET MAUI/Xamarin.AndroidX 应用调用 ASP.NET Core API 端点时 SSL 连接被拒绝的问题
  • 助贷CRM系统:为金融中介行业打造全新营销管理模式
  • HBase 部署及shell操作
  • 使用Fign进行客户端远程调用和SpringFormEncoder的使用
  • golang 数据库使用注意事项
  • Leetcode面试经典150题-221.最大正方形
  • 51单片机-DS1302,操作简述
  • Vue3+Vite+Echarts 出现Missing semicolon错误
  • iOS——frame和bounds的区别
  • Trm理论 3(注意力机制)
  • Vue2和Vue3项目创建的区别和 element ui 和element plus的导入方式
  • 基于STM32的猫狗宠物喂养系统设计(微信小程序)(215)
  • spark读取csv文件
  • 钢铁百科:Q420DR力学性能、Q420DR执行标准、Q420DR低温容器钢板
  • 三菱机器人手柄维修示教器维修手操器面板等