pytest读取.ini文件
# config/setting.ini
[host]
api_sit_url = https://api.binstd.com
# utils/read_ini.py
import configparser
import os
current_path = os.path.realpath(__file__)
parent_path = os.path.dirname(current_path)
path = os.path.join(os.path.dirname(parent_path), "config", "setting.ini")
def read_ini():
config = configparser.ConfigParser()
config.read(path, encoding='utf8')
return config
get_ini = read_ini()
# print(read_ini()['host']['api_sit_url'])
## https://api.binstd.com
将read_ini 与 read_data整合,类方法实现
project > config > setting.ini
[host]
api_sit_url = https://api.binstd.com
# project > data > data.yaml
mobile: {shouji: 13456759012,appkey: 0c818521d38759e1}
# project > utils > read_test.py
import yaml
import configparser
import os
current_path = os.path.realpath(__file__)
parent_path = os.path.dirname(current_path)
data_path = os.path.join(os.path.dirname(parent_path), "data", "data.yaml")
ini_path = os.path.join(os.path.dirname(parent_path), "config", "setting.ini")
class FileRead:
def __init__(self):
self.data_path = data_path
self.ini_path = ini_path
def read_data(self):
f = open(self.data_path, encoding="utf8")
data = yaml.safe_load(f)
return data
def read_ini(self):
config = configparser.ConfigParser()
config.read(self.ini_path, encoding='utf8')
return config
base_data = FileRead()
# project > test_read > test_read.py
import requests
import pytest
from utils.read_test import base_data
url = base_data.read_ini()['host']['api_sit_url']
def test_mobile():
param = base_data.read_data()['mobile']
r = requests.get(url + "/shouji/query",
params={"shouji": param['shouji'], "appkey": param['appkey']})
print(r.status_code)
print(r.json())
if __name__ == '__main__':
pytest.main()
# Result
test_read.py 200
{'status': 0, 'msg': 'ok', 'result': {'shouji': '13456759012', 'province': '浙江', 'city': '杭州', 'company': '中国移动', 'cardtype': None, 'areacode': '0571'}}
.
Notepad++ download | SourceForge.net
网友评论