Define data-driven tests

The Ballerina Test Framework allows you to specify a function that returns a set of data values as a data-provider.

Use data providers

A data provider is a function, which will be used to provide the data sets for a test function. A data provider function supports one of the following return types.

Map of tuple or error

The key to represent a specific data set can be specified using the key of an entry and data can be specified using the value of an entry. The data provider function can return an error to indicate an issue with the data sets.

Example:

Output:

Compiling source
        ballerina_tests/dataproviders:0.1.0

Running Tests

        dataproviders

		[pass] fruitsDataProviderTest#'banana'
		[pass] fruitsDataProviderTest#'cherry'

		2 passing
		0 failing
		0 skipped

Array of arrays or error

Example:

Output:

Compiling source
        ballerina_tests/dataproviders:0.1.0

Running Tests

        dataproviders

                [pass] stringDataProviderTest#0
                [pass] stringDataProviderTest#1
                [pass] stringDataProviderTest#2

                3 passing
                0 failing
                0 skipped

Execute specific data sets

If you need to run only a specific case from the given data set, you can use the test name with the key to do that. You can make use of wild cards(*) to capture multiple cases as well.

Example:

The following is an example to execute map data sets.

Note: Include the key within double-quotes.

$ bal test --tests fruitsDataProviderTest#"'banana'"

Compiling source
	intg_tests/dataproviders:0.0.0

Running Tests

	dataproviders

		[pass] fruitsDataProviderTest#'banana'

		1 passing
		0 failing
		0 skipped

Example:

The following is an example to execute array data sets.

$ bal test --tests stringDataProviderTest#1

Compiling source
        ballerina_tests/dataproviders:0.1.0

Running Tests

        dataproviders

                [pass] stringDataProviderTest#1
                
                1 passing
                0 failing
                0 skipped