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

// The `BeforeEach` function is executed before each test function.
@test:BeforeEach
function beforeEachFunc() {
    io:println("I'm the before each function!");
}

// The `AfterEach` function is executed after each test function.
@test:AfterEach
function afterEachFunc() {
    io:println("I'm the after each function!");
}

// A test function.
@test:Config { }
function testFunction1() {
    io:println("I'm in test function 1!");
    test:assertTrue(true, msg = "Failed!");
}

// Another test function.
@test:Config { }
function testFunction2() {
    io:println("I'm in test function 2!");
    test:assertTrue(true, msg = "Failed!");
}

Before and After Each

The function specified with the BeforeEach annotation is executed before every test and the function specified with the AfterEach annotation is executed after every test within the test suite. This can be used for repeatedly initializing and tearing down test level aspects before every test function.

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

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

The BeforeEach function is executed before each test function.

@test:AfterEach
function afterEachFunc() {
    io:println("I'm the after each function!");
}

The AfterEach function is executed after each test function.

@test:Config { }
function testFunction1() {
    io:println("I'm in test function 1!");
    test:assertTrue(true, msg = "Failed!");
}

A test function.

@test:Config { }
function testFunction2() {
    io:println("I'm in test function 2!");
    test:assertTrue(true, msg = "Failed!");
}

Another test 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 each function!
I'm in test function 1!
I'm the after each function!
I'm the before each function!
I'm in test function 2!
I'm the after each function!
                [pass] testFunction1
                [pass] testFunction2
                2 passing
                0 failing
                0 skipped
import ballerina/io;
import ballerina/test;

// The `BeforeEach` function is executed before each test function.
@test:BeforeEach
function beforeEachFunc() {
    io:println("I'm the before each function!");
}

// The `AfterEach` function is executed after each test function.
@test:AfterEach
function afterEachFunc() {
    io:println("I'm the after each function!");
}

// A test function.
@test:Config { }
function testFunction1() {
    io:println("I'm in test function 1!");
    test:assertTrue(true, msg = "Failed!");
}

// Another test function.
@test:Config { }
function testFunction2() {
    io:println("I'm in test function 2!");
    test:assertTrue(true, msg = "Failed!");
}

Before and After Each

The function specified with the BeforeEach annotation is executed before every test and the function specified with the AfterEach annotation is executed after every test within the test suite. This can be used for repeatedly initializing and tearing down test level aspects before every test function.

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

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

The BeforeEach function is executed before each test function.

@test:AfterEach
function afterEachFunc() {
    io:println("I'm the after each function!");
}

The AfterEach function is executed after each test function.

@test:Config { }
function testFunction1() {
    io:println("I'm in test function 1!");
    test:assertTrue(true, msg = "Failed!");
}

A test function.

@test:Config { }
function testFunction2() {
    io:println("I'm in test function 2!");
    test:assertTrue(true, msg = "Failed!");
}

Another test 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 each function!
I'm in test function 1!
I'm the after each function!
I'm the before each function!
I'm in test function 2!
I'm the after each function!
                [pass] testFunction1
                [pass] testFunction2
                2 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