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文件方法无需导入
相关推荐
k layc3 分钟前
【论文解读】《Training Large Language Models to Reason in a Continuous Latent Space》
人工智能·python·机器学习·语言模型·自然语言处理·大模型推理
阿正的梦工坊18 分钟前
Sliding Window Attention(滑动窗口注意力)解析: Pytorch实现并结合全局注意力(Global Attention )
人工智能·pytorch·python
喜-喜41 分钟前
Python pip 缓存清理:全面方法与操作指南
python·缓存·pip
rgb2gray42 分钟前
GeoHD - 一种用于智慧城市热点探测的Python工具箱
人工智能·python·智慧城市
MZWeiei1 小时前
Matplotlib,Streamlit,Django大致介绍
python·django·matplotlib
游客5202 小时前
自动化办公|xlwings生成图表
python·自动化
ylfhpy2 小时前
Python常见面试题的详解16
开发语言·python·面试
蹦蹦跳跳真可爱5892 小时前
Python----PyQt开发(PyQt高级:手搓一个音乐播放器)
python·pyqt
高力士等十万人2 小时前
OpenCV对比度增强
人工智能·python·opencv
宝哥的菜鸟之路2 小时前
Python 数据分析概述 ①
开发语言·python·数据分析