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文件方法无需导入
相关推荐
我的xiaodoujiao19 小时前
快速学习Python基础知识详细图文教程18--多线程
开发语言·python·学习·测试工具
❀搜不到20 小时前
isat-sam分割导出掩码图
开发语言·python
Zane199420 小时前
ClassName() 只是一步?拆开看 __new__ 和 __init__ 各自在干什么
后端·python
曾阿伦21 小时前
Trae CN Python环境调试debug指南
开发语言·python
渣男教父21 小时前
Python游戏自动化实战案例
python·编程语言
天天爱吃肉821821 小时前
【工程师笔记|新能源整车电控一次过CISPR25/BCI,汽车EMC/EMI落地十大核心设计技巧】
大数据·人工智能·笔记·python·汽车
m0_6174939421 小时前
Python 使用 cryptography 实现数据加密与解密
开发语言·python
万山寒1 天前
python依赖包导出离线安装到没有外网的服务器
服务器·开发语言·python
程序员雷欧1 天前
LongAdder
开发语言·python
月光船幽幽1 天前
分层阈值规避归藏协议过度重置
人工智能·python