当前位置:网站首页>Detailed explanation of rest assured interface testing framework
Detailed explanation of rest assured interface testing framework
2022-07-26 01:02:00 【Nice2cu_ Code】
REST-assured
List of articles
One 、 Interface test introduction
What is interface testing
- Essentially based on some kind of agreement , Send request to server , The server returns the response data , Analyze the response data , Judge whether it is consistent with our expectations , To verify whether the function is correct .
Why do interface tests
- Find problems earlier , Reduce R & D costs
- Reduce the labor cost and time of regression testing , Fast quality feedback
- It's easier to achieve continuous integration
Interface automation test process
Understand business needs
Analyze the interface test scope
Interface test case design
Interface test framework selection , The common ones are RestAssured
Interface test case writing
Framework and use case maintenance
Continuous integration
Two 、REST-assured Basic use
REST-assured It's a test for REST API And the designed testing framework .
Introduce dependencies
<dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>4.2.0</version> <scope>test</scope> </dependency>Grammar format
given(). XXXX when(). XXXX then(). XXXX- given: Set test presets , Include request headers 、 Request parameters 、 Request body 、cookie etc.
- when: What to do , That is, the URL of the request (GET / POST request )
- then: Analysis results 、 Assertion
3、 ... and 、 Send a request
httpbin It's a test HTTP Requested URL , What parameters are passed to it , It will return any parameters .
according to http://httpbin.org/get perhaps http://httpbin.org/post The difference is get still post request .
3.1 GET request
Mode one : Directly in URL Splice parameters after address
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
public class Test {
@Test
public void test() {
given().
when().
get("http://httpbin.org/get?name=jay").
then().
log().body(); // Return the data in the response body
//log You can output the returned information to the console
//log().all() Data in all responses can be returned
}
}
Running results :

Mode two : adopt queryParam Method to add parameters
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
public class Test {
@Test
public void test() {
given().
queryParam("name", "jay").
queryParam("age", "22").
when().
get("http://httpbin.org/get").
then().
log().body();
}
}
Running results :

3.2 POST request
1. form Form parameter type
public class Test {
@Test
public void test() {
given().
formParam("name", "jay").
formParam("password", "qwerty").
when().
post("http://httpbin.org/post").
then().
log().body();
}
}
Running results :

2. JSON Parameter type
String jsonData = "{\"mobilephone\":\"13323234545\",\"password\":\"234545\"}";
given().
body(jsonData).
contentType(ContentType.JSON). // Use body Type must be specified when
when().
post("http://httpbin.org/post").
then().
log().body();
3. xml Parameter type
String xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<suite>\n" +
" <class> test xml</class>\n" +
"</suite>";
given().
contentType(ContentType.XML).
body(xmlStr).
when().
post("http://www.httpbin.org/post").
then().
log().body();
4. Upload files
REST-assured There's a name multiPart() Can specify the file (file)、 Byte array (byte-array)、 Input stream or upload file :
given().
multiPart(new File("D:\\match.png")).
when().
post("http://httpbin.org/post").
then().
log().body();
Four 、 Get a response
adopt extract().response() Assign the response result to a Response In variables of type .
Response res=
given().
contentType(ContentType.JSON).
body(jsonData).
//headers(Map map), take JSON Multiple formats header, adopt fastjson convert to map:Map result = (Map) JSON.parse(JSON_String);
when().
post("http://httpbin.org/post").
then().
extract().response();
// Get the response time of the interface (ms)
System.out.println(res.time());
// Get response header information
System.out.println(res.getHeader("Content-Type"));
// Get the response body information (Json Format )
System.out.println(res.jsonPath().get("lotto.lottoId"));
Gpath Use :
Gpath It is used to extract a specific data in the response .
- extract JSON:
res.jsonPath().get("XXX.XXX.XXX"); - extract xml:
res.xmlPath().get("XXX.XXX.XXX"); - extract HTML:
res.htmlPath().get("XXX.XXX.XXX");
extract JSON Example :

extract HTML Example :
obtain HTML Response time , adopt Tag name . Tag name To get the value in the tag , adopt @ Property name Get the value of the attribute :

