Skip to content

Husna-POYRAZ/api-test-with-rest-assured

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Automating API tests with REST Assured

What is an API?

  • Application
  • Programming
  • Interface

The API Contract

  • Request
    . Endpoint
    . Headers
    . Body
  • Response
    . Status Code
    . Headers
    . Body

REST Assured Feature

  • Parameterization and data driven testing
  • Verifying response headers and bodies
  • Optimizing your test code through reuse
  • (De-) serializing request and response bodies
  • Java DSL for writing tests for RESTful APIs
  • Runs on top of JUnit or TestNG
  • Supports all HTTP methods (GET, POST, PUT, DELETE, ...)
  • Supports Gherkin syntax (Given/When/Then)
  • Uses Hamcres matchers for checks
  • Uses JsonPath/Gpath and XmlPath for selecting elements from response
  • Developed and maintained by Johan Haleby
  • Source: https://rest-assured.io/

Configuring REST Assured

  • Maven
        <!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>4.4.0</version>
            <scope>test</scope>
        </dependency>
  • TestNG
        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.4.0</version>
            <scope>test</scope>
        </dependency>

API Test

Website: https://www.zippopotam.us/

  • Supplies location data related to given country and zip code

  • An example request and response
    Structure: api.zippopotam.us/country/postal-code
api.zippopotam.us/us/90210
  • JSON Result
{
   "post code": "90210",
   "country": "United States",
   "country abbreviation": "US",
   "places": [
       {
           "place name": "Beverly Hills",
           "longitude": "-118.4065",
           "state": "California",
           "state abbreviation": "CA",
           "latitude": "34.0901"
       }
   ]
}

Tests Result

restassured

How to create a parameterized REST Assured test?

  • Step 1: Create a test data collection
  • Step 2: Feed data collection to your test method
  • Step 3: Use path parameters to parameterize REST Assured tests
  • Step 4: Update the specified test data

parameterizing

Optimizing REST Assured code

1- Reusable request specifications
2- Reusable response checks
3- Extracting response values for reuse

optimizing