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文件方法无需导入
相关推荐
2401_8949155337 分钟前
GEO 搜索优化完整源码从零部署:环境配置、集群搭建全流程
开发语言·python·tcp/ip·算法·unity
zhiSiBuYu05172 小时前
Python3 模块开发与应用实战指南
python
databook3 小时前
用方差阈值过滤掉“惰性特征”
python·机器学习·scikit-learn
Freak嵌入式3 小时前
MCU 低功耗模式解析:时钟门控、电源门控、深度休眠
人工智能·python·单片机·嵌入式硬件·开源·依赖倒置原则·micropython
weixin_BYSJ19874 小时前
springboot校园自习室管理小程序---附源码32142
java·javascript·spring boot·python·django·flask·php
用户8356290780515 小时前
使用 Python 在 PDF 中绘制线条、矩形和自定义图形
后端·python
wang_yb5 小时前
用方差阈值过滤掉“惰性特征”
python·ai·databook
威联通网络存储5 小时前
TS-h2490FU在钢铁热轧/冷轧板材表面缺陷在线视觉检测中的部署
人工智能·python·计算机视觉·视觉检测
RFID固定资产管理系统6 小时前
公司RFID管理系统揭秘
大数据·python
IVEN_6 小时前
Python官方包、Conda、uv,应该怎么选
python