python测试-接口及接口测试

python测试-接口及接口测试

【码尚】测试进阶全套最新学习视频+笔记资料
链接:https://pan.quark.cn/s/238bca791317

【软件测试项目包】
链接: https://pan.baidu.com/s/12YtCDsgbxH492T3IP7-egw?pwd=msxy 提取码: msxy

【码尚】2026最新测试学习路线图:https://www.processon.com/embed/697047648a03b33eba9ee6fa
(涵盖测试全职业测试技术,点开专题后数字可以看详细学习路径)

看看上面是不是你所需资料呢?不是的话记得资料截图或发下视频链接,给你发送更精准的学习资料呦

接口

概念(面试)

1
2
3
4
5
6
7
提到接口会涉及到4个概念:
客户端(顾客)
服务端(后厨厨师)
接口(服务员)
接口文档(菜单)

接口是两个独立系统或者组件进行交互的连接点,有具体的交互规范,但不涉及内部具体实现

意义

1
2
3
4
解耦
复用
标准化
生态

示例

1
2
3
硬件接口
系统接口
软件接口 api应用编程接口

为什么测试接口

测试金字塔

1
2
3
更集成		UI测试					更慢,效率更低,成本大
Service测试(也叫集成测试或者接口测试)
更孤立 unit测试 更快
1
2
3
4
5
UI测试:web,app  	(金融行业、银行测试涉及得多,不计成本)
集成层:承上启下 (一般的普通的软件开发公司,追求成本低、速度快、效果还不错、允许适当bug)
单元测试:白盒测试(代码走读走查) 有开发者进行更合适

软件测试一般上面两种涉及的多
1
集成测试(接口测试):承上启下,投入产出较高

接口测试特点

1
2
3
效率:比ui测试更快 一个用例十几秒;api一个用例0.3秒
收益:提前介入、快速验证、避免全平台BUG、测试驱动开发、收益高
生态:工具、教程非常多

HTTP接口(重要面试)

1
2
3
基于HTTP协议来进行提供服务的接口(重点关注,http接口是在互联网中应用最广泛的接口,>90%)

还有很多其它基于协议提供服务的接口 基于websockt、tcp、udp、rpc等

基本特点

1
2
3
4
5
6
7
8
9
交互简单:请求加响应模式(随时请求得到相应,没有请求不会响应),无状态(不会记得以前是否联系过,以及以后是否会联系) 案例:女神服务器 舔狗:客户端

结构简单:请求、响应,都是:行、头、体

内容简单:除了体,其它均为ASCII内容

应用广泛:上面的简单决定了此特征。至于体,支持json、html、xml、视频、图片、音乐

跨平台兼容:上面的简单决定了此特征。电脑、手机、电视、智能家居。

协议细节

cURL(面试和工作中用非常好)
1
cURL(Client URL)是一个强大的命令行工具,用于通过多种网络协议(如HTTP、HTTPS、FTP、SFTP等)与服务器进行数据传输。它广泛应用于API测试、数据获取、文件传输等场景。cURL 的设计目标是支持无用户交互的自动化操作。
-v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# curl www.baidu.com -v

