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文件方法无需导入
相关推荐
蔚说4 分钟前
is 与 == 的区别 python
python
cnxy1888 分钟前
围棋对弈Python程序开发完整指南:步骤3 - 气(Liberties)的计算算法设计
python·算法·深度优先
叶子20242225 分钟前
骨架点排序计算
python
AC赳赳老秦39 分钟前
行业数据 benchmark 对比:DeepSeek上传数据生成竞品差距分析报告
开发语言·网络·人工智能·python·matplotlib·涛思数据·deepseek
小鸡吃米…41 分钟前
带Python的人工智能——深度学习
人工智能·python·深度学习
胡伯来了1 小时前
07 - 数据收集 - 网页采集工具Scrapy
python·scrapy·数据采集
御水流红叶1 小时前
第七届金盾杯(第一次比赛)wp
开发语言·python
小徐Chao努力1 小时前
【Langchain4j-Java AI开发】04-AI 服务核心模式
java·人工智能·python
白日做梦Q1 小时前
预训练模型微调(Finetune)实战:策略、技巧及常见误区规避
人工智能·python·神经网络·机器学习·计算机视觉
历程里程碑1 小时前
双指针巧解LeetCode接雨水难题
java·开发语言·数据结构·c++·python·flask·排序算法