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文件方法无需导入
相关推荐
Csvn几秒前
🐍 Day 6: Python 异常处理 — 防御式编程的核心
后端·python
朝发如雪2 分钟前
快速掌握Linux(2)(时间相关函数)(文件IO)
linux·python·算法
苏子寒8 分钟前
Nano-VLLM全代码解析笔记(2)-block_manager
人工智能·笔记·python·机器学习·nlp·vllm
小猴子爱上树16 分钟前
跨境电商图片翻译工具,跨马批量处理视频字幕与抠图
python·音视频
smj2302_7968265217 分钟前
解决leetcode第4033题有效K个不同元素子数组I
数据结构·python·算法·leetcode
Patrick在香港18 分钟前
Python asyncio vs 多线程 vs 多进程——同一份 HKOpenDataClient 实测 5/30/100 endpoint,谁是真的银弹
开发语言·python
Metaphor69221 分钟前
使用 Python 设置 Excel 文件属性
开发语言·python·excel
IT毕设实战小研37 分钟前
基于大数据的桂林热门旅游数据分析与可视化
大数据·python·机器学习·数据分析·django·课程设计·旅游
SamChan9041 分钟前
用PostgreSQL+pgvector构建PDF翻译记忆库:向量相似度检索+增量更新实战
数据库·python·ai·postgresql·pdf·wpf
金融小师妹42 分钟前
多因子市场推演:油价、英伟达与杰克逊霍尔如何影响资产定价的AI事件预测框架
人工智能·python·深度学习