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文件方法无需导入
相关推荐
子午9 分钟前
【2026计算机毕设】水果识别分类系统~python+深度学习+人工智能+算法模型+TensorFlow
人工智能·python·深度学习
No0d1es13 分钟前
2023年NOC大赛创客智慧编程赛项Python复赛模拟题(二)
python·青少年编程·noc·复赛·模拟题
SmartRadio17 分钟前
ESP32-S3实现KVM远控+云玩功能 完整方案
运维·python·计算机外设·esp32·kvm·云玩
治愈系科普19 分钟前
数字化种植牙企业
大数据·人工智能·python
AI数据皮皮侠19 分钟前
中国植被生物量分布数据集(2001-2020)
大数据·人工智能·python·深度学习·机器学习
a程序小傲31 分钟前
京东Java面试被问:基于Gossip协议的最终一致性实现和收敛时间
java·开发语言·前端·数据库·python·面试·状态模式
小二·33 分钟前
Python Web 开发进阶实战:AI 原生应用商店 —— 在 Flask + Vue 中构建模型即服务(MaaS)与智能体分发平台
前端·人工智能·python
tqs_1234533 分钟前
Spring Boot 的自动装配机制和 Starter 的实现原理
开发语言·python
好好学习啊天天向上35 分钟前
conda pip更新安装路径,解决C盘容易不够的问题
python·conda·pip·2025yfb3003605
~kiss~37 分钟前
多头注意力中的张量重塑
pytorch·python·深度学习