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文件方法无需导入
相关推荐
❁满城风絮*10 小时前
Claude Code 学习总结
python·agent·claude
维克兜率天12 小时前
【维克】复合型CTA:趋势+套利的双轮驱动
python·学习方法
伞伞悦读13 小时前
【第76期】两张3行表左连接变5行:主键、validate与未匹配排查完整教程
人工智能·python
用户83562907805113 小时前
使用 Python 在 Excel 中添加和自定义迷你图
后端·python
用户83562907805114 小时前
使用 Python 在 Excel 图表中添加趋势线与回归方程
后端·python
特立独行的猫A14 小时前
一份 Markdown 发十个平台:cspub 一稿多投
java·后端·python
深蓝AI14 小时前
HTTPX 超时 1 秒,接口为何还能跑 2.4 秒?
python
CyrusYiVPP14 小时前
哪个用户会再来买?RFM模型+plotly画出用户分层,数据分析实战第2期
人工智能·python
李帅51516 小时前
用 CPU 做发票识别:从 OCR 文本行到结构化明细表的 4 个硬骨头
python
happylifetree16 小时前
Python79:核心语法-面向对象基础-实例方法
python