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文件方法无需导入
相关推荐
财经资讯数据_灵砚智能14 分钟前
基于全球经济类多源新闻的NLP情感分析与数据可视化(日间)2026年4月23日
大数据·人工智能·python·信息可视化·自然语言处理
yuanmazhiwu14 分钟前
计算机毕业设计:Python股市行情可视化与ARIMA预测系统 Flask框架 ARIMA 数据分析 可视化 大数据 大模型(建议收藏)✅
大数据·python·数据分析·django·flask·课程设计
AC赳赳老秦21 分钟前
OpenClaw与Excel联动:批量读取/写入数据,生成可视化报表
开发语言·python·excel·产品经理·策略模式·deepseek·openclaw
覆东流23 分钟前
第5天:Python字符串操作进阶
开发语言·后端·python
NotFound48625 分钟前
分享实战中Python Web 框架对比:Django vs Flask vs FastAPI
前端·python·django
kongba0071 小时前
vibe coding的测试架构设计提示词V1.0
python
qq_283720051 小时前
2026 最新 Python+AI 零基础入门实战教程:从零搭建企业级人工智能项目
人工智能·python·#机器学习·#python #ai零基础·#大模型开发·#rag·#ai避坑
贵沫末1 小时前
Python——图像处理项目Conda环境搭建
开发语言·python·conda
财经资讯数据_灵砚智能1 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年4月22日
大数据·人工智能·python·信息可视化·自然语言处理