Swan Lake Beta2 is here.
import ballerina/io;
import ballerina/test;

// Executed before the `testFunction` function.
function beforeFunc() {
    io:println("I'm the before function!");
}

// The Test function.
// The `before` and `after` attributes are used to define the functions
// that need to be executed before and after this test function.
@test:Config {
    before: beforeFunc,
    after: afterFunc
}
function testFunction() {
    io:println("I'm in test function!");
    test:assertTrue(true, msg = "Failed!");
}

// Executed after the `testFunction` function.
function afterFunc() {
    io:println("I'm the after function!");
}

Before and After Test

The before attribute allows you to execute a function before a test function. Similarly, the after attribute can be used to execute a function after a test function.

These annotations can be used to set up the prerequisites and post actions for a test case.

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

import ballerina/io;
import ballerina/test;
function beforeFunc() {
    io:println("I'm the before function!");
}

Executed before the testFunction function.

@test:Config {
    before: beforeFunc,
    after: afterFunc
}
function testFunction() {
    io:println("I'm in test function!");
    test:assertTrue(true, msg = "Failed!");
}

The Test function. The before and after attributes are used to define the functions that need to be executed before and after this test function.

function afterFunc() {
    io:println("I'm the after function!");
}

Executed after the testFunction function.

bal test test_module
Compiling source
        ballerinatest/test_module:0.1.0
Running tests
    ballerinatest/test_module:0.1.0
I'm the before function!
I'm in test function!
I'm the after function!
        [pass] testFunction
        1 passing
        0 failing
        0 skipped
import ballerina/io;
import ballerina/test;

// Executed before the `testFunction` function.
function beforeFunc() {
    io:println("I'm the before function!");
}

// The Test function.
// The `before` and `after` attributes are used to define the functions
// that need to be executed before and after this test function.
@test:Config {
    before: beforeFunc,
    after: afterFunc
}
function testFunction() {
    io:println("I'm in test function!");
    test:assertTrue(true, msg = "Failed!");
}

// Executed after the `testFunction` function.
function afterFunc() {
    io:println("I'm the after function!");
}

Before and After Test

The before attribute allows you to execute a function before a test function. Similarly, the after attribute can be used to execute a function after a test function.

These annotations can be used to set up the prerequisites and post actions for a test case.

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

import ballerina/io;
import ballerina/test;
function beforeFunc() {
    io:println("I'm the before function!");
}

Executed before the testFunction function.

@test:Config {
    before: beforeFunc,
    after: afterFunc
}
function testFunction() {
    io:println("I'm in test function!");
    test:assertTrue(true, msg = "Failed!");
}

The Test function. The before and after attributes are used to define the functions that need to be executed before and after this test function.

function afterFunc() {
    io:println("I'm the after function!");
}

Executed after the testFunction function.

bal test test_module
Compiling source
        ballerinatest/test_module:0.1.0
Running tests
    ballerinatest/test_module:0.1.0
I'm the before function!
I'm in test function!
I'm the after function!
        [pass] testFunction
        1 passing
        0 failing
        0 skipped

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand