Application background
At present, many Web applications in the market have shifted to RESTful architecture, which often exposes users to a set of REST apis. The advantage of this is that developers can call different apis according to their needs and integrate their own applications.
Each set of apis then forms an information center that, when combined, forms an interconnected information architecture. So for this kind of lightweight popular architecture, it is necessary to test the scenario of interface services, although the mainstream tools like Postman or JMeter can also do the job, but there is always something missing in the overall design.
Big companies like Alibaba have been promoting their own customized quality platforms and designing specific test schemes suitable for their products. There is an open source interface service framework that supports restful protocols and transmission, verification and assertion of data formats such as XML and JSON.
REST Assured is a Java-implemented REST API testing framework. It is a lightweight REST API client that can write code directly to send HTTP requests to the server and verify the returned results. Its syntax is concise and is a DSL designed to test REST apis.
Rest Assured. IO /
Official documentation: Restful assured. IO /#docs
Github project address: github.com/rest-assure…
Simple practice
If we were to configure a simpler interface testing environment in the IDE, we could first configure REST Assured into Maven:
With this configured, let’s look at a typical test case:
Create a package named OurTest in SRC /test/ Java and create a GioApiTests class in the package
Let’s look at the code:
public class OurTest{ @Test public void getAddress(){ given() RequestApplication. Get (s: "https://ip:8090/trueTest/com/ourname/226514") Response, then () ValidateReponse. StatusCode (200); } @test public void postOurName(){String json= "{" \firstName\" :\ "wangtao\",\ "secondName\ :\" tao\ "} "; given() RequestApplication. ContentType (" application/json ") RequestApplication. Body (json) RequestApplication. Post (s: "IP: 8090 / trueTe St/com/ourname ") response. Then () ValidateReponse. StatusCode (200); }}Copy the code
Let’s explain:
The first is that Given () is a typical approach to REST Assured, where all the content of a request (header, body, etc.) is described. I don’t have to say much about get and post, how you send a request, then () is what you need to do after the request, and statusCode is what you need to say if 200 is returned.
This is easy to understand, basically based on a series of simple send, bind, verify process.
Of course, when we design the automation API platform, we still need to have the concept of platform in it. For the test team that is not familiar with code development, we need to present the input items in the form of pages, while for the team with certain development skills, we can do the same as above. Do a lot of development work based on the code and reST-Assured syntax.
Advanced practice
REST Assured has a requirement that static imports are best done:
io.restassured.RestAssured.*
io.restassured.matcher.RestAssuredMatchers.*
org.hamcrest.Matchers.*
Copy the code
This can improve some efficiency in use.
REST Assured will automatically try to determine which parameter types (that is, query or form parameters) based on HTTP methods
In the case of GET, the query parameters are used automatically, and in the case of POST, the form parameters are used. In some cases, it is important to separate form and query parameters in PUT or POST:
given(). formParam("formParamName", "value1"). queryParam("queryParamName", "value2"). when(). post("/something");
Copy the code
Some parameters can also be set at the URL:
when().get("/name? firstName=wang&lastName=tao");Copy the code
3. About the use of cookies
In normal mode, you can specify cookies by:
given().cookie("username", "John").when().get("/cookie").then().body(equalTo("username"));
Copy the code
Cookies can also be assigned multiple values like this:
This creates two cookies:
CookieName = Value1 and cookieName = value2.
Detailed cookies can also be specified in the following way:
Cookie someCookie = new Cookie.Builder("some_cookie", "some_value").setSecured(true).setComment("some comment").build(); given().cookie(someCookie).when().get("/cookie").then().assertThat().body(equalTo("x"));
Copy the code
Or specify multiple cookies at the same time:
Cookie cookie1 = Cookie.Builder("username", "John").setComment("comment 1").build(); Cookie cookie2 = Cookie.Builder("token", 1234).setComment("comment 2").build(); Cookies cookies = new Cookies(cookie1, cookie2); given().cookies(cookies).when().get("/cookie").then().body(equalTo("username, token"));
Copy the code
4, still like a point of assertion
Status code assertion:
get("/x").then().assertThat().statusCode(200). .. get("/x").then().assertThat().statusLine("something"). .. get("/x").then().assertThat().statusLine(containsString("some"))
Copy the code
A perfectly matched assertion:
get("/x").then().assertThat().body(equalTo("something"))
Copy the code
Form validation:
The form code:
<html><head><title>Login</title></head> <body><form action="j_spring_security_check" method="POST"><table><tr><td>User: </td><td><input type='text' name='j_username'></td></tr> <tr><td>Password:</td><td><input type='password' name='j_password'></td></tr> <tr><td colspan='2'><input name="submit" type="submit"/></td></tr> </table></form> </body></html>Copy the code
Assertion:
given(). auth().form("wang", "tao"). when(). get("/formAuth"); then(). statusCode(200);
Copy the code
experience
Based on the framework of the mainstream quality platform in the market at present, I recommend the basic framework of VUE+Springboot+Mybatis+Mysql, API interface test and the scene architecture designed by it. It is recommended that REST Assured’s secondary encapsulation approach (form REST Assured API), in conjunction with the VUe-based front-end framework, collects the message parameters, converts them into Json format and transmits them to REST Assured API, and first predetermines whether the URL is available. The variables and parameters between single threads are stored in the cache and disappear after they are closed. However, in scenarios, it is recommended to store them in local files or appolo configuration file storage media.
We hope that we can spread and summarize more, and design the customized functions of the quality platform flexibly based on product characteristics.
Author: Wang Tao/Test Manager
Recruitment information
GrowingIO technology team is an energetic and passionate technical team. We are looking for more positions! We are looking for big data engineer/Java engineer/data mining engineer. If you are interested, please send your resume to [email protected] (please specify your specific position title). More positions and information can enter the recruitment website to check!
About GrowingIO
GrowingIO is the leading one-stop data growth engine overall solution service provider in China. Founded in 2015, GrowingIO takes data intelligence analysis ability as the core. By constructing customer data platform, GrowingIO creates growth marketing closed-loop, helping enterprises improve data-driven ability, enabling business decisions and realizing business growth.