4

I'm a newbie to Ballerina integration language and need a way to write a test case. Following is my initial code.

package samples.foo.bar;

import ballerina.lang.system;

function main (string[] args) {
    int i = addTwoNumbers(1, 2);
    system:println("Result: " + i);
}

function addTwoNumbers(int a, int b) (int) {
    return a + b;
}
Riyafa Abdul Hameed
  • 7,417
  • 6
  • 40
  • 55
Chanuka Ranaba
  • 635
  • 3
  • 12
  • 24

1 Answers1

1

You can use Testerina for this purpose which is the test framework written for Ballerina language. This is shipped by default in ballerina tools distribution. http://ballerinalang.org/downloads/

Writing Test File

Write your test cases as follows in a different file say sample_test.bal

package samples.foo.bar;

import ballerina.test;

function testAddTwoNumbers() {
    test:assertEquals(addTwoNumbers(1,2), 3, "Positive number addition failed");
}

Running tests with Ballerina test command.

./bin/ballerina test <package_path>

Note that this file is located in the same package as your sample.bal file, i.e. ../samples/foo/bar.

You can invoke your test cases as follows. Assuming you are using ballerina tools distribution 0.8.0 and sample.bal, sample_test.bal files are located in ballerina-tools-0.8.0/samples/foo/bar,

./bin/ballerina test samples/foo/bar/

You will get an output as follows as per version 0.8.0.

result: 
tests run: 1, passed: 1, failed: 0

For more available native test functions, please refer Ballerina API Documentation.

  • Also please note the following. 1. One package may contain more than one `*._test.bal` file. (As a best practice name the test files with `_test.bal` suffix) 2. One `*._test.bal` file may contain more than one test function. (Test functions should contain the prefix `test`) 3. Each test function may contain one or more asserts. (If at least one assert fails, whole test function will be marked as failed). Detailed information is shown in the test result summary with failed tests if any. – Suhan Dharmasuriya Feb 21 '17 at 06:07
  • 1
    https://medium.com/@suhanr/testerina-test-framework-built-for-the-ballerina-language-cdb5b061ff6c#.j3q0cqv0t – Suhan Dharmasuriya Feb 22 '17 at 11:30