D99【python 接口自动化学习】- pytest进阶之fixture用法

day99 pytest使用conftest管理fixture

学习日期:20241216

学习目标:pytest基础用法 -- pytest使用conftest管理fixture

学习笔记:

fixture(scope="function")

conftest.py为固定写法,不可修改名字,使用conftest.py文件方法无需导入,函数作用于当前文件夹及下属文件

  • pytest使用fixture返回数据,可以接收返回值
python 复制代码
conftest.py

@pytest.fixture(scope="function")
def get_params():
    params = {'key1': 'value1', 'key2': 'value2'}
    return params
python 复制代码
test_fixture_return.py

import pytest
import requests


def test_getmobile(get_params):
    print("测试get请求")
    #第1种方法
    # r=requests.get('https://httpbin.org/get',params=get_params)

    #第2种方法
    key1 = get_params['key1']
    key2 = get_params['key2']
    r=requests.get('https://httpbin.org/get',params={'key1':key1, 'key2':key2})
    print(r.status_code)
    assert r.status_code == 200
    res = r.json()
    assert res['url'] == 'https://httpbin.org/get?key1=value1&key2=value2'
    assert res['origin'] == '163.125.202.248'
    assert res['args']['key1'] == 'value1'
    assert res['args']['key2'] == 'value2'
总结
  1. conftest.py为固定写法,不可修改名字,使用conftest.py文件方法无需导入
相关推荐
AsDuang24 分钟前
Python 3.12 MagicMethods - 28 - __rsub__
开发语言·python
李可以量化42 分钟前
用 KMeans 聚类寻找股票支撑位与压力位(上):基于 QMT 量化平台实现
python·量化 qmt ptrade
所谓伊人,在水一方33343 分钟前
【Python数据科学实战之路】第12章 | 无监督学习算法实战:聚类与降维的奥秘
python·sql·学习·算法·信息可视化·聚类
MoRanzhi12031 小时前
Pillow 灰度化、二值化与阈值处理
图像处理·python·pillow·二值化·图像预处理·阈值处理·灰度化
飞Link1 小时前
告别复杂调参:Prophet 加法模型深度解析与实战
开发语言·python·数据挖掘
测试人社区—66791 小时前
当代码面临道德选择:VR如何为AI伦理决策注入“人性压力”
网络·人工智能·python·microsoft·vr·azure
独行soc1 小时前
2026年渗透测试面试题总结-36(题目+回答)
网络·python·安全·web安全·网络安全·渗透测试·安全狮
witAI2 小时前
**Kimi小说灵感2025推荐,从零到一的创意激发指南**
人工智能·python
飞Link2 小时前
深度解析:基于专家的监管方法(Expert-Based Supervision)在复杂系统中的应用
python·数据挖掘·回归
Shining05962 小时前
Triton & 九齿系列《Triton 练气术》
开发语言·人工智能·python·学习·其他·infinitensor