使用XML文件管理測(cè)試數(shù)據(jù)
1.jpeg.png (131.04 KB, 下載次數(shù): 307)
下載附件
2020-5-12 10:33 上傳
使用Excel文件保存測(cè)試用例和測(cè)試結(jié)果:
2.png (47.98 KB, 下載次數(shù): 287)
下載附件
2020-5-12 10:33 上傳
Python腳本遍歷全部測(cè)試用例: # -*- coding=utf-8 -*-
import os
import sys
import requests
from xml.dom.minidom import parse
from openpyxl.reader.excel import load_workbook
# import xml.dom.minidom
# Window下必須加載Commons庫(kù)的目錄 否則在Dos窗口中執(zhí)行時(shí)會(huì)找不到庫(kù)文件
parent_dir = os.path.split(os.getcwd())[0]
comm_dir = parent_dir+str("\BaseFunction")
sys.path.append(comm_dir)
AUTO_RESULT = u"E:\PyScripts\MdServices\TestCases\WebServiceCases.xlsx"
# 自動(dòng)化測(cè)試用例Excel文件
case_file = open(u'E:\PyScripts\MdServices\TestCases\casedata.xml', 'rb')
# 加載XML文件
tree = parse(file=case_file)
collection = tree.documentElement
# 讀取所有接口方法信息
operation_list = collection.getElementsByTagName('Operation')
# 讀取保存測(cè)試結(jié)果的文件
book = load_workbook(AUTO_RESULT)
# 獲取Excel文件中的表單sheet
sheet_names = book.get_sheet_names()
# 指定工作表單--根據(jù)sheet名查找
working_sheet = book.get_sheet_by_name(sheet_names[0])
# Excel中測(cè)試用例開始的索引
start_index = 2
# 循環(huán)每個(gè)webservice方法
for operation in operation_list:
# 定義operation的UR地址
uri = operation.getAttribute('url')
# 讀取接口中方法的調(diào)用方式get post head等等
action_type = operation.getAttribute('action')
# 接口方法名
function_name = operation.getAttribute('name')
# 讀取所有測(cè)試用例
case_list = operation.getElementsByTagName("case")
# 循環(huán)執(zhí)行所有測(cè)試用例
for case in case_list:
# 得到測(cè)試用例中所有參數(shù)的名稱和值
parameter_list = case.getElementsByTagName("Parameter")
length = len(parameter_list)-1
json_data = {}
for index in (0, length):
# 得到參數(shù)名稱
name = parameter_list[index].getAttribute('name')
# 得到參數(shù)值
value = parameter_list[index].getAttribute('value')
# 將每個(gè)參數(shù)和值存為字典形式 即JSON格式
json_data[name] = value
# 接口調(diào)用方式為post
if action_type == 'post':
# 接口中方法的調(diào)用方式為post
response = requests.post(uri, data=json_data)
# 測(cè)試用例編號(hào)
case_id_locator = 'A'+str(start_index)
# 保存case id
working_sheet.cell(case_id_locator).value = start_index
# 保存Excel文件的更改信息
book.save(AUTO_RESULT)
# service接口 url地址
service_url_locator = 'B'+str(start_index)
# 保存case id
working_sheet.cell(service_url_locator).value = uri
# 保存Excel文件的更改信息
book.save(AUTO_RESULT)
# 接口方法名稱
function_name_locator = 'C'+str(start_index)
# 保存case id
working_sheet.cell(function_name_locator).value = function_name
# 保存Excel文件的更改信息
book.save(AUTO_RESULT)
# 實(shí)際返回結(jié)果
actual_result_locator = 'G'+str(start_index)
# 保存case id
working_sheet.cell(actual_result_locator).value = response.content
# 保存Excel文件的更改信息
book.save(AUTO_RESULT)
# 預(yù)期返回結(jié)果
expect_result_locator = 'F'+str(start_index)
expected_content = working_sheet.cell(expect_result_locator).value
# run result測(cè)試結(jié)果
run_result_locator = 'H'+str(start_index)
# 對(duì)比預(yù)期結(jié)果和實(shí)際結(jié)果
if expected_content == actual_result_locator:
working_sheet.cell(run_result_locator).value = 'Pass'
else:
working_sheet.cell(run_result_locator).value = 'Fail'
# 保存Excel文件的更改信息
book.save(AUTO_RESULT)
# 接口調(diào)用方式為get
elif action_type == 'get':
response = requests.get(uri, data=json_data)
print 'hello get webservice'
# Excel中循環(huán)
start_index += 1
# print 'start index', start_index
|