Skip to main content
The leo test command discovers and runs test functions annotated with @test in your Leo programs.

Syntax

Arguments

string
default:""
Optional filter to run only tests whose qualified name matches this string.Examples:
  • test_add - Runs tests containing “test_add”
  • my_program.aleo/test_transfer - Runs specific test in program

Options

Network Options

string
Network type: mainnet, testnet, or canary. Defaults to testnet.
string
Network endpoint URL for dependencies.

Build Options

boolean
default:false
Enable offline mode. Prevents network requests.
boolean
default:false
Don’t use the dependency cache.
boolean
default:false
Use network versions of dependencies.
All build options are also supported.

Examples

Run All Tests

Output:

Run Specific Test

Output:

Run Tests Matching Pattern

Runs all tests containing “transfer” in their name.

Writing Tests

Basic Test

Annotate transition functions with @test:
Test functions must be transitions (entry points) to be discovered by leo test.

Test with Expected Failure

Use @should_fail to test error conditions:

Test with Custom Private Key

Specify a private key for the test:

Multiple Tests

Test Output

Passing Tests

Failing Tests

No Tests Found

This occurs when:
  • No functions have the @test annotation
  • The test filter doesn’t match any tests
  • All test functions are not transitions

Test Execution Flow

  1. Build Phase:
    • Compiles program with --build-tests flag
    • Includes test functions in build
  2. Test Discovery:
    • Scans AST for @test annotations
    • Filters by test name if provided
    • Validates tests are transitions
  3. Test Execution:
    • Initializes VM with in-memory ledger
    • Executes each test independently
    • Captures outputs and errors
  4. Result Reporting:
    • Displays pass/fail for each test
    • Shows error messages for failures
    • Reports total pass/fail count

Assertions

Leo provides built-in assertion functions:

assert_eq

Asserts two values are equal:

assert_neq

Asserts two values are not equal:

assert

Asserts a boolean condition:

Testing Best Practices

1. Test Coverage

Test all public transitions:

2. Descriptive Names

Use clear, descriptive test names:

3. Test Edge Cases

4. Independent Tests

Each test should be independent:

5. Test with Realistic Data

Testing with Dependencies

Tests can call functions from dependencies:

Continuous Integration

Integrate leo test in CI/CD pipelines:

Troubleshooting

Test Not Discovered

Ensure:
  1. Function has @test annotation
  2. Function is a transition (not a function)
  3. Test name matches filter (if provided)

Test Fails Unexpectedly

Check:
  1. Function logic is correct
  2. Assertions use correct expected values
  3. No side effects from previous tests

Build Errors in Tests

If tests fail to compile:
Fix any compilation errors before running tests.

Performance

Test execution time depends on:
  • Number of tests
  • Complexity of test functions
  • Number of dependencies
For large test suites, use filtering to run specific tests:

Next Steps