当前位置:网站首页>Interface test framework practice (II) | interface request assertion
Interface test framework practice (II) | interface request assertion
2022-04-21 17:59:00 【Hua Weiyun】
The picture is quoted from Real Python
Interface request assertion means that after the request is initiated , Judge the returned response content , Used to check whether the response content is consistent with the specified return value .
After initiating the request , We use a variable r Store the contents of the response , That is to say Response object .
Response Object has many powerful methods to call , For example, get the response header directly , obtain Unicode The encoded response content , Get binary response content , Get the original response content and so on .
Get the response header
Get the encoded response value :
You can also use r.raw Get the original response content ,r.content Get binary response content , In addition, the code is JSON The response content of the format , It will be detailed in later chapters .
install JSON library :
Response status code assertion :
assert yes Python Built in functions for , Used to determine the expression , When the expression condition is False An exception will be triggered when .
r.status_code yes Response Object , The status code used to get the return value .
assert r.status_code==200 Is to judge whether the status code is equal to 200, If not equal to 200 An exception will be thrown .
Counter example : Response status code assertion , Determine whether the response status code is 400
As can be seen from the previous example , The response code should be 200, Because with 400 It's not equal , So an exception was thrown .
During the test , The return value of most interfaces is JSON Format . therefore , Master how to JSON The skill of asserting response values , It can more easily improve the interface automation test cases .
r.json() For response values r to JSON code :
For dictionary format , Can pass dict[“key”] The way to get value value .
For list format , Can pass list[index] Get the corresponding index value value .
stay JSON In the assertion of , The main application is the search method of dictionary and list . If you encounter mixing or nesting , It just needs to be removed layer by layer , Until you find the field that needs to be asserted .
Dictionary format assertion , Judge headers Medium Host by httpbin.org 1
1. The first layer is key The value is “header” Of value
2. The second level is key The value is “Host” Of value
3. Judge key The value is “Host” Of value Is it worth it “httpbin.org 1” equal
Dictionary mixed list format assertion , Judge hogwarts The first place in the corresponding list is ‘a’
4. The first layer is key The value is ‘form’ Of value
5. The second level is key The value is ‘hogwarts’ Of value
6. The third layer is indexed as 0 Of value
7. Judge that the previous index is 0 Of value Is it equal to “a”
More , We will read it in detail in the following chapters .
版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211756229837.html
边栏推荐
- MySQL进阶之表的增删改查
- In cabin visual AI application front loading "increased by nearly 200% year-on-year". Which suppliers lead the Chinese market
- 做自媒体副业真能月入上万?这篇分享,不藏私
- 修改van-dropdown-menu默认高度
- Variable与Tensor合并后,关于训练、验证的相关变化
- 为什么基础设施工程师更喜欢MySQL?
- 怎么获取map值才不那么low,获取map参数的三种方式
- 单片机能做什么,你有什么有单片机或开源硬件做的有意思的作品吗
- 用户发送请求到执行controller方法的过程发生了什么
- PMP每日一练 | 考试不迷路-4.21
猜你喜欢

【acwing】165. 小猫爬山(DFS之剪枝)

MySQL数据库常识之储存引擎

What is ODBC – open database connectivity

Linux~libc.so.6(GLIBC_2.28)(64bit) 被 redis-5.0.3-1.nfs.x86_64 需要

Pytorch数据封装进入网络前的几种方式

MySQL进阶之常用函数

MySQL localization workbench localization XML file

Do we media sidelines really earn tens of thousands a month? This article is shared without privacy

动态规划:硬币题目总结

5000 movie datasets from tmdb
随机推荐
Beijing quasi GPS Beidou satellite timing system helps South Asia electronic production management system
mysql查询表字段默认值
小小的模糊查询,竟来来回回修改了3次代码?找个电子厂上班吧
mysql8. Cannot start after 0 setting ignores case
How to get the map value is not so low. There are three ways to get the map parameters
Appreciation of single chip microcomputer DIY works, beginners come in to worship
接口测试框架实战(一) | Requests 与接口请求构造
Logstash ~ output of logstash
Build a short video platform and copy a demo of the login interface
MySQL进阶之表的增删改查
There is a prize for essay | a heavy prize of 5000 yuan. You are invited to contribute
国泰君安安全吗?靠谱嘛
华为云GaussDB(for Influx)揭密第六期——数据分级存储
【论文精读】Stable Linear Structures and Seam Measurements for Parallax Image Stitching
New ETL scheduling batch management tool taskctl 8.0 simplest installation
LogStash~LogStash的执行模型
Logstash ~ logstash one-stop tutorial (continuously updated)
【acwing】166. 数独****(DFS)
Shell programming learning (III) condition judgment and process control
Part time jobs are higher than wages. In 2022, there are three sidelines with a monthly income of more than 10000