当前位置: 首页 > news >正文

Pytest项目_day18(读取ini文件)

读取ini文件

  • 安装configparser

ini的格式如下

  • []内为key值
  • 另起一行为value值,可以用字典嵌套
  • 配置文件不需要加引号
[host]
api_sit_url = http://sellshop.5istudy.online/sell/shouji/query

read_ini()方法

  • 主要作用是获取ini文件
  • 写法比较固定,如下:
import configparser
import ospath = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))),"config", "settings.ini")def read_ini():config = configparser.ConfigParser()config.read(path, encoding="utf8")return config
  • 可以在测试用例中直接使用
    在这里插入图片描述

合并 read_ini() 和 read_data() 方法

  • 这两个方法都是读取操作,可以合并到一个文件中
  • 如果一个文件中有两个方法都返回数据,那么可以将它们放到一个类中,代码如下:
import configparser
import os
import yamlini_path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "config", "settings.ini")
data_path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "data", "data.yaml")class FileRead:def __init__(self):self.ini_path = ini_pathself.data_path = data_pathdef read_ini(self):config = configparser.ConfigParser()config.read(self.ini_path, encoding="utf8")return configdef read_data(self):f = open(self.data_path, encoding="utf8")data = yaml.safe_load(f)return data# 此处直接实例化,方便调用
get_data = FileRead()
  • 测试用例中可以做相应修改
    在这里插入图片描述
http://www.dtcms.com/a/337726.html

相关文章:

  • Spring Boot 实用小技巧:多级缓存(Caffeine + Redis)- 第545篇
  • 如何解决机器翻译的“幻觉“问题(Hallucination)?
  • 当AI学会“思考”:大语言模型背后的智能本质与伦理边界
  • 【提示词技巧】通用提示词原则介绍
  • Linux学习-软件编程(进程间通信1)
  • ROS 2 中用于建图的一些 topic
  • PyTorch神经网络工具箱(优化器)
  • buuctf:护网杯_2018_gettingstart、oneshot_tjctf_2016
  • llamafactory使用qlora训练
  • VectorDB+FastGPT一站式构建:智能知识库与企业级对话系统实战
  • 使用LLaMA-Factory对大模型进行微调-详解
  • OSG+Qt —— 笔记2- Qt窗口绘制棋盘及模型周期运动(附源码)
  • linux:告别SSH断线烦恼,Screen命令核心使用指南
  • 第四章:大模型(LLM)】07.Prompt工程-(1)Prompt 原理与基本结构
  • 大数据分析-读取文本文件内容进行词云图展示
  • Zephyr 中的 bt_le_per_adv_set_data 函数的介绍和应用方法
  • [机器学习]09-基于四种近邻算法的鸢尾花数据集分类
  • 具身智能赋能轮椅机器人的认知革命与人机共生新范式
  • 【软考架构】第4章 信息安全的抗攻击技术
  • 从「行走」到「思考」:机器人进化之路与感知—决策链路的工程化实践
  • 微电网管控系统中python多线程缓存与SQLite多数据库文件连接池实践总结(含源码)
  • 安川YASKAWA焊接机器人保护气智能节气阀
  • 蓝牙 GFSK RX Core 架构解析
  • Linux下的软件编程——IPC机制
  • 重复(Repeat)和迭代(Iteration)区别、递归(Recursion)
  • 超级云平台:重构数字生态的“超级连接器“
  • 想找出版社出书?这样选就对了!
  • 哈工深无人机目标导航新基准!UAV-ON:开放世界空中智能体目标导向导航基准测试
  • 【论文阅读】-《GeoDA: a geometric framework for black-box adversarial attacks》
  • 基于Flink CDC实现联系人与标签数据实时同步至ES的实践