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文件方法无需导入
相关推荐
科研鬼才(bushi2 小时前
项目文件夹规范
python
程序员:钧念2 小时前
深度学习与大语言模型LLM的区别
人工智能·python·深度学习·语言模型·自然语言处理·transformer·agent
深蓝电商API2 小时前
Scrapy Feed Exports 进阶:多种格式导出配置
爬虫·python·scrapy
JAVA+C语言2 小时前
如何在Java中实现线程间的通信?
java·大数据·python
移远通信2 小时前
短信的应用
java·git·python
a努力。2 小时前
阿里Java面试被问:WebSocket的心跳检测和自动重连实现
java·开发语言·python·websocket·面试·职场和发展·哈希算法
冷雨夜中漫步2 小时前
Python入门——__init__.py文件作用
android·java·python
Volunteer Technology2 小时前
Centos7安装python和jupyter
linux·python·jupyter
@zulnger2 小时前
Django 模型
后端·python·django
nvd112 小时前
Pytest 异步数据库测试实战:基于 AsyncMock 的无副作用打桩方案
数据库·pytest