import ballerina/io;
import ballerina/test;

// The `dataProvider` attribute is used to specify a data-provider function for the test.
@test:Config { 
    dataProvider: stringDataProvider
}
// Data is passed to the function as function parameters.
function testAddingValues(string fValue, string sValue, string result) returns error? {
    int value1 = check 'int:fromString(fValue);
    int value2 = check 'int:fromString(sValue);
    int result1 = check 'int:fromString(result);
    io:println("Input : [" + fValue + "," + sValue + "," + result + "]");
    test:assertEquals(value1 + value2, result1, msg = "Incorrect Sum");
}

// The data provider function, which returns a `string` value-set.
function stringDataProvider() returns (string[][]) {
    return [["1", "2", "3"], ["10", "20", "30"], ["5", "6", "11"]];
}

Data Driven Tests

The Ballerina Test framework provides in-built support for data-driven tests.

You can specify a function that returns a set of data values as a data-provider to a test function. The test execution will iterate the same test function over the provided dataset.

For more information, see Testing Ballerina Code and the Test Module.

import ballerina/io;
import ballerina/test;
@test:Config { 
    dataProvider: stringDataProvider
}

The dataProvider attribute is used to specify a data-provider function for the test.

function testAddingValues(string fValue, string sValue, string result) returns error? {
    int value1 = check 'int:fromString(fValue);
    int value2 = check 'int:fromString(sValue);
    int result1 = check 'int:fromString(result);
    io:println("Input : [" + fValue + "," + sValue + "," + result + "]");
    test:assertEquals(value1 + value2, result1, msg = "Incorrect Sum");
}

Data is passed to the function as function parameters.

function stringDataProvider() returns (string[][]) {
    return [["1", "2", "3"], ["10", "20", "30"], ["5", "6", "11"]];
}

The data provider function, which returns a string value-set.

bal test test_module
Compiling source
        ballerinatest/test_module:0.1.0
Running tests
        ballerinatest/test_module:0.1.0
Input : [1,2,3]
Input : [10,20,30]
Input : [5,6,11]
        [pass] testAddingValues
        [pass] testAddingValues
        [pass] testAddingValues
        3 passing
        0 failing
        0 skipped