当前位置:网站首页>Technology sharing | how to do Assertion Verification in interface automated testing?
Technology sharing | how to do Assertion Verification in interface automated testing?
2022-07-28 06:53:00 【Yehna rahmin】
In the process of server-side automated testing , After the request is initiated, the response value needs to be verified . After verifying that the response information meets the expected value , This interface automation test case can be considered as a complete pass . So this chapter , It will be explained in the interface automation test , How to assert and verify the response content returned by the server .
Practice
Initiate interface service HTTP Request information , After obtaining the response content , Make Assertion Verification .
Python Demo code
After initiating the request , Use a variable r Store the contents of the response , That is to say Response object .
r = requests.get("https://httpbin.ceshiren.com/get")
In response to the results :
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.ceshiren.com",
"User-Agent": "python-requests/2.25.1",
"X-Forwarded-Host": "httpbin.ceshiren.com",
"X-Scheme": "https"
},
"origin": "119.123.205.82",
"url": "https://httpbin.ceshiren.com/get"
}
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 .
Response assertion
Response status code assertion
- Assert success
import requests
r = requests.get('https://httpbin.ceshiren.com/get')
assert r.status_code==200
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 .
- Assertion failed
>>> import requests
>>> r = requests.get('https://httpbin.ceshiren.com/get')
>>> assert r.status_code==400
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
From the above example, we can see , The actual output of this response status code is consistent with the expected result status code 400 It's not equal , So an exception was thrown .
Json Response assertion
data = {
"hogwarts": ["a","b","c"]
}
r = requests.post('https://httpbin.ceshiren.com/post',json=data)
print(json.dumps(r.json(),indent=2))
assert r.status_code == 200
assert r.json()["json"]["hogwarts"][0] == "a"
In response to the results :
"args": {},
"data": "{"hogwarts": ["a", "b", "c"]}",
"files": {},
"form": {},
"headers": {
... Omit ...
},
"json": {
"hogwarts": [
"a",
"b",
"c"
]
},
"origin": "113.89.8.68",
"url": "https://httpbin.ceshiren.com/post"
}
adopt assert r.json()[“json”][“hogwarts”][0] == “a” Yes json Assert the content of , among r.json() Is to get the corresponding content ,r.json()[“json”] It's about getting json The content of ,r.json()[“json”][“hogwarts”] It's about getting hogwarts The content of ,r.json()[“json”][“hogwarts”][0] yes hogwarts The first data under .
Java Demo code
Java adopt then Perform assertion validation ,then() Method can verify many different types of response information .
- Assert that the response status code is successful
import static io.restassured.RestAssured.*;
public class Requests {
public static void main(String[] args) {
given().when().get("https://httpbin.ceshiren.com/get").
// Usually by then Perform assertion validation
then().statusCode(200);
}
}
adopt then() Method statusCode() Method to verify the response status code ,statusCode() Methods usually receive int Parameters of type .statusCode(200) Indicates whether the response status code is equal to 200, If not equal to 200 An exception will be thrown .
If you change the code verified by the above code interruption to statusCode(300), Then the console will output exception information .
Exception in thread "main" java.lang.AssertionError: 1 expectation failed.
Expected status code <300> but was <200>.
json Response assertion
import static io.restassured.RestAssured.*;
import static org.hamcrest.core.IsEqual.equalTo;
public class Requests {
public static void main(String[] args) {
given().when().get("https://httpbin.ceshiren.com/get").
then().body("headers.Host", equalTo("httpbin.ceshiren.com")).log().all();
}
}
adopt then().body(“headers.Host”, equalTo(“httpbin.ceshiren.com”)) Yes json Assert the content of , among then().body() Is to get the corresponding content , The first parameter is to extract the actual field value from the response content , The second parameter calls equalTo() Method , And the expected result is introduced into it .
边栏推荐
猜你喜欢

技术分享 | 接口测试价值与体系

DNS域名解析服务

技术分享 | 服务端接口自动化测试, Requests 库的这些功能你了解吗?

Cocos2d-x learning notes Tile Map tiledmap

It is recommended to wear air conduction earphones, which do not need to wear in ear

Skimming records -- sequence traversal of binary tree

DNS domain name resolution service

MySQL master-slave

Prometheus monitoring Nacos

MySQL主从
随机推荐
Hdu-2036-reform spring breeze blowing all over the ground (polygon area template)
思寒漫谈测试人职业发展
DHCP原理与配置
Array solution script
Technology sharing | how to simulate real use scenarios? Mock technology to help you
Implementation of simple address book in [c language]
[C language] dynamic memory management
软件测试的生命周期(流程)
浅谈Cookie和Session
HDU-1097-A hard puzzle(快速幂)
rancher部署实战
等保3.0-服务器三权分立配置
设计测试用例的方法
[c language] - step by step to achieve minesweeping games
测试面试题集锦(一)| 软件测试常见必考问题与流程篇(附答案)
HDU-5805-NanoApe Loves Sequence(思维题)
手把手教你三步完成测试监控系统搭建
mongo ssl 配置实战
Graphic pipeline foundation (I)
NFS 共享存储服务