配置文件
配置文件
至少可以分出:接口服務器、數據庫服務器
配置文件存儲服務器的相關信息(主要包括IP地址、端口號、應用名、接口名、數據庫用戶名、數據庫密碼、數據庫端口號、數據庫名等)
一般一個配置文件只存儲一種服務器的相關信息,通常把多個接口服務器的信息存在同一個配置文件中,把多個數據庫服務器信息存儲在另一個配置文件中:
配置文件的寫法:
[自定義節點名1]
IP=172.166.100.65
port=80
[自定義節點名2]
IP=172.166.100.66
port=8080
講課中的案例:
exam項目:exam/login/ 、exam/signup/ 端口號80
發布會項目:sign/接口名,端口號是8080
配置文件的意義
測試過程中可能會配備多台測試服務器、多台數據庫服務器,可以把使用的服務器環境配置情況寫入配置文件
配置文件的擴展名
可以是.ini、.cfg、.conf、.config、.txt等
配置文件的格式
[節名1] -- 節名 節點名 建議書寫英文單次或拼音
鍵1=值1
鍵2=值2
....
[節名2] -- 一個節名 代表一個服務器
鍵3=值3 ;注釋 結尾加;書寫注釋
創建server.conf文件
用於記錄接口服務器的相關配置信息
[host-211] ;服務器1,自定義名稱,不重復
;接口地址格式:http://IP:端口號/path?參數
ip=172.166.0.211
port=80
[host-104] ;服務器2
ip=192.168.0.104
port=8080
配置project_h1目錄下server.conf文件
[exam] ;書寫內容公司會有相應文檔 exam項目:包括登錄和注冊接口
IP=192.168.175.128
port=80
[sign];發布會項目:添加發布會、查詢發布會
IP=192.168.175.128
port=8080
創建db.conf文件
用於記錄數據庫服務器的相關配置信息
[mysql-211] ;數據庫服務器1
host=172.166.0.211
port=3306
user=root
password=123456
db=exam
[mysql-104] ;數據庫服務器2
host=192.168.0.104
port=3306
user=root
password=123456
db=exam
配置project_h1目錄下db.conf文件
[exam] ;exam項目 上課的案例
host=192.168.175.128
port=3306
user=root
password=123456
db=exam
[sign] ;發布會項目 上課的練習
host=192.168.175.128
port=3306
user=root
password=123456
db=guest
創建entry.ini文件
被測接口和數據庫的入口地址
在每次執行測試之前,手動修改此文件
用於指定本次測試要使用的server.conf節名和db.conf節名
[entry]
which_server=host-211
which_db=mysql-211
配置project_h1目錄下entry.ini文件
[entry];入口:用於指定測試使用的服務器
which_server = exam
which_db = exam
讀配置文件的步驟
導包
import configparser
創建ConfigParser對象
confile=configparser.ConfigParser()
讀配置文件
confile.read('配置文件名')
文件中有中文字符時,需用encoding='utf-8'指定字符編碼
獲取節名和鍵值
var=confile.get('節名','鍵名')
返回類型為字符串
讀配置文件entry.ini,輸出配置信息
"""
讀配置文件entry.ini,輸出配置信息
步驟:
1.導入configparser
2.創建ConfigParser對象
3.讀文件
4.取數據
5.輸出/返回數據
"""
import configparser
def read_entry():# 定義函數define:讀入口函數
confile = configparser.ConfigParser()# 創建對象 這里是CinfigParser類
confile.read('entry.ini',encoding='utf-8')#左邊不要寫=,輸入數據存入confile對象中;只要有中文就要加字符編碼
which_server = confile.get('entry','which_server')#將本次測試的接口服務器名讀出
which_db = confile.get('entry','which_db')# 將本次測試的數據庫服務器名讀出
return which_server,which_db
print(read_entry())# 調用函數,函數中有return,則不能省略print()
讀配置文件server.conf
"""
讀配置文件server.conf,輸出配置信息
"""
import configparser # 配置轉換
def read_server(section,interface):# 讀取接口服務器信息,參數為節點名section,section為形式參數
confile = configparser.ConfigParser()
confile.read('server.conf',encoding='utf-8')
ip = confile.get(section,'IP')# 取節點、鍵
port = confile.get(section,'port')
host = 'http://'+ip+':'+port+'/'+interface
return host
# 調用函數
print(read_server('exam','exam/login.php'))# 看exam項目的接口地址
print(read_server('sign','sign/login_cathion/'))# 看發布會項目sign的接口地址
讀配置文件db.conf
"""
讀配置文件db.conf,輸出配置信息
1.導入configparser
2.創建對象
3.讀文件
4.根據節點名和鍵名讀數據
5.根據需要,組裝數據,返回結果
"""
import configparser
def read_db(which_db): # which_db代表哪一個數據庫服務器
confile = configparser.ConfigParser()
confile.read('db.conf', encoding='utf-8')
host = confile.get(which_db, 'host') # 取數據庫主機名
port = confile.get(which_db, 'port') # 端口號
user = confile.get(which_db, 'user') # 數據庫用戶名
password = confile.get(which_db, 'password') # 數據庫密碼
db = confile.get(which_db, 'db') # 數據庫名
# 考慮到連接數據庫時,語法是:connect(host=,user=)
# 用字典可以很容易的轉化為host=這種形式
dbinfo = {'host': host, 'port': int(port), 'user': user, 'password': password, 'db': db}
return dbinfo
print(read_db('exam')) # exam節點名稱
# print(read_db('sign'))