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文件方法无需导入
相关推荐
free-elcmacom8 小时前
深度学习<4>高效模型架构与优化器的“效率革命”
人工智能·python·深度学习·机器学习·架构
liliangcsdn8 小时前
python模拟beam search优化LLM输出过程
人工智能·python
王琦03189 小时前
Python 函数详解
开发语言·python
胡伯来了9 小时前
13. Python打包工具- setuptools
开发语言·python
小鸡吃米…9 小时前
Python 中的多层继承
开发语言·python
中國移动丶移不动10 小时前
Python MySQL 数据库操作完整示例
数据库·python·mysql
落叶,听雪10 小时前
AI建站推荐
大数据·人工智能·python
ZAz_10 小时前
DAY 45 预训练模型
python
呆萌很10 小时前
python 项目迁移
python
清水白石00810 小时前
《requests vs httpx:Python 网络请求库的全面对比与实战指南》
网络·python·httpx