Advanced USAGE and TIPS for the Rest Assured Framework
East Assured framework for advanced usage and skills
REST ASSURED is a popular Java -based testing framework, which provides strong support for the Restful API automation test.In addition to basic requests and response verification, REST Assured also provides many senior functions and skills to help developers test and verify their API easier.
Here are some advanced usage and techniques for the REST Assured framework.
1. Increase coverage through parameterized testing:
REST ASSURED allows parameterized testing to increase test coverage.You can use Junit's `@Parameterizedtest` annotation and`@Methodsource` to provide different input data and run the same tests according to these input data.
@ParameterizedTest
@MethodSource("testData")
void validateStatusCode(String baseUri) {
given()
.baseUri(baseUri)
.when()
.get("/api/books")
.then()
.statusCode(HttpStatus.SC_OK);
}
static Stream<String> testData() {
return Stream.of("http://example.com", "http://localhost:8080");
}
2. Add custom head information to the request:
REST Assured allows you to add custom HTTP header information to your request.You can use the `header` method to add head information, and add multiple head information through the` headers` method in the `given` method chain.
given()
.header("Authorization", "Bearer token")
.headers("Content-Type", "application/json", "Accept", "application/json")
.when()
.get("/api/books")
.then()
.statusCode(HttpStatus.SC_OK);
3. Use path parameters and query parameters:
REST Assured allows you to use path parameters and query parameters to build complex requests.You can use the occupying symbol in the URL to represent the path parameter and use the `Pathparam` method to set the actual value.For query parameters, you can use the `QueryParam` method to set the parameter value.
given()
.pathParam("id", 123)
.queryParam("sort", "desc")
.when()
.get("/api/books/{id}")
.then()
.statusCode(HttpStatus.SC_OK);
4. Configure global basic information:
REST ASSURD allows you to configure global basic information before testing, such as basic URL, port, and root path.You can use the static method in the `RESTASSURED` class to configure this information, so you don't have to repeat specifications in each request.
RestAssured.baseURI = "http://example.com";
RestAssured.port = 8080;
RestAssured.basePath = "/api";
5. Conclusion the response:
REST ASSURED provides a wealth of assertions that can perform a flexible verification of response.You can use the `Body` method in the` THEN` method chain to verify the attributes, values and data types in the response body. You can also use the `header` method to verify the response header information.
given()
.baseUri("http://example.com")
.when()
.get("/api/books")
.then()
.statusCode(HttpStatus.SC_OK)
.body("size()", equalTo(10))
.body("books[0].title", equalTo("REST Assured in Action"))
.header("Content-Type", containsString("application/json"));
These are just examples of some advanced usage and techniques in the REST assured framework.By making full use of the functions provided by Rest Assured, developers can test and verify their Restful API more effectively.Whether it is parameterized testing, custom header information, path parameters, and query parameters, or global configuration and response assertions, REST assured provides many useful tools to simplify the process of API testing.