Unit test

Unit testing for Move adds three new annotations to the Move source language:

  • #[test]

  • #[test_only], and

  • #[expected_failure].

They respectively mark a function as a test, mark a module or module member (use, function, or struct) as code to be included for testing only, and mark that a test is expected to fail. These annotations can be placed on a function with any visibility. Whenever a module or module member is annotated as #[test_only] or #[test], it will not be included in the compiled bytecode unless it is compiled for testing.

module my_addrx::Testing
{
    fun is_even(number : u64) : bool
    {
        if(number % 2==0)
        {
            true
        }
        else
        {
            false
        }
    }


    #[test]
    fun testing_is_even()
    {
        let x=is_even(14);
        assert!(x==true,1);

    }
}

Unit tests for a Move package can be run with the aptos move test command.

There are also a number of options that can be passed to the unit testing binary to fine-tune testing and to help debug failing tests. These can be found using the the help flag:

Example:

A simple module using some of the unit testing features is shown in the following example:

Running test:

Last updated