Quick Start

The Ballerina Language has a built-in robust test framework, which allows you to achieve multiple levels of the test pyramid including unit testing, integration testing, and end to end testing. It provides features such as assertions, data providers, mocking, and code coverage, which enable the programmers to write comprehensive tests.

Writing a simple function

To get started, let’s write a simple Ballerina function and test it.

  1. First, let’s create a Ballerina project and add a new module. Use the ballerina new command to create the project. For more information on the command, see Structuring Ballerina Code.

    The standard project will have the structure below.

  2. Now, let’s write the function, which handles sending a get request in the main.bal file of the module you just created.

     // main.bal
     import ballerina/io;
     import ballerina/http;
     import ballerina/stringutils;
     http:Client clientEndpoint = new("https://api.chucknorris.io/jokes/");
     // This function performs a `get` request to the Chuck Norris API and returns a random joke 
     // with the name replaced by the provided name or an error if the API invocation fails.
     function getRandomJoke(string name) returns string|error {
         http:Response|error result = clientEndpoint->get("/random");
         http:Response response = <http:Response>result;
         if (response.statusCode == http:STATUS_OK) {
             json payload = <json>response.getJsonPayload();
             json joke = <json>payload.value;
             string replacedText = stringutils:replace(joke.toJsonString(), "Chuck Norris", name);
             return replacedText;
         } else {
             error err = error("error occurred while sending GET request");
                 ", status code: ", response.statusCode,
                 ", reason: ", response.getJsonPayload());
             return err;
  3. Now, let’s write a simple test case to verify the behavior of the main function in the main_test.bal file.

     // main_test.bal
     import ballerina/io;
     import ballerina/test;
     import ballerina/http;
     // This test function tests the behavior of the `getRandomJoke` when
     // the API returns a successful response.
     @test:Config {}
     function testGetRandomJoke() {
         // Create a default mock HTTP Client and assign it to the `clientEndpoint`
         clientEndpoint = test:mock(http:Client);
         // Stub the behavior of the `get` function to return the specified mock response.
         // Invoke the function to test.
         string result = checkpanic getRandomJoke("Sheldon");
         // Verify the return value.   
         test:assertEquals(result, "When Sheldon wants an egg, he cracks open a chicken.");
     // Returns a mock HTTP response to be used for the jokes API invocation.
     function getMockResponse() returns http:Response {
         http:Response mockResponse = new;
         json mockPayload = {"value":"When Chuck Norris wants an egg, he cracks open a chicken."};
         return mockResponse;
  4. Finally, let’s execute the tests using the following command.

    $ ballerina test --code-coverage --all

    This will print an output similar to the following.

     Compiling source
     Creating balos
     Running Tests with Coverage
     When Sheldon wants an egg, he cracks open a chicken.
         [pass] testGetRandomJoke
         1 passing
         0 failing
         0 skipped
     Generating Test Report
         View the test report at: file:///home/foo/test/sample-project/target/report/index.html

What’s Next?

Now, that you have an understanding of how a test case can be written and executed, you can dive deep into the available features in Writing Tests.