'''
脚本层的一些公共方法
整个执行过程只执行一次
'''
import pytest
from zonghe.caw import FileRead
from zonghe.caw.BaseRequests import BaseRequests
# 测试前置,session级别的,整个过程执行一次
# 读取url
@pytest.fixture(scope='session')
def url():
return FileRead.read_ini(r"dataenv/env.ini", "url")
# 读取db
@pytest.fixture(scope='session')
def db():
# 配置文件中读取出来是字符串,但是需要一个字典
# 字符串转字典 eval
return eval(FileRead.read_ini(r"dataenv/env.ini", "db"))
# 创建BaseRequests的实例
@pytest.fixture(scope='session')
def baserequests():
return BaseRequests()
'''
读文件相关的操作
'''
import configparser
import os
import yaml
def get_project_path():
'''
获取过程路径:
:return:
''
print(__file__)
file_path = os.path.realpath(__file__) # 当前文件路径
dir_path = os.path.dirname(file_path) # 获取文件所在目录的路径
dir_path = os.path.dirname(dir_path) # 获取上一级路径
return dir_path + "\\"
def read_ini(file_path, key):
'''
读配置文件,根据key取value
:param file_path:
:param key:
:return:
'''
# configparser是python中读取
config = configparser.ConfigParser()
config.read(get_project_path() + file_path)
value = config.get("env", key)
return value
def read_yaml(file_path):
'''
读取文件
:param file_path:
:return:
'''
file_path = get_project_path() + file_path
with open(file_path, "r", encoding='utf-8') as f:
content = f.read()
return yaml.load(content, Loader=yaml.FullLoader)
# 测试代码,
if __name__ == '__main__':
# 绝对路径使用时不方便,如果不在c 盘,或者不存在这个路径,脚本无法执行。
url = read_ini(r"dataenv\env.ini", "url")
print(url)
db = read_ini(r"dataenv\env.ini", "db")
print(db)
print(read_yaml(r"datacase\register_fail.yaml"))
|