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文件方法无需导入
相关推荐
Alkaid20771 分钟前
我把文件和脚本放一起了,Python 就是看不见?新手必看的绝对路径 vs 相对路径终极避坑指南
python·ai编程
SamChan9022 分钟前
Python+OpenCV检测PDF翻译后排版偏移:像素级格式一致性验证
python·opencv·ai·pdf·wpf
会博通·代码搬运工23 分钟前
双层PDF技术实现与档案数字化系统的API集成实践
数据库·人工智能·分布式·python·计算机视觉·api集成
健康平安的活着25 分钟前
java中批量查询+stream返回map提高查询效率
java·windows·python
无线通信科研笔记27 分钟前
IEEE TWC 2026 论文精读与完整复现|H-PASS:机械—电子双重可重构波束成形
论文阅读·人工智能·笔记·python·论文笔记
青 春 记 忆35 分钟前
零基础入门python35:Django用户登录与权限边界
python·django·后端开发
淼澄研学40 分钟前
LangChain构建Prompt模板解决Python长尾报错实操
python·langchain·prompt
枫叶v.43 分钟前
DeepSeek Harness(DSH)插件开发保姆级教程:从 0 到 1 快速写出第一个插件
python
Htr_1 小时前
Python面向对象编程:类与对象基础
开发语言·python
muddjsv1 小时前
Python 神经网络入门:用 scikit-learn 完成 MLP 分类与回归
python·神经网络·scikit-learn