本文共 2846 字,大约阅读时间需要 9 分钟。
我们首先来看图灵机器人的API接口。通过访问http://www.tuling123.com/openapi/api
,可以直接在浏览器中查看接口返回结果。为了详细了解接口信息,我们参考了图灵机器人的官方文档http://www.tuling123.com/help/h_cent_webapi.jhtml?nav=doc
,其中提供了规范的接口说明和使用指南。
在开始测试之前,我们需要明确测试的目标和思路。主要包括以下方面:
通过文档获取的信息包括:
基于以上信息,我们设计了以下测试用例。
测试用例基于YAML文件组织,便于读写和管理。以下是测试用例的主要内容:
在编写测试脚本时,我们利用了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/