【Robotframework+python】实现http接口自动化测试,熬夜肝完这份Framework笔记

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新软件测试全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注软件测试)
img

正文

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

请求方法

${method} getTestHttpMethod ${testDataFile} ${testScene}
执行测试 ${testScene} ${method}

test_getScheduleFlags
[Documentation] /ManageSchedule/getScheduleFlags

测试场景

${testScene} Set Variable getScheduleFlags

请求方法

${method} getTestHttpMethod ${testDataFile} ${testScene}
执行测试 ${testScene} ${method}

test_calendar

测试场景

${testScene} Set Variable calendar

请求方法

${method} getTestHttpMethod ${testDataFile} ${testScene}
执行测试 ${testScene} ${method}

http_Request.txt
在各测试场景中会根据excel中的测试用例记录数目去批量调用http_Request下的sendHttpRequest执行http接口测试。在sendHttpRequest中会根据caseNo去excel中查询相应测试数据,并发送对应的http请求到相应http接口中。收到响应后,与excel中的预期响应码和响应内容做比对。

*** Settings ***
Library …/pycode/Common_Exec.py
Library …/pycode/Common_Excel.py
Resource 关键字/关键字index.txt

*** Test Cases ***
sendHttpRequest

获取测试用例数据

${method} ${url} ${caseNo} ${testName} ${dict_params} ${expectCode} ${expectCotent}
… getTestData ${testDataFile} ${testScene} ${Host} ${caseNo}

设置用例说明

Set Test Documentation ${testName}

请求头

${headers} 获取请求头
#根据method发送对应的http请求
${actualCode} ${actualContent} send_HttpRequest ${url} ${dict_params} ${headers} ${method}

响应码比对

Should Be Equal ${actualCode} ${expectCode}

响应内容比对

Should Be Equal ${actualContent} ${expectCotent}

关键字
关键字模块主要是对一些复用逻辑的封装。

*** Settings ***
Resource …/配置信息/configIndex.txt
Library …/…/pycode/Common_Excel.py
Library …/…/pycode/Common_Exec.py

*** Keywords ***
获取请求头
d i c t h e a d e r s C r e a t e D i c t i o n a r y H o s t = {dict_headers} Create Dictionary Host= dictheadersCreateDictionaryHost={Host} User-Agent= U s e r − A g e n t A c c e p t = {User-Agent} Accept= UserAgentAccept={Accept} Accept-Language= A c c e p t − L a n g u a g e A c c e p t − E n c o d i n g = {Accept-Language} Accept-Encoding= AcceptLanguageAcceptEncoding={Accept-Encoding}
… Cookie= C o o k i e C o n n e c t i o n = {Cookie} Connection= CookieConnection={Connection} Cache-Control=${Cache-Control}
Return From Keyword ${dict_headers}

执行测试
[Arguments] ${testScene} ${method} # 测试场景|请求方法

获取用例数目

${case_num} getTestCaseNum ${testDataFile} ${testScene}

获取当前时间

${execTime} getCurtime
#批量执行testScene测试场景下的用例
${status} batch_Call ${httpTestSuite} ${httpRequestTestCase} ${testScene} ${case_num} ${testCaseReportPath}
… ${execTime}
log ${status}

合并报告

${status} mergeReport ${testScene} ${testCaseReportPath} ${execTime}
log ${status}

清理合并前的报告

cleanLogs ${testScene} ${testCaseReportPath}

配置信息
配置信息中存储配置信息以及通讯头的信息。通讯头中有cookie,保存有登录信息,通讯头的部分涉及隐私,故这部分数据不放出来了。
config.txt

*** Settings ***

*** Variables ***
${testDataFile} E:/llf_58TestSuites/jz_webIntergration/robot_code/testData/testData.xlsx # 测试数据
${httpRequestTestCase} sendHttpRequest # http请求用例模板
${httpTestSuite} E:/llf_58TestSuites/jz_webIntergration/robot_code/rfcode/http_Request.txt # http请求测试套件
${testCaseReportPath} E:/llf_58TestSuites/jz_webIntergration/robot_code/report/TestCaseReport # 业务用例测试报告路径

RequestHeaders.txt

*** Settings ***
Documentation 请求头信息

*** Variables ***
${Host} ******* # 服务器主机
${User-Agent} Mozilla/5.0 (Windows NT 6.1; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0 # 浏览器代理
${Accept} text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
${Accept-Language} en-US,en;q=0.5
${Accept-Encoding} gzip, deflate
${Cookie} ************
${Connection} keep-alive
${Cache-Control} max-age=0
${Upgrade-Insecure-Requests} ***

  • testData
    该目录下存放测试数据excel文件。
执行测试

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注软件测试)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
rade-Insecure-Requests} ***

  • testData
    该目录下存放测试数据excel文件。
执行测试

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注软件测试)
[外链图片转存中…(img-OPk0lpx0-1713177229401)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 11
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值