博客
关于我
python 自动化接口测试(6)
阅读量:430 次
发布时间:2019-03-06

本文共 2846 字,大约阅读时间需要 9 分钟。

图灵机器人API接口测试实践

API接口概述

我们首先来看图灵机器人的API接口。通过访问http://www.tuling123.com/openapi/api,可以直接在浏览器中查看接口返回结果。为了详细了解接口信息,我们参考了图灵机器人的官方文档http://www.tuling123.com/help/h_cent_webapi.jhtml?nav=doc,其中提供了规范的接口说明和使用指南。

接口测试分析

在开始测试之前,我们需要明确测试的目标和思路。主要包括以下方面:

  • 测试API的URL地址
  • 测试请求方式(如POST、GET等)
  • 测试请求参数格式
  • 分析返回结果,进行合理的断言

通过文档获取的信息包括:

  • 请求地址
  • 请求参数格式
  • 返回结果的数据结构

基于以上信息,我们设计了以下测试用例。

测试用例设计

测试用例基于YAML文件组织,便于读写和管理。以下是测试用例的主要内容:

测试用例1

  • key: "aaaa"
  • content: "sasa"
  • url: "http://www.tuling123.com/openapi/api"
  • method: "POST"
  • expected code: "40001" # 密码错误

测试用例2

  • key: "dfeb1cc8125943d29764a2f2f5c33739"
  • content: ""
  • url: "http://www.tuling123.com/openapi/api"
  • method: "POST"
  • expected code: "40002" # 未输入内容

测试用例3

  • key: "dfeb1cc8125943d29764a2f2f5c33739"
  • content: ""
  • url: "http://www.tuling123.com/openapi/api"
  • method: "POST"
  • expected code: "40007" # 格式异常

测试用例4

  • key: "dfeb1cc8125943d29764a2f2f5c33739"
  • content: "sdsad"
  • url: "http://www.tling123.com/openapi/api"
  • method: "POST"
  • expected code: "40004" # 次数用完

测试脚本编写

在编写测试脚本时,我们利用了Python的requests库来发送HTTP请求,并使用unittest框架进行测试用例的执行和结果验证。以下是测试脚本的主要部分:

import requestsimport yamlimport unittestclass TestTuling(unittest.TestCase):    def setUp(self):        self.data_file = open(r"C:\Users\Administrator\Desktop\jiejko\data.yaml", "r", encoding="utf-8")        self.data = yaml.load(self.data_file)        self.post_data = self.data['post']    def tearDown(self):        pass    def test_post1(self):        self.url = self.post_data['post1']['url']        self.key = self.post_data['post1']['key']        self.content = self.post_data['post1']['content']        self.method = self.post_data['post1']['method']        self.expected_code = int(self.post_data['post1']['code'])        self.param = {'key': self.key, 'info': self.content}        if self.method == 'POST':            response = requests.post(url=self.url, params=self.param)            response.encoding = 'UTF-8'            self.assertEqual(response.json()['code'], self.expected_code, "接口返回标识符错误")        else:            print(f"不支持{self.method}方式请求")    # 其他测试方法类似,依次测试post2、post3、post4

测试报告生成

在测试完成后,我们使用HTMLTestRunner生成测试报告。报告包含详细的测试结果和执行信息,方便团队查看和分析。以下是生成报告的代码片段:

import timefrom unittest import TestSuite, HTMLTestRunnerimport ossuite = TestSuite()suite.addTest(TestTuling("test_post4"))suite.addTest(TestTuling("test_post3"))suite.addTest(TestTuling("test_post2"))suite.addTest(TestTuling("test_post1"))current_time = time.strftime("%Y%m%d_%H%M%S")report_dir = os.path.join(os.path.abspath(__file__), "report")os.makedirs(report_dir, exist_ok=True)html_file = os.path.join(report_dir, "test_report.html")with open(html_file, "wb") as fp:    runner = HTMLTestRunner(fp, title="测试报告", description="API测试结果")    runner.run(suite)

后续优化与扩展

在测试脚本完成后,我们进行了以下优化:

  • 加入了日志模块,便于跟踪测试过程
  • 增加了多线程执行,提高测试效率
  • 自动化生成测试报告,支持邮件发送

通过以上步骤,我们成功完成了图灵机器人API接口的全面测试,确保接口的稳定性和可靠性。如有任何问题或需要进一步的帮助,请随时联系我。

转载地址:http://vdjyz.baihongyu.com/

你可能感兴趣的文章
MySQL-redo日志
查看>>
MySQL-【1】配置
查看>>
MySQL-【4】基本操作
查看>>
Mysql-丢失更新
查看>>
Mysql-事务阻塞
查看>>
Mysql-存储引擎
查看>>
mysql-开启慢查询&所有操作记录日志
查看>>
MySQL-数据目录
查看>>
MySQL-数据页的结构
查看>>
MySQL-架构篇
查看>>
MySQL-索引的分类(聚簇索引、二级索引、联合索引)
查看>>
Mysql-触发器及创建触发器失败原因
查看>>
MySQL-连接
查看>>
mysql-递归查询(二)
查看>>
MySQL5.1安装
查看>>
mysql5.5和5.6版本间的坑
查看>>
mysql5.5最简安装教程
查看>>
mysql5.6 TIME,DATETIME,TIMESTAMP
查看>>
mysql5.6.21重置数据库的root密码
查看>>
Mysql5.6主从复制-基于binlog
查看>>