This page looks best with JavaScript enabled

API Test Automation - in minutes!!

 ·  ☕ 5 min read  ·  ✍️ Neeraj Sidhaye · 👀... views

In this post, I will show you, how simple and fast you can write automation tests for your API using ZeroCode framework

Before we start, let’s quickly read about Test Flavours - test types and where does ZeroCode framework fits in.

Test Flavours

Unit Tests

This covers smallest piece of code which validates whether targeted code block works as expected with varieties of inputs. We use JUNIT and other mocking frameworks like Mockito etc as required.

Integration Tests

This verifies communication paths and interaction between components and used to detect interface defects.
For Example:- If our application has got 3 microservices which might be consuming other external services or databases. Integration tests make sure that our application API’s are communicating well those external services as per agreed contacts.

Component Tests

This is where we write test which cover various layers within an API as a whole component. We don’t go outside of our api boundary.
For example:- In this we write test for our Controllers and then the test flow goes through service layers, repository layers, gateway layers. Here we mock the api response of external api’s, because we are testing our own API component and not the external boundaries.

Contract Tests

Verify the contracts with external boundary systems and making sure that it meets the contact expected by the consuming services.
For example:- Our application api’s are consuming external services and the contract with those external services meets the functionality.

End to end Tests

As the name suggests, this verifies that the system works as expected as a whole entity from end to end - covering all internal and external components together.

Ok, I think that pretty much covers various test flavours.

Alright cool, so if you have read about Test Flavours, then let’s get into code and find out how simple and quickly we can write tests with ZeroCode framework.

💥 ZeroCode tests can be written for API's exposed in any language (JAVA, GO, DOT NET etc ). As long the API is exposed over http, we are good to write test using ZeroCode.

Product API

We have got our Product API running locally with below end points and we will be writing ZeroCode tests for this Product API.

  1. GET /products
    Returns list of products

  2. POST /products
    Creates a new product

  3. PUT /products/{id}
    Updates a product for a given product id

  4. PATCH /products/{id}
    Partial updates to product for a given id

  5. DELETE /products/{id}
    Deletes a product for a given product id

Product API running locally

I have product api running on http://localhost:7070 and and it’s written in GO using Gorilla Mux library.

Please just click on below gif to get better view 😎

ProductService

ZeroCode tests

Steps to write ZeroCode tests.

Step 1 - Generate test project using ArcheType

Archetype is a fastest way to generate project skeleton and we can add boiler plate code with it.

Create a new folder ➡️ cd to that folder ➡️ copy below archetype command and just run it.

You should see a project ready for you with all the boiler plate code.

  mvn archetype:generate \
  -DarchetypeGroupId=org.jsmart \
  -DarchetypeArtifactId=zerocode-maven-archetype \
  -DgroupId=com.ns \
  -DartifactId=product-api-tests \
  -Dversion=1.0.0-SNAPSHOT
ArchType - see in Action

Please just click on below gif to get better view 😎

archtypeInAction

Step 2 - Writing Tests

Ok, Cool. We have got our project ready with dummy tests.
Now let’s update config and tests

  1. update hostconfig_ci.properties

    Open hostconfig_ci.properties file and add url of our Product api against the key web.application.endpoint.host.
    I have added url of locally running Product API.

    web.application.endpoint.host=http://localhost:7070

      #Continuous Integration Context
      # Web Server host and port
      web.application.endpoint.host=http://localhost:7070
      # Web Service Port; Leave it blank in case it is default port i.e. 80 or 443 etc
      web.application.endpoint.port=
      # Web Service context; Leave it blank in case you do not have a common context
      web.application.endpoint.context=
    
  2. Write tests cases using JSON

    Writing test for GET end point.
    For all other endpoints tests, please have a look at the section
    Writing All Tests in Action below.

    GET /products

    Open get_api_200.json and update below as per your api functionality.
    For Product API, I have updated below.

    -> name - “get_product_detail”. It’s a step name to tell, what api we are testing.
    -> url - /products
    -> request body - left it blank here for GET request in this case
    -> verify status - It’s a assertion we are doing here. Verifying 200 status code.
    -> verify response body - Asserting that response body should have not-null id.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
           {
             "scenarioName": "Validate the GET api",
             "steps": [
                 {
                     "name": "get_product_details",
                     "url": "/products",
                     "method": "GET",
                     "request": {
                     },
                     "verify": {
                         "status": 200,
                         "body":[
                             {
                                 "id" : "$IS.NOTNULL"
                             }
                         ] 
                     }
                 }
             ]
         }
    
    

And that’s all, Our test case is ready for GET PRODUCT details.

Please just click on below gif to get better view 😎

writingTestsInAction

That’s all! We are just done with writing out tests. It just really few mins!!

Step 3 - Running the tests

Let’s run our tests. We can run test directly from IDE and from command line and from Jenkins jobs. 😎

For integration with CI/CD pipeline, we can just create mvn or gradle tasks for our tests and configure the task in the jenkins pipeline.

Please just click on below gif to get better view 😎

runningTestInAction

Running Individual Test from command line

mvn -Dtest=MyGetApiTest test

Running TestSuite from command line

mvn -Dtest=MyApiSuite test

Creating gradle task

task runProductAPITestSuite ( type : Test ) {
    delete "/target/"
    systemProperty 'zerocode.junit', 'gen-smart-charts-csv-reports'
    include 'com/ns/MyApiSuite.class
}

Running the gradle task - runProductAPITestSuite

gradle runProductAPITestSuite

Test Reports

ZeroCode generates interactive test reports.

Just build the test project and it will run all the tests and create a report folder which will have detailed report.

mvn clean install

Please just click on below gif to get better view 😎

testReports

I hope you like this post. Please do share your comments.

Share on

{Neeraj:Sidhaye}
WRITTEN BY
Neeraj Sidhaye
I Aspire to Inspire before I Expire!