Skip to main content
Testing is essential for building reliable smart contracts. Sui provides powerful testing tools for Move code, including unit tests and scenario-based tests.

Testing Framework Overview

Sui supports two main testing approaches:
  1. Unit tests: Test individual functions in isolation
  2. Scenario tests: Test multi-transaction flows simulating real usage

Writing Unit Tests

Unit tests use the #[test] attribute and run in a simulated environment.

Basic Test Structure

Test-Only Code

Use #[test_only] for code that only exists in tests:

Expected Failures

Test that code fails correctly:

Scenario Testing

Scenario tests simulate multi-transaction flows using test_scenario.

Example from First Package

Based on the actual Sui codebase first_package example:

Testing Module Initialization

From the first_package example:

Testing Shared Objects

Test shared objects using take_shared and return_shared:

Real-World Example: Flash Loan Tests

From examples/move/flash_lender:

Running Tests

Run all tests

Run specific test

Verbose output

With coverage

Filter by module

Testing Best Practices

1. Test Object Lifecycle

Always properly handle objects in tests:

2. Test Error Conditions

3. Use Constants for Test Addresses

4. Test State Transitions

Verify state changes across transactions:

5. Test Multiple Users

Common Test Patterns

Testing with Coins

Testing Events

Debugging Tests

Use debug::print

Check intermediate state

Next Steps