框架采用robot和python实现,因为robot中的复杂逻辑实现起来比较繁琐,故选择用python实现,然后以外部库的形式导入robot中使用。测试用例数据保存在excel中。
使用过robot的人了解,robot中测试维度划分为测试套件(Test Suite)和测试用例(Test Case),一个Suite为一组Case的集合,每个Case对应为我们手工执行测试时的Case。
假设测试一个路径为/areaplug/showCityName的http接口,常规方法是在robot中新建一个showCityName的Suite,其下包含测试该http接口的用例集,如下图所示:
showCityName Test Suite
倘若测试该接口有20个异常用例,则建立20条相应的test case。但是,对于测试http接口来讲,以上用例无非就是请求参数和响应不一样而已,发送请求的逻辑是一模一样的。所以,这20条test case其实用一条test case就能实现了,在这1条case中分别遍历读取20个异常用例的测试数据执行测试就ok了。所以最后构造的suite和case如下:
接口case
图中,batch_Request为测试套件,其下的每个robot的test case对应一个http接口测试场景,比如测试路径为/areaplug/showCityName的http接口,该接口的所有正向和异常用例均在test_showCityName中实现,在test_showCityName中读取测试数据文件,获取该接口的测试用例数目,遍历每一条测试用例数据,调用http_Request下的sendHttpRequest发送http请求。其实,这里的test_showCityName就相当于test suite了,而遍历测试数据文件中的每一行测试数据去调用sendHttpRequest时,就相当于生成了一条test case,这样就可以将一个接口的所有测试用例用robot的一条test case实现(实质是robot的一条test case相当于一个test suite,在这个robot的test case中动态生成n条test case)。整个流程如下图所示:
框架流程图
搭建
测试数据
测试数据保存在excel中,每一个sheet页对应一个测试场景,即一个http接口。该sheet也保存有测试该接口的所有测试用例数据以及接口路径和请求方法,如下图所示(这里仅仅是一个demo,实际回归测试时,会有大量的用例和数据):
测试数据
测试框架
整个工程目录如下:
E:\LLF_58TESTSUITES\JZ_WEBINTERGRATION\ROBOT_CODE
│ execPybot.bat
│
├─pycode
│ │ Common_Excel.py
│ │ Common_Excel.pyc
│ │ Common_Exec.py
│ │ Common_Exec.pyc
│ │ testHTTP.py
│ │ __init__.py
│ │
│ ├─.idea
│ │ │ misc.xml
│ │ │ modules.xml
│ │ │ pycode.iml
│ │ │ workspace.xml
│ │ │
│ │ └─inspectionProfiles
│ └─__pycache__
│ Common_Excel.cpython-36.pyc
│ Common_Exec.cpython-36.pyc
│ __init__.cpython-36.pyc
│
├─report
│ │ log.html
│ │ output.xml
│ │ report.html
│ │
│ └─TestCaseReport
│ ├─result_calendar
│ │ log_20180130195712.html
│ │ output_20180130195712.xml
│ │ report_20180130195712.html
│ │
│ ├─result_getScheduleFlags
│ │ log_20180130195710.html
│ │ output_20180130195710.xml
│ │ report_20180130195710.html
│ │
│ └─result_showCityName
│ log_20180130195707.html
│ output_20180130195707.xml
│ report_20180130195707.html
│
├─rfcode
│ │ batch_Request.txt
│ │ http_Request.txt
│ │ __init__.robot
│ │
│ ├─关键字
│ │ 关键字index.txt
│ │ 自定义关键字.txt
│ │
│ └─配置信息
│ config.txt
│ configIndex.txt
│ RequestHeaders.txt
│
└─testData
testData.xlsx
工程有4部分构成:
- pycode
由于robot中复杂逻辑的实现比较繁琐,所以将一些复杂逻辑直接用python代码实现,然后以外部库的形式导入robot中调用。共有2个文件:
Common_Excel.py
主要负责对测试数据excel文件的读取操作。
# coding: utf-8
import xlrd
def getTestData(testDataFile, testScene, host, caseNo):
'''
从excel中获取测试数据
:param testDataFile: 测试数据文件
:param testScene: 测试场景
:param host: 服务器主机
:param caseNo: 用例No
:param method: 请求方法
:return: url,用例No,用例名称,请求参数,预期返回码,预期响应内容
'''
caseNo = int(caseNo)
data = xlrd.open_workbook(testDataFile)
table = data.sheet_by_name(testScene)
cols = table.ncols
resource_path = table.cell(0, 1).value # 文件路径
url = "http://" + host + resource_path # 访问的url
method = table.cell(1, 1).value # 请求方法
dict_params = {}
for i in range(cols):
dict_params[table.cell(2, i).value] = table.cell(caseNo+2, i).value
caseNo = dict_params.pop("caseNo")
caseName = dict_params.pop("caseName")
expectCode = dict_params.pop("expect_code")
expectCotent = dict_params.pop("expect_content")
testName = "TestCase" + caseNo + "_" + caseName
return method, url, caseNo, testName, dict_params, expectCode, expectCotent
def getTestCaseNum(testDataFile, testScene):
'''
获取testScene测试场景中的测试用例数
:param testDataFile: 测试数据文件
:param testScene: 测试场景
:return: 测试用例数
'''
data = xlrd.open_workbook(testDataFile)
table = data.sheet_by_name(testScene)
rows = table.nrows
return rows-3
def getTestHttpMethod(testDataFile, testScene):
'''
获取testScene测试场景的请求方法
:param testDataFile: 测试数据文件
:param testScene: 测试场景
:return: 请求方法
'''
data = xlrd.open_workbook(testDataFile)
table = data.sheet_by_name(testScene)
method = table.cell(1, 1).value # 请求方法
return method
Common_Exec.py
主要负责根据测试数据批量构造pybot命令来调用robot执行测试。
# coding: utf-8
import requests
import os
import time
def batch_Call(robot_testSuite, robot_testCase, testScene, caseNum, testCaseReportPath, execTime):
'''
批量执行testScene测试场景下的用例
:param robot_testSuite: robot testSuite路径
:param robot_testCase: robot testCase路径
:param testScene: 测试场景
:param caseNum: 用例数
:param testCaseReportPath: 业务用例测试报告路径
:param execTime: 执行时间
:return:
'''
try:
for caseNo in range(caseNum):
testCase = ""
caseNo = caseNo + 1
testName = "testcase" + "_" + str(caseNo)
output_dir = "-d " + testCaseReportPath + "/result_{0}".format(testScene) # 输出目录
output_xml = "-o output_{0}_{1}.xml".format(testName, execTime)
output_log = "-l log_{0}_{1}.html".format(testName, execTime)
output_report = "-r report_{0}_{1}.html".format(testName, execTime)
variable = "-v caseNo:" + str(caseNo) + " -v testScene:" + testScene
testCase = "--test " + robot_testCase
pybot_cmd = "pybot " + output_dir + " " + output_xml + " " + output_log + " " + output_report + " " + variable + " " + " " + testCase + " " + robot_testSuite
os.system(pybot_cmd) # 执行pybot命令
return "done"
except Exception as e:
return "Error: " + str(e)
def send_HttpRequest(url, data=None, headers=None, method=None):
'''
发送http请求
:param url: 请求的url
:param data: 请求数据
:param headers: 请求头
:param method: 请求方法
:return: 响应码,响应内容
'''
if method == "get":
response = requests.get(url, data, headers=headers)
if method == "post":
response = requests.post(url, data, headers=headers)
code = str(response.status_code)
content = response.content.decode("utf-8") # 转码
return code, content
def cleanLogs(testScene, testCaseReportPath):
'''
删除硬盘中合并前的测试报告
:param testScene: 测试场景
:param testCaseReportPath: 业务用例测试报告路径
:return:
'''
testCaseReportPath = testCaseReportPath + "/result_{0}".format(testScene)
report_files = testCaseReportPath + "/report_testcase*"
xml_files = testCaseReportPath + "/output_testcase*"
log_files = testCaseReportPath + "/log_testcase*"
cmd = "del " + report_files + " " + xml_files + " " + log_files # windows
cmd = cmd.replace("/", "\\")
print(cmd)
os.system(cmd)
def getCurtime():
'''
获取当前时间
:return: 当前时间
'''
return time.strftime("%Y%m%d%H%M%S", time.localtime(time.time()))
def mergeReport(testScene, testCaseReportPath, execTime):
'''
# 合并报告
:param testScene: 测试场景
:param testCaseReportPath: 业务用例测试报告路径
:param execTime: 执行时间
:return:
'''
try:
output_dir = "-d " + testCaseReportPath + "/result_{0}".format(testScene) # 输出目录
output_xml = "-o output_{0}.xml".format(execTime)
output_log = "-l log_{0}.html".format(execTime)
output_report = "-r report_{0}.html".format(execTime)
# 被合并的报告
merge_report = testCaseReportPath + "/result_{0}".format(testScene) + "/output_testcase_*.xml"
name = "--name " + testScene
rebot_cmd = r"rebot " + output_dir + " " + output_xml + " " + output_log + " " + output_report + " " + name + " " + merge_report
os.system(rebot_cmd) # 执行rebot命令
return "done"
except Exception as e:
return "Error: " + str(e)
- report
该目录用于存放测试报告。其中report目录下的robot测试报告为测试Suite的测试报告,而TestCaseReport下会根据不同的测试场景生成对应该场景名称的测试报告文件夹,其下会包含该测试场景下所有用例的合并报告(即excel中的每一条case会生成一个报告,最后会将这些cases的报告合并为一个报告,作为该测试场景即该http接口的测试报告)。 - rfcode
该目录下为robot的代码。
batch_Request.txt
batch_Request下包含要测试的各http接口对应的测试场景(即robot的测试用例)。在各测试场景中会设置${testScene}变量,通过该变量去excel文件中对应的sheet页获取相应的测试数据。
*** Settings ***
Library ../pycode/Common_Exec.py
Resource 关键字/关键字index.txt
Resource 配置信息/configIndex.txt
Library ../pycode/Common_Excel.py
*** Test Cases ***
test_showCityName
[Documentation] /areaplug/showCityName
# 测试场景
${testScene} Set Variable showCityName