import ballerina/test;
import ballerina/io;
@test:Config{
dataProvider:"ValueProvider"
}
function testAddingValues (string fValue, string sValue, string result) { int value1 = check <int>fValue;
int value2 = check <int>sValue;
int result1 = check <int>result;
io:println("Input params: ["+fValue+","+sValue+","+result+"]");
test:assertEquals(value1 + value2, result1, msg = "The sum is not correct");
}
function ValueProvider() returns (string[][]) {
return [["1", "2", "3"], ["10", "20", "30"], ["5", "6", "11"]];
}
@test:Config{
dataProvider:"jsonDataProvider"
}
function testJsonObjects (json fValue, json sValue, json result) {
json a = {"a": "a"};
json b = {"b": "b"};
json c = {"c": "c"};
test:assertEquals(fValue, a, msg = "json data provider failed");
test:assertEquals(sValue, b, msg = "json data provider failed");
test:assertEquals(result, c, msg = "json data provider failed");
}
function jsonDataProvider() returns (json[][]) {
return [[{"a": "a"}, {"b": "b"}, {"c": "c"}]];
}
Testerina Data ProviderTesterina, provides inbuilt support for data driven tests You can provide a function pointer as a data-provider and iterate the same test over the returned data-set. |
|
import ballerina/test;
import ballerina/io;
|
|
@test:Config{
dataProvider:"ValueProvider"
}
function testAddingValues (string fValue, string sValue, string result) {
|
dataProvider attribute allows you to add a data provider function to the test-case |
int value1 = check <int>fValue;
int value2 = check <int>sValue;
int result1 = check <int>result;
io:println("Input params: ["+fValue+","+sValue+","+result+"]");
test:assertEquals(value1 + value2, result1, msg = "The sum is not correct");
}
|
|
function ValueProvider() returns (string[][]) {
return [["1", "2", "3"], ["10", "20", "30"], ["5", "6", "11"]];
}
|
Data provider function which provides string value-set |
@test:Config{
dataProvider:"jsonDataProvider"
}
function testJsonObjects (json fValue, json sValue, json result) {
json a = {"a": "a"};
json b = {"b": "b"};
json c = {"c": "c"};
test:assertEquals(fValue, a, msg = "json data provider failed");
test:assertEquals(sValue, b, msg = "json data provider failed");
test:assertEquals(result, c, msg = "json data provider failed");
}
|
Providing Json objects as a value set |
function jsonDataProvider() returns (json[][]) {
return [[{"a": "a"}, {"b": "b"}, {"c": "c"}]];
}
|
This function returns a Json value set |
$ ballerina test testerina-data-provider.bal
---------------------------------------------------------------------------
T E S T S
---------------------------------------------------------------------------
---------------------------------------------------------------------------
Running Tests of Package: .
---------------------------------------------------------------------------
Input params: [1,2,3]
Input params: [10,20,30]
Input params: [5,6,11]
|
At the command line, navigate to the directory that contains the
|
Tests run: 4, Passed: 4, Failures: 0, Skipped: 0 - in TestSuite
|
|
---------------------------------------------------------------------------
Results:
|
|
Tests run: 4, Passed: 4, Failures: 0, Skipped: 0
---------------------------------------------------------------------------
Summary:
|
|
................................................................... SUCCESS
---------------------------------------------------------------------------
|
|