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文件方法无需导入
相关推荐
xinxingrs4 分钟前
贪心算法、动态规划以及相关应用(python)
笔记·python·学习·算法·贪心算法·动态规划
不知更鸟32 分钟前
无法联系上下文
python·django
U***e6341 分钟前
Python测试
开发语言·python
金色熊族1 小时前
win11安装matplotlib过程
python·matplotlib
databook2 小时前
让你的动画“活”过来:Manim 节奏控制指南 (Rate Functions)
后端·python·动效
大佬,救命!!!3 小时前
最新的python3.14版本下仿真环境配置深度学习机器学习相关
开发语言·人工智能·python·深度学习·机器学习·学习笔记·环境配置
2***57424 小时前
Java数据分析实战
java·python·数据分析
vvoennvv4 小时前
【Python TensorFlow】 CNN-GRU卷积神经网络-门控循环神经网络时序预测算法(附代码)
python·神经网络·机器学习·cnn·gru·tensorflow
程序员三藏4 小时前
软件测试之压力测试详解
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·压力测试