C:\Users\Administrator>curl www.baidu.com -v
* Trying 153.3.238.127:80...
* Connected to www.baidu.com (153.3.238.127) port 80 (#0)
> GET / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/8.0.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Content-Length: 2381
< Content-Type: text/html
< Pragma: no-cache
< Server: bfe
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
< Date: Thu, 30 Apr 2026 14:49:26 GMT
<
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产 品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
* Connection #0 to host www.baidu.com left intact
1
2
*   Trying 153.3.238.127:80...
* Connected to www.baidu.com (153.3.238.127) port 80 (#0) # 建立连接,80端口(面试)
1
2
3
4
5
6
右箭头代表发送的请求
> GET / HTTP/1.1 # 行 面试中经常问都有哪些请求方式
> Host: www.baidu.com # 带冒号的都是头
> User-Agent: curl/8.0.1
> Accept: */*
> # 最后一个头也就是上一行开始两个换行符,也就是下一行没有了,也就是没有体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
左箭头代表给的回应
< HTTP/1.1 200 OK # 行
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform # 带冒号的都是头
< Content-Length: 2381 # 响应体的字节长度
< Content-Type: text/html # 响应体格式
< Pragma: no-cache
< Server: bfe
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
< Date: Thu, 30 Apr 2026 14:49:26 GMT # 服务时间
<
<!DOCTYPE html> # 最后一个头两个换行符,这一行开始都是体
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产 品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
* Connection #0 to host www.baidu.com left intact

响应的体是到结束才结束,没到结束也可以结束,到了结束也可以不结束

http请求

http请求行的请求方法
1
有无穷多的请求方法,只需要关注常见的就行了
  • 方法名 安全性 幂等性 可缓存性

    • GET v v v 获取数据、下载资源

    • HEAD v v v 与get一样,但没有正文

    • POST x x x 提交数据,创建资源

    • PUT x v x 修改数据、覆盖资源

    • PATCH x v x 修改[部分]数据

    • DELETE x v x 删除数据、资源

    • CONNECT x x x 创建代理隧道

    • OPTITIONS v v x 显示通信选项

      1
      2
      3
      幂等:比如一个方法调用无数次结果都是一样的

      上面的特征,不幂等一定不安全。幂等不一定安全(比如删除操作)
    1
    2
    这里的安全并不是真的安全。
    只有加密才算安全,HTTPS使用SSL,也不是绝对安全
http请求的头和体
1
2
3
4
5
头:描述(修饰体) 下面这列举了三种
- 缓存:Cache-Control,Last-Modified (期望返回304时会用到)
- 身份:Authorization(是否登录),User-Agent(以什么身份请求)
- Content-Type,Content-Length
体:类型太多没法讲
GET和POST区别是什么?(面试)

http响应

http响应行的状态码(面试)
1
2
3
4
5
6
7
有很多,也只需要关注常见的
2xx 都是与成功有关的
3xx 都是重定向(需要重新请求)
4xx 请求失效(自身原因)
5xx 服务器原因(开发原因)

只有1xx-5xx(http里1xx的状态码很少见了)
  • 状态码 含义 应用场景
    • 200 请求成功 网页正常加载、接口成功返回数据
    • 201 内容创建成功 创建新的文章、数据、资料
    • 204 请求成功但无返回 删除资源或数据上报
    • 301 永久重定向 网站更换域名后跳转
    • 302 临时重定向 登录后跳转回原页面、短链接跳转
    • 304 资源未修改(重定向到缓存中) 浏览器缓存生效中
    • 400 请求无效 客户端发送错误
    • 401 无授权 缺少身份信息
    • 403 禁止访问 权限不足
    • 404 资源不存在 访问已删除的页面或URL拼写错误
    • 405 方法不被允许 错误使用HTTP方法
    • 500 服务器内部错误 服务器代码异常
    • 502 网关错误 后端服务不可用
    • 504 网关超时 后端服务响应超时
POST会建立两次连接?(错误)
1
2
3
那是08年以前,服务器硬盘贵存储不够
第一次请求询问需要上传多大(返回101状态码,继续请求)
第二次上传固定大小的资源
http响应的头和体
1
2
3
4
5
头:描述(修饰体) 下面只列举了两种
- 缓存:Cache-Control,Last-Modified (期望返回304时会用到)
- 没有身份
- Content-Type,Content-Length
体:类型太多没法讲

接口测试

凡是可以发送http请求、接收http响应,能够显示其内容的工具,都可以对http接口进行测试

录制+重放

fiddler

fiddler测试步骤:

  • 配置代理,使fiddler能够录制请求
  • 点击replay and edit,修改参数(相当于编写测试用例)
  • 点击run,start,发送修改后的请求,得到新的响应
优势
1
2
3
基于真实的请求,有连贯业务场景
进行底层的精细化的操作
配套的相关请求,有客户端完成,降低使用门槛
劣势
1
2
3
需要UI端配合(没有发挥接口测试的优势)
不美观,不方便(一个个地方手动修改字符)
接口信息不全面(比如有的接口自带默认值,需要接口文档才能发现,也就是有的不看接口文档测试不完整)

调试工具

curl(方便)、postman(好看,美观)、jmeter(不如postman方便)

postman

不付费、不登录有的功能会限制

  • 输入请求方法、地址
  • 没必要去header里去一个个配置请求头,直接在body的row写上体会自动识别配置头

python

手写:

生成:postman也可以生成各种编程语言的接口测试代码