如何用Python结合Excel实现测试数据的读取与接口自动化存储?

2026-05-26 20:381阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计647个文字,预计阅读时间需要3分钟。

如何用Python结合Excel实现测试数据的读取与接口自动化存储?

`http_request2.py` 是一个用于发送 HTTP 请求的 Python 脚本。以下是对该脚本开头内容的简化

如何用Python结合Excel实现测试数据的读取与接口自动化存储?

pythonhttp_request2.py - 发送 HTTP 请求的脚本import http.client

创建连接conn=http.client.HTTPConnection(example.com)

发送 GET 请求conn.request(GET, /)

获取响应response=conn.getresponse()

打印状态码和响应内容print(状态码:, response.status)print(响应内容:, response.read().decode())

关闭连接conn.close()

passport.cnblogs.com/user/signin') # dr.find_element_by_id('input1').clear() dr.find_element_by_id('input1').send_keys(user[0]) # dr.find_element_by_id('input2').clear() dr.find_element_by_id('input2').send_keys(user[1]) dr.find_element_by_id('signin').click() sleep(1) print('\n' + '测试项:' + user[2]) dr.get_screenshot_as_file(path + user[3] + ".jpg") try: assert dr.find_element_by_id(user[4]).text try: error_message = dr.find_element_by_id(user[4]).text self.assertEqual(error_message, user[5]) print('提示信息正确!预期值与实际值一致:') print('预期值:' + user[5]) print('实际值:' + error_message) except: print('提示信息错误!预期值与实际值不符:') print('预期值:' + user[5]) print('实际值:' + error_message) except: print('提示信息类型错误,请确认元素名称是否正确!') def tearDown(self): dr.refresh() # 关闭浏览器 dr.quit() if __name__ == "__main__": # 定义脚本标题,加u为了防止中文乱码 report_title = u'登陆模块测试报告' # 定义脚本内容,加u为了防止中文乱码 desc = u'登陆模块测试报告详情:' # 定义date为日期,time为时间 date = time.strftime("%Y%m%d") time = time.strftime("%Y%m%d%H%M%S") # 定义path为文件路径,目录级别,可根据实际情况自定义修改 path = 'F:\\Python_test\\' + date + "\\login\\" + time + "\\" # 定义报告文件路径和名字,路径为前面定义的path,名字为report(可自定义),格式为.html report_path = path + "report.html" # 判断是否定义的路径目录存在,不能存在则创建 if not os.path.exists(path): os.makedirs(path) else: pass # 定义一个测试容器 testsuite = unittest.TestSuite() # 将测试用例添加到容器 testsuite.addTest(testLo("test_login")) # 将运行结果保存到report,名字为定义的路径和文件名,运行脚本 report = open(report_path, 'wb') #with open(report_path, 'wb') as report: runner = HTMLTestRunner.HTMLTestRunner(stream=report, title=report_title, description=desc) runner.run(testsuite) # 关闭report,脚本结束 report.close()

csv文件格式:

备注:

使用python处理中文csv文件,并让execl正确显示中文(避免乱码)设施编码格式为:utf_8_sig,示例:

''''' 将结果导出到result.csv中,以UTF_8 with BOM编码(微软产品能正确识别UTF_8 with BOM存储的中文文件)存储 ''' #data.to_csv('result_utf8_no_bom.csv',encoding='utf_8')#导出的结果不能别excel正确识别 data.to_csv('result_utf8_with_bom.csv',encoding='utf_8_sig')

以上这篇python利用Excel读取和存储测试数据完成接口自动化教程就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

本文共计647个文字,预计阅读时间需要3分钟。

如何用Python结合Excel实现测试数据的读取与接口自动化存储?

`http_request2.py` 是一个用于发送 HTTP 请求的 Python 脚本。以下是对该脚本开头内容的简化

如何用Python结合Excel实现测试数据的读取与接口自动化存储?

pythonhttp_request2.py - 发送 HTTP 请求的脚本import http.client

创建连接conn=http.client.HTTPConnection(example.com)

发送 GET 请求conn.request(GET, /)

获取响应response=conn.getresponse()

打印状态码和响应内容print(状态码:, response.status)print(响应内容:, response.read().decode())

关闭连接conn.close()

passport.cnblogs.com/user/signin') # dr.find_element_by_id('input1').clear() dr.find_element_by_id('input1').send_keys(user[0]) # dr.find_element_by_id('input2').clear() dr.find_element_by_id('input2').send_keys(user[1]) dr.find_element_by_id('signin').click() sleep(1) print('\n' + '测试项:' + user[2]) dr.get_screenshot_as_file(path + user[3] + ".jpg") try: assert dr.find_element_by_id(user[4]).text try: error_message = dr.find_element_by_id(user[4]).text self.assertEqual(error_message, user[5]) print('提示信息正确!预期值与实际值一致:') print('预期值:' + user[5]) print('实际值:' + error_message) except: print('提示信息错误!预期值与实际值不符:') print('预期值:' + user[5]) print('实际值:' + error_message) except: print('提示信息类型错误,请确认元素名称是否正确!') def tearDown(self): dr.refresh() # 关闭浏览器 dr.quit() if __name__ == "__main__": # 定义脚本标题,加u为了防止中文乱码 report_title = u'登陆模块测试报告' # 定义脚本内容,加u为了防止中文乱码 desc = u'登陆模块测试报告详情:' # 定义date为日期,time为时间 date = time.strftime("%Y%m%d") time = time.strftime("%Y%m%d%H%M%S") # 定义path为文件路径,目录级别,可根据实际情况自定义修改 path = 'F:\\Python_test\\' + date + "\\login\\" + time + "\\" # 定义报告文件路径和名字,路径为前面定义的path,名字为report(可自定义),格式为.html report_path = path + "report.html" # 判断是否定义的路径目录存在,不能存在则创建 if not os.path.exists(path): os.makedirs(path) else: pass # 定义一个测试容器 testsuite = unittest.TestSuite() # 将测试用例添加到容器 testsuite.addTest(testLo("test_login")) # 将运行结果保存到report,名字为定义的路径和文件名,运行脚本 report = open(report_path, 'wb') #with open(report_path, 'wb') as report: runner = HTMLTestRunner.HTMLTestRunner(stream=report, title=report_title, description=desc) runner.run(testsuite) # 关闭report,脚本结束 report.close()

csv文件格式:

备注:

使用python处理中文csv文件,并让execl正确显示中文(避免乱码)设施编码格式为:utf_8_sig,示例:

''''' 将结果导出到result.csv中,以UTF_8 with BOM编码(微软产品能正确识别UTF_8 with BOM存储的中文文件)存储 ''' #data.to_csv('result_utf8_no_bom.csv',encoding='utf_8')#导出的结果不能别excel正确识别 data.to_csv('result_utf8_with_bom.csv',encoding='utf_8_sig')

以上这篇python利用Excel读取和存储测试数据完成接口自动化教程就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。