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文件方法无需导入
相关推荐
万年咸鱼23 分钟前
Java PrintStream 详解:从基础用法到实战技巧
java·开发语言·python
cui_ruicheng35 分钟前
FastAPI 应用开发(二):请求响应、Pydantic 模型与依赖注入
python·fastapi·web
DeepVisionary1 小时前
SoundHound 完成收购 LivePerson:股权对价 4300 万美元,实际总成本约 3.04 亿
python·自动化
deepseek231 小时前
Google Gemini Agentic Video 上线:88% 少 token 的主动取样如何改写长视频理解
python·多模态·ai agent·gemini·视频理解
Zane19941 小时前
只重写了 __eq__,为什么类突然变得不可哈希了?常用魔法方法大盘点
后端·python
2601_962885721 小时前
如何用 Python 做 A 股全市场扫描选股?(多条件筛选实战)
开发语言·python
零域码客1 小时前
一文掌握 jQuery + Flask 全栈核心知识点(前端交互 + 后端接口 + 前后端联调)
前端·python·ajax·flask·jquery·前后端联调·web全栈
BYSJMG1 小时前
计算机毕设选题做什么好?基于大数据的用户美食数据分析与可视化,PySpark预处理+ECharts大屏,含Isolation Forest异常检测算法
大数据·python·信息可视化·数据分析·spark·课程设计·美食
奈斯先生Vector2 小时前
AIGC 视频生成换个拍法:用 Kling Video 把一张人物图变成可剪辑的短故事
开发语言·人工智能·windows·python·aigc·音视频
卷无止境2 小时前
当"精简主义"住进AI编程助手:Ponytail深度解读
后端·python·fastapi