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文件方法无需导入
相关推荐
逄逄不是胖胖38 分钟前
《动手学深度学习》-60translate实现
人工智能·python·深度学习
橘颂TA38 分钟前
【测试】自动化测试函数介绍——web 测试
python·功能测试·selenium·测试工具·dubbo
爱学习的阿磊42 分钟前
Python上下文管理器(with语句)的原理与实践
jvm·数据库·python
m0_736919101 小时前
Python面向对象编程(OOP)终极指南
jvm·数据库·python
one____dream1 小时前
【网安】Reverse-非常规题目
linux·python·安全·网络安全·ctf
冷雨夜中漫步1 小时前
python反转列表reverse()和[::-1]哪个效率更高
开发语言·python
rainbow68891 小时前
Python面向对象编程与异常处理实战
开发语言·python
weixin199701080161 小时前
锦程物流item_get - 获取详情接口对接全攻略:从入门到精通
数据库·python
2501_907136821 小时前
基于Python+QT6的移动硬盘弹出工具
python·软件需求
2501_907136822 小时前
python 界面元素控件库工具,可以看到python的可视控件和使用方法
python·软件需求