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文件方法无需导入
相关推荐
神仙别闹1 分钟前
基于Python(Django)+MySQL 实现(Web)SQL智能检测系统的设计与实现
python·mysql·django
甄心爱学习1 分钟前
【项目实训】法律文书智能摘要系统4
python·github·个人开发
huzhongqiang26 分钟前
Playwright理解与封装
python
zhangchaoxies29 分钟前
MySQL触发器能否监控特定用户操作_结合审计功能实现分析
jvm·数据库·python
qq_4135020243 分钟前
如何解决ORA-12518监听程序无法分配进程_内存耗尽与PGA溢出
jvm·数据库·python
zhangrelay1 小时前
三分钟云课实践速通--大学物理--python 版
linux·开发语言·python·学习·ubuntu·lubuntu
djjdjdjdjjdj1 小时前
如何用参数解构在函数入口处直接提取对象属性
jvm·数据库·python
forEverPlume2 小时前
mysql如何批量增加表的字段_脚本化DDL操作实践
jvm·数据库·python
asdzx672 小时前
使用 Python 读取 PDF: 提取文本和图片
开发语言·python·pdf
m0_596406372 小时前
CSS如何高效引入样式表_对比link标签与import指令的性能差异
jvm·数据库·python