dataProvider/data-driven with RestAssured


Test Class looks like:

package RestAssuredGrp.RestAssuredArtId;
import RestAssuredGrp.RestAssuredArtId.provideData;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.Header;
import io.restassured.http.Headers;
import io.restassured.http.Method;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

public class Currency {
   
    RequestSpecification httpRequest;
   
   
    @BeforeTest
    public void setup()
    {
        RestAssured.baseURI = "http://api.fixer.io";
        httpRequest = RestAssured.given();
    }
   
    @Test(dataProvider="dataFeed",dataProviderClass=provideData.class)
    public void GetCurrencyDetails(String from,String to,Float expectedValue)
    {  

        httpRequest.param("base", from);

        Response response = httpRequest.request(Method.GET, "/latest");


        String responseBody = response.getBody().asString();
        System.out.println("Status code is: "+response.getStatusCode());
   
   
        JsonPath jsonPathEvaluator = response.jsonPath();
        String desiredNode="rates."+to;
        System.out.println(from+" to "+to);
   
        Float rate = jsonPathEvaluator.get(desiredNode);
        System.out.println(rate);
        Assert.assertEquals(rate,expectedValue);
       
       
        Headers allHeaders = response.headers();
        // Iterate over all the Headers
        for(Header header : allHeaders)
        {
            System.out.println("Key: " + header.getName() + " Value: " + header.getValue());
        }


    }

}

Data provider class looks like:

package RestAssuredGrp.RestAssuredArtId;

import org.testng.annotations.DataProvider;

public class provideData {
   
   
      @DataProvider(name = "dataFeed")
   
      public static Object[][] mydataFeed() {
   
            return new Object[][] {
                { "AUD", "INR",50.826f},
                { "USD", "INR",65.383f}
            };
}

}

OUTPUT


Status code is: 200
AUD to INR
50.826
Key: Server Value: nginx/1.13.5
Key: Date Value: Mon, 09 Oct 2017 03:14:31 GMT
Key: Content-Type Value: application/json
Key: Content-Length Value: 450
Key: Connection Value: keep-alive
Key: Cache-Control Value: public, must-revalidate, max-age=900
Key: Last-Modified Value: Fri, 06 Oct 2017 00:00:00 GMT
Key: Vary Value: Origin
Key: X-Content-Type-Options Value: nosniff
Status code is: 200
USD to INR
65.383
Key: Server Value: nginx/1.13.5
Key: Date Value: Mon, 09 Oct 2017 03:14:33 GMT
Key: Content-Type Value: application/json
Key: Content-Length Value: 449
Key: Connection Value: keep-alive
Key: Cache-Control Value: public, must-revalidate, max-age=900
Key: Last-Modified Value: Fri, 06 Oct 2017 00:00:00 GMT
Key: Vary Value: Origin
Key: X-Content-Type-Options Value: nosniff
PASSED: GetCurrencyDetails("AUD", "INR", 50.826)
PASSED: GetCurrencyDetails("USD", "INR", 65.383)

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================

Comments

  1. This post is really very informative. Thanks for sharing such a great knowledge.
    big data

    ReplyDelete

Post a Comment

Popular Posts