Configure tests

The Ballerina Test framework has configurations at various levels to streamline the testing process and ensure that the tests are written with a comprehensible structure.

Set up and tearing down

The following test annotations can be used to set up and tear down the instructions. These configuration annotations enable executing instructions at various levels.

Suite level

The @test:BeforeSuite {} annotation

The function annotated with the BeforeSuite annotation will be run once before any of the tests in the test suite. This can be used for initializing the test suite level pre-requisites.

Example:

The @test:AfterSuite {} annotation

The AfterSuite annotated function will be run once after all the tests in the test suite are run. This can be used for cleaning up the test suite level aspects. A test suite covers tests related to a module.

Example:

Group level

The @test:BeforeGroups {} annotation

For each group specified in this annotation, the BeforeGroups annotated function will be executed once before any of the tests belonging to the group.

Example:

The @test:AfterGroups {} annotation

For each group specified in this annotation, the AfterGroups annotated function will be executed once after all the tests belonging to the group is executed.

Example:

Test case level

The @test:BeforeEach annotation

The BeforeEach annotated function will be run before each test in the test suite. This can be used to initialize the test-level prerequisites repeatedly before every test function.

Example:

The @test:AfterEach annotation

The AfterEach annotated function will be run after each test within the test suite. This can be used to clean up the test-level aspects repeatedly after every test function.

Example:

Each test case

The before attribute of the @test:Config {} annotation

The test config annotation makes use of ‘before’ to denote which function needs to execute before the particular test is run.

Example:

The after attribute of the @test:Config {} annotation

The test config annotation makes use of ‘after’ to denote which function needs to execute after the particular test is run.

Example:

Define test-specific configurations

Configurations for testing can be provided using configurable variables. The values for configurable variables can be provided in a file named Config.toml located in the tests directory, which will only be initialized when the tests are run.

Configurable variables are useful when you require separate configurations that cannot be feasibly used outside of testing. This is particularly useful when testing services and clients where you may need different host values when you are trying to test the service or client.

Define test-only dependencies

Dependencies are meant to be resolved only during testing and can be specified in the Ballerina.toml file by specifying the scope.