How to use Data Providers in Ballerina Testing

Image for post
Image for post

Data Driven Testing

Assume we have a simple backend service written in ballerina to accept doctors’ details and add doctors to the system.

@http:ResourceConfig {methods: ["POST"],path: "/admin/newdoctor"}resource function addNewDoctor(http:Caller caller, http:Request req) {}

How do we test this using data providers?

Data Provider accepts array of arrays or array of tuples. The test case format is as below.

@test:Config {dataProvider: "testAddDoctorDataProvider"}function testAddDoctor(json dataset) {
}
function testAddDoctorDataProvider() returns json[][]{
return [];
}

We need to define a data provider for a test function in test config. Then we need to define the data in its implementation.

Let’s look at the implementation of this.

http:Client healthCareEP = new("http://localhost:9090/healthcare");@test:Config{dataProvider: "testAddDoctorDataProvider"}
function testAddDoctor(json dataset){
http:Request request = new;request.setPayload(dataset);
string expectedResponseMessage = "New Doctor Added Successfully";
http:Response | error response = healthCareEP->post("/admin/newdoctor", request);if (response is http:Response)
{
string | error responsePayload = response.getTextPayload();test:assertEquals(responsePayload, expectedResponse, msg = "Response mismatch!");test:assertEquals(response.statusCode, 200, msg = "Status Code mismatch");}
else
{
test:assertFail(msg = "Error sending request");}}

The following is the data provider implementation.

function testAddDoctorResponseDataProvider() returns json[][] {return [// TC001 — Verify if a doctor can be added to Grand oak community hospital under the category surgery.[{“name”: “T D Uyanage”,“hospital”: “grand oak community hospital”,“category”: “surgery”,“availability”: “Weekends”,“fee”: 2500.0}],// TC002 - Verify if an existing doctor cannot be added.[{"name": "T D Uyanage","hospital": "clemency medical center","category": "surgery","availability": "Weekends","fee": 2500.0}]];}

On above we have two test cases provided.

TC001 — : Verify if a doctor can be added to Grand oak community hospital under the category surgery.

TC002 — : Verify if an existing doctor cannot be added.

Likewise we can provide any number of test data which counts as a test case. First it will assign the array[0] of data to the ‘dataset’ value in function testAddDoctor and run the test case. Next, it will assign the array[1] and re run.

Sending multiple values via Data Providers.

function testAddDoctorResponseDataProvider() returns json[][] {return [
[
{ "name": "T D Uyanage", "hospital": "grand oak community hospital", "category": "surgery", "availability": "Weekends", "fee": 2500.0 }, { "expectedResponse": "New Doctor Added Successfully.", "expectedStatusCode": 200 } ]
];

Then we can define our test function as below.

function testAddDoctor(json dataset, json resultset){}

In the first run json dataset will be assigned with the following.

{"name": "T D Uyanage","hospital": "grand oak community hospital","category": "surgery","availability": "Weekends","fee": 2500.0}

And the json resultset will get assigned with the following.

{"expectedResponse": "New Doctor Added Successfully.","expectedStatusCode": 200}

Written by

Senior Software Engineer — QA at WSO2

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store