Rest Assured Python

0 Comments

Introduction to REST Assured

In today’s software development landscape, API testing plays a crucial role in ensuring the functionality and reliability of web services. REST Assured is a popular Java-based library that simplifies API testing by providing a powerful and easy-to-use syntax for writing test cases.

In this blog, we’ll explore how to set up and use REST Assured for API automation testing, covering:

  • Setting up REST Assured
  • Writing basic API test cases
  • Validating API responses
  • Handling authentication

1. Setting Up REST Assured

Before you start writing test cases, you need to set up REST Assured in your project. If you’re using Maven, add the following dependency to your pom.xml:

<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>5.3.0</version>
    <scope>test</scope>
</dependency>

For Gradle, add this line to build.gradle:

testImplementation 'io.rest-assured:rest-assured:5.3.0'

2. Writing Your First REST Assured Test

Once the setup is complete, let’s write a basic test to verify a GET API request.

import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.testng.Assert;
import org.testng.annotations.Test;

public class ApiTest {
    @Test
    public void testGetRequest() {
        Response response = RestAssured.get("https://jsonplaceholder.typicode.com/posts/1");

        // Print response
        System.out.println(response.getBody().asString());

        // Validate status code
        Assert.assertEquals(response.getStatusCode(), 200);
    }
}

3. Validating JSON Response Data

import io.restassured.RestAssured;
import io.restassured.response.ValidatableResponse;
import org.testng.annotations.Test;
import static org.hamcrest.Matchers.*;

public class ResponseValidationTest {
    @Test
    public void validateJsonResponse() {
        RestAssured.get("https://jsonplaceholder.typicode.com/posts/1")
                .then()
                .statusCode(200)
                .body("userId", equalTo(1))
                .body("id", equalTo(1))
                .body("title", notNullValue());
    }
}

4. Sending a POST Request with JSON Body

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.testng.annotations.Test;
import static org.hamcrest.Matchers.*;

public class PostRequestTest {
    @Test
    public void testPostRequest() {
        String requestBody = "{ \"title\": \"QA Automation\", \"body\": \"REST Assured Guide\", \"userId\": 1 }";

        Response response = RestAssured.given()
                .contentType(ContentType.JSON)
                .body(requestBody)
                .post("https://jsonplaceholder.typicode.com/posts");

        // Validate response
        response.then()
                .statusCode(201)
                .body("title", equalTo("QA Automation"))
                .body("userId", equalTo(1));

        System.out.println("Response: " + response.getBody().asString());
    }
}

5. Handling Authentication in REST Assured

Basic Authentication:

RestAssured.given()
    .auth()
    .basic("username", "password")
    .get("https://api.example.com/secure-endpoint")
    .then()
    .statusCode(200);

Bearer Token Authentication:

RestAssured.given()
    .header("Authorization", "Bearer your_token_here")
    .get("https://api.example.com/protected-resource")
    .then()
    .statusCode(200);

6. Using Query and Path Parameters

Path Parameters:

RestAssured.given()
    .pathParam("id", 1)
    .get("https://jsonplaceholder.typicode.com/posts/{id}")
    .then()
    .statusCode(200);

Query Parameters:

RestAssured.given()
    .queryParam("userId", 1)
    .get("https://jsonplaceholder.typicode.com/posts")
    .then()
    .statusCode(200);

7. Running Tests in TestNG Framework

import org.testng.annotations.Test;
import static io.restassured.RestAssured.get;
import static org.hamcrest.Matchers.equalTo;

public class TestNGExample {
    @Test
    public void testApiResponse() {
        get("https://jsonplaceholder.typicode.com/posts/1")
            .then()
            .statusCode(200)
            .body("userId", equalTo(1));
    }
}

Conclusion

REST Assured is a powerful and flexible tool for API automation testing. In this guide, we covered:

  • Setting up REST Assured
  • Writing GET and POST requests
  • Validating JSON responses
  • Handling authentication
  • Using query and path parameters

With REST Assured, API automation becomes easy and efficient! Start writing your test cases today and improve your API testing process. 🚀