extract xml And extraction HTML Almost the same way of using .
Be careful :
REST-assured When getting the response, if the return is JSON decimal , So the corresponding Java The type is float, But in order to solve the problem of losing accuracy , Generally, it will be converted into BigDecimal.
If you want to convert the returned result into BigDecimal, Need to add configuration , Here's the picture :

Convert floating point numbers to BigDecimal:
BigDecimal bigDecimal = BigDecimal.valueOf(3.14159);Global configuration :

Used
baseURIAfter the global configuration, the address will be spliced automatically :
Some basic automated processes : For example, the test cases of login registration are written in the same Excel In file , have access to
@BeforeMethodAnnotation mark registration method ( Register before login ),@TestAnnotation mark login method , Define two dataprovider Method , Read separately Excel A few lines of are used as test cases for login or registration , Put these test cases in the corresponding test methods and run them in sequence .
边栏推荐
- 力扣 25. K 个一组翻转链表
- [RTOS training camp] course learning methods and C language knowledge (pointer, structure, function pointer, linked list) and student questions
- 手游多开用模拟器多开游戏如何切换IP搬砖
- 《暗黑破坏神:不朽》手游如何多开搬砖及新手入门搬砖攻略
- matlab 移位操作基础
- 用 QuestPDF操作生成PDF更快更高效!
- How to use if in sql service
- [install software after computer reset] software that can search all files of the computer, the best screenshot software in the world, free music player, JDK installation, MySQL installation, installa
- 【秒杀概念】大小端
- [RTOS training camp] equipment subsystem, questions of evening students
猜你喜欢

【MATLAB appdesigner】27_ How to debug and view variables in appdesigner? (examples + skills)
![[RTOS training camp] about classes and Q & A](/img/ea/32a4f4a5be29afcd0a68a0bcf0169f.jpg)
[RTOS training camp] about classes and Q & A
![[RTOS training camp] operation explanation, queue and ring buffer, queue - transmission data, queue - synchronization tasks and evening class questions](/img/59/0b671129e8cfaed28e2fe484ae0467.jpg)
[RTOS training camp] operation explanation, queue and ring buffer, queue - transmission data, queue - synchronization tasks and evening class questions
![[plaything determination scratch children programming] ride a small motorcycle (dynamic background + camera control operation)](/img/35/5fe27d3f61b41bf4b9434cd4d7d1b1.png)
[plaything determination scratch children programming] ride a small motorcycle (dynamic background + camera control operation)

【RTOS训练营】晚课学员问题
![[RTOS training camp] I2C and UART knowledge and preview arrangement + evening class questions](/img/4c/3c0453caaa4e7e8ce64da8ff3be93b.jpg)
[RTOS training camp] I2C and UART knowledge and preview arrangement + evening class questions

985 associate professors in Colleges and universities sun their annual salary, and the provident fund tops the monthly salary of ordinary people. Netizen: it is worthy of being in Shanghai
![[RTOS training camp] problems of evening students](/img/4a/9d781a28751c15e9e42cd5743e97db.jpg)
[RTOS training camp] problems of evening students

【RTOS训练营】设备子系统、晚课学员提问

The difference and application of in and exists in SQL statement
随机推荐
Gcdqueue encapsulation
力扣 25. K 个一组翻转链表
Oauth2 and JWT
JDBC实现MySQL8.0数据库的增删改查
RHCE之at和crontab命令详解及chrony部署
[RTOS training camp] problems of evening students
The bumpy road of referencing jar package json-path.jar in jmeter/idea
我们没有退路了
【秒杀概念】大小端
pip install --upgrade can‘t find Rust compiler
109. 使用 SAP UI5 FileUploader 控件上传本地文件
EasyCVR页面添加Loading加载效果的实现过程
【RTOS训练营】继续程序框架、tick中断补充、预习、课后作业和晚课提问
Lock upgrade: no lock, bias lock, lightweight lock, heavyweight lock
REST-assured接口测试框架详解
Django数据库增删改查
MySQL pymysql operation
How can MySQL just duplicate data?
Processes and threads
【RTOS训练营】设备子系统、晚课学员提问