五分钟学会接口自动化测试框架

转载请注明出处❤️

你好,我是测试蔡坨坨。

今天,我们来讨论一下什么是接口自动化测试。我该怎么开始呢?接口自动化测试框架的作用是什么?

[En]

Today, let’s talk about what interface automation testing is. How do I start? What does the interface automation testing framework do?

自动化测试是近年来业界的一个热词,不仅是测试人员的必备技能,也是未来软件测试的发展趋势。

[En]

Automated testing, a hot word in the industry in recent years, is not only a necessary skill for testers, but also the development trend of software testing in the future.

特别是在敏捷模式下,产品迭代速度快,市场不断调整,客户需求不断变化,单纯的手工测试越来越无法适应整个变化过程(迭代快,加班多)。测试人员如何快速响应并保证产品在上线后的质量能够满足市场要求(如何在上线一个新功能的同时快速对旧功能快速进行回归,保证旧功能不被新功能影响而出现严重的Bug?)。

针对以上问题,采用自动化测试无疑是一个不错的选择,能够做到在 保证产品质量的同时 提升测试效率

随着行业体量越来越大,对测试岗位的要求水涨船高,而自动化测试这个词将出现在岗位招聘要求中,因此它也是跳槽面试、晋升和加薪的必备工具。

[En]

As the volume in the industry becomes more and more serious, the requirements for testing positions are rising all boats, and the word automated testing will appear in job recruitment requirements, so it is also a necessary tool for job-hopping interviews, promotions and salary increases.

自动化测试又可分为接口自动化、Web UI自动化、App自动化,今天我们就来聊聊接口自动化测试。

  • Python/Java + Requests + Unittest/Pytest + HTMLTestRunner/Allure
  • RobotFramework:关键词驱动的自动测试框架
  • 基于Web的自动化测试平台(公司自主研发,成本较高,方便不懂编程也能使用)

自动化测试工具有很多,每种都有自己的优缺点,选择一个适合自己实际情况的框架,实现是重点,具体选择哪种工具放在其他文章中。

[En]

There are many automated testing tools, each has its own advantages and disadvantages, choose a framework suitable for their own actual situation, implementation is the focus, the specific choice of which tool to be put in other articles.

这里使用 Python + Requests + Pytest + Allure

接口信息:

名称:全国高校信息查询接口
描述:用于查询全国高校信息
Host: www.iamwawa.cn
Request URL:/home/daxue/ajax
Request Method:POST
Content-Type: application/x-www-form-urlencoded
headers:user-agent:Chrome

参数:

名称 类型 是否必填 描述 type String 是 name,根据名称查询 keyword String 是 高校名称,如:四川轻化工大学

请求示例:

POST /home/daxue/ajax HTTP/1.1
Host: www.iamwawa.cn
user-agent: Chrome
Content-Type: application/x-www-form-urlencoded
Cookie: PHPSESSID=sjsrn0drje6ds5fq9kouoo2r23
Content-Length: 54

type=name&keyword=四川轻化工大学

响应示例:

{
    "status": 1,
    "info": "查询成功!",
    "data": [
        {
            "id": "2181",
            "name": "四川轻化工大学",
            "code": "4151010622",
            "department": "四川省",
            "city": "自贡市",
            "level": "本科",
            "remark": ""
        }
    ]
}
import requests

res = requests.post(url="https://www.iamwawa.cn/home/daxue/ajax",
                    headers={"user-agent": "Chrome"},
                    data={"type": "name", "keyword": "四川轻化工大学"})
assert res.status_code == 200
res_json = res.json()
print(res_json)
assert res_json["status"] == 1

有了上面的代码,最基本和最简单的接口测试就完成了,一个良好的开端就是成功的一半。

[En]

With the above code, the most basic and simplest interface testing is done, and a good beginning is half the success.

然而,问题随之而来,线性脚本的缺点暴露出来:

[En]

However, the problem followed, and the shortcomings of linear scripts were exposed:

因此,下一步是如何优化线性脚本,即如何实现代码的高内聚低耦合,这也是接口自动化测试框架要解决的问题。

[En]

Therefore, the next step is how to optimize linear scripts, that is, how to achieve high cohesion and low coupling of code, which is also a problem to be solved by the interface automation testing framework.

GitHub开源代码:关注微信公众号 测试蔡坨坨,回复关键字 源码获取

  • base_api.py:对Requests库进行二次封装,完成对api的驱动
  • api:继承base_api,将http请求接口封装成Python方法
  • utils:CommonUtil,公共模块,将一些公共函数、方法以及通用操作进行封装,如:日志模块、yaml操作模块、时间模块
  • config:配置文件模块,配置信息存放,如:URL、Port、Headers、Token、数据库信息等
  • data:测试数据模块,用于测试数据的管理,数据与脚本分离,降低维护成本,提高可移植性,如:yml文件数据
  • cases:测试用例模块,用于测试用例的管理,这里会用到单元测试框架,如:Pytest、Unittest
  • run.py:批量执行测试用例的主程序,根据不同需求不同场景进行组装,遵循框架的灵活性和扩展性
  • logs:日志模块,用于记录和管理日志,针对不同情况,设置不同的日志级别,方便定位问题
  • reports:测试报告模块,用于测试报告的生成和管理,如:基于Allure生成的定制化报告 五分钟学会接口自动化测试框架

最后,可以关注公众号 测试蔡坨坨,和坨坨一起学习软件测试,升职加薪 ~

关于软件测试相关问题,都可以添加我微信私信交流: caituotuo666

如果你需要学习资料,你也可以发私信!免费获取简历、面试问题、自动化测试、测试开发、性能等30种学习资源。

[En]

If you need learning materials, you can also send private messages! Free access to resumes, interview questions, automated testing, test development, performance and other 30 kinds of learning resources.

Original: https://www.cnblogs.com/caituotuo/p/16296668.html
Author: 测试蔡坨坨
Title: 五分钟学会接口自动化测试框架

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/499523/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球