home / skills / raintree-technology / claude-starter / move-testing

move-testing skill

/skills/aptos/move-testing

This skill helps you validate Aptos Move smart contracts with unit, integration, and prover tests, improving reliability and CI/CD coverage.

npx playbooks add skill raintree-technology/claude-starter --skill move-testing

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
4.2 KB
---
name: aptos-move-testing
description: Expert on testing Move smart contracts including unit tests, integration tests, Move Prover formal verification, debugging strategies, test coverage, and CI/CD integration for Aptos development.
allowed-tools: Read, Write, Edit, Grep, Glob, Bash
model: sonnet
license: MIT
metadata:
  author: raintree
  version: "1.0"
---

# Aptos Move Testing Expert

Expert on testing Move smart contracts on Aptos blockchain.

## Triggers

- move test, unit test, integration test
- move prover, formal verification
- debug, coverage, assert, expect
- test failure, debugging

## Test Attributes

```move
#[test]
fun test_basic() { }

#[test(account = @0x1)]
fun test_with_signer(account: &signer) { }

#[test(alice = @0x123, bob = @0x456)]
fun test_multi_signer(alice: &signer, bob: &signer) { }

#[test]
#[expected_failure(abort_code = ERROR_CODE)]
fun test_should_fail() { }

#[test_only]
fun helper_function() { }
```

## Basic Testing Pattern

```move
#[test(account = @0x123)]
fun test_resource_creation(account: &signer) {
    let addr = signer::address_of(account);
    
    // Create resource
    create_resource(account);
    
    // Verify exists
    assert!(exists<MyResource>(addr), 0);
    
    // Verify state
    let resource = borrow_global<MyResource>(addr);
    assert!(resource.value == expected, 1);
}
```

## Test Commands

```bash
# Run all tests
aptos move test

# Run specific test
aptos move test --filter test_name

# With coverage
aptos move test --coverage

# With gas profiling
aptos move test --gas

# Verbose
aptos move test --verbose
```

## Multi-Signer Testing

```move
#[test(alice = @0x123, bob = @0x456)]
fun test_transfer(alice: &signer, bob: &signer) {
    let alice_addr = signer::address_of(alice);
    let bob_addr = signer::address_of(bob);
    
    initialize(alice);
    initialize(bob);
    
    transfer(alice, bob_addr, 100);
    
    assert!(get_balance(alice_addr) == 900, 0);
    assert!(get_balance(bob_addr) == 100, 1);
}
```

## Error Testing

```move
#[test]
#[expected_failure(abort_code = ERROR_INSUFFICIENT_BALANCE)]
fun test_insufficient_balance() {
    transfer(from, to, amount_too_large);
}

#[test]
#[expected_failure]  // Any failure
fun test_any_failure() {
    assert!(false, 0);
}
```

## Test-Only Helpers

```move
#[test_only]
module test_helpers {
    public fun setup_account(account: &signer): address {
        let addr = signer::address_of(account);
        // Setup logic
        addr
    }
}
```

## Debugging

```move
#[test_only]
use std::debug;

#[test]
fun test_with_debug() {
    debug::print(&b"Value:");
    debug::print(&value);
}
```

## Move Prover Specs

```move
spec transfer {
    requires sender_balance >= amount;
    ensures global<Balance>(sender).value == 
            old(global<Balance>(sender).value) - amount;
    aborts_if sender_balance < amount;
}

spec module {
    invariant forall addr: address:
        exists<Balance>(addr) ==> global<Balance>(addr).value >= 0;
}
```

```bash
# Run prover
aptos move prove
aptos move prove --filter MyModule
```

## Coverage Goals

Test coverage should include:
- All public functions
- All abort conditions
- All state transitions
- All access control checks
- Edge cases (zero, max values, empty)

## Common Issues

### Resource Already Exists

```move
// Check existence first
if (!exists<Resource>(addr)) {
    move_to(account, Resource {});
}
```

### Need Signer

```move
// Use test parameter
#[test(account = @0x1)]
fun test_with_signer(account: &signer) { }
```

### Test Timeout

```move
// Reduce iterations
#[test]
fun test_optimized() {
    for (i in 0..100) { }  // Not 1000000
}
```

## CI/CD Integration

```yaml
name: Move Tests
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install Aptos CLI
        run: curl -fsSL "https://aptos.dev/scripts/install_cli.py" | python3
      - name: Run Tests
        run: aptos move test --coverage
      - name: Run Prover
        run: aptos move prove
```

## Best Practices

- Test all public functions
- Test all error conditions
- Use specific abort codes
- Break complex tests into smaller ones
- Use test-only helpers for setup
- Profile gas usage in tests
- Run prover for critical code

Overview

This skill is an expert guide for testing Move smart contracts on Aptos, covering unit and integration tests, Move Prover formal verification, debugging strategies, coverage goals, and CI/CD integration. It provides practical patterns, commands, and troubleshooting tips to build reliable, production-ready Move modules. The content targets developers who need structured testing workflows and automated verification in their Aptos projects.

How this skill works

The skill explains what to inspect in tests: resource existence, state transitions, access control, abort conditions, and edge cases. It shows concrete Move test attributes, multi-signer patterns, expected-failure tests, test-only helpers, debug printing, and how to run tests, coverage, gas profiling, and the Move Prover. It also outlines CI steps to automate tests and prover runs in pipelines.

When to use it

  • When writing unit tests for Move modules that create, borrow, or move resources.
  • When testing multi-signer flows such as transfers or multi-party initialization.
  • When verifying abort conditions and specific error codes with expected_failure tests.
  • When you need formal verification of invariants and pre/post conditions with Move Prover.
  • When adding automated test runs and prover checks to CI/CD pipelines.

Best practices

  • Cover all public functions, abort paths, access controls, and state transitions.
  • Write small, focused tests and use test-only helpers for setup and reuse.
  • Use #[expected_failure(abort_code = ...)] for precise error assertions.
  • Profile gas in tests and keep test loops short to avoid timeouts.
  • Run aptos move prove for critical modules and include it in CI.

Example use cases

  • Unit test for resource creation: assert exists and validate fields after create_resource.
  • Multi-signer transfer test: initialize both signers, execute transfer, assert balances.
  • Error path test: assert transfer aborts with insufficient balance using expected_failure.
  • Gas and coverage check: run aptos move test --gas --coverage to profile and measure coverage.
  • CI workflow: run aptos move test --coverage and aptos move prove in a GitHub Actions job.

FAQ

How do I test functions that require a signer?

Use the #[test(account = @0x1)] attribute to inject a signer parameter into the test function.

When should I use Move Prover versus unit tests?

Use unit tests for behavior and edge cases; use Move Prover to verify invariants, pre/post conditions, and absence of specified aborts for critical logic.