home / skills / multiversx / mx-ai-skills / mvx_testing_handbook

mvx_testing_handbook skill

/antigravity/skills/mvx_testing_handbook

This skill guides you through MultiversX testing, covering RustVM unit tests, Mandos scenarios, and chain simulation to improve reliability.

npx playbooks add skill multiversx/mx-ai-skills --skill mvx_testing_handbook

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

Files (1)
SKILL.md
3.9 KB
---
name: mvx_testing_handbook
description: Guide to mandos (scenarios), rust-vm unit tests, and chain-simulator.
---

# MultiversX Testing Handbook

This skill provides expert guidance on the 3 layers of MultiversX testing: RustVM Unit Tests, Mandos Integration Tests, and Chain Simulation.

## 1. RustVM Unit Tests
- **Location**: `#[cfg(test)]` modules within contract files or `tests/` directory.
- **Speed**: Instant.
- **Scope**: Internal logic, strict math, private functions.
- **Mocking**: Use `multiversx_sc_scenario::imports::*` to mock the blockchain environment (`blockchain_mock.set_caller(addr)`).

## 2. Mandos Scenarios (`.scen.json`)
- **Location**: `scenarios/` directory.
- **Requirement**: "If it's an endpoint, it MUST have a Mandos test."
- **Execution**:
    - `cargo test-gen` generates Rust wrappers for scenarios.
    - `cargo test` runs them.
- **Key Concepts**:
    - `step: setState`: Initialize accounts and balances.
    - `step: scCall`: Execute endpoint.
    - `step: checkState`: Verify storage matches expected values.

## 3. Chain Simulator (`mx-chain-simulator-go`)
- **Location**: Separate system test suite (often Go or Python).
- **Scope**: Interaction with Node API, complex cross-shard reorgs, off-chain indexing.
- **Tool**: Use `POST /simulator/generate-blocks` to force execution.

## 4. Coverage Strategy
- **Money In/Out**: 100% Mandos coverage required.
- **View Functions**: Unit test coverage sufficient.
- **Access Control**: Explicit negative tests (expect error 4) for unauthorized calls.
- **Endpoint Coverage**: Every `#[endpoint]` must have at least one Mandos scenario.
- **Payable Endpoints**: Every `#[payable]` endpoint must test: correct token, wrong token, zero amount, unauthorized caller.
- **Role-Gated Endpoints**: Every `#[only_owner]` / `#[only_role]` must have a negative test (unauthorized caller → expect error).
- **State Transitions**: State transitions must be verified with `checkState` in Mandos.

## 5. Test Quality Scoring Rubric

Score each category, then average for overall score.

| Category | 0-2 (Poor) | 3-5 (Partial) | 6-8 (Good) | 9-10 (Excellent) |
|----------|-----------|---------------|------------|-------------------|
| Unit Tests | No tests | Some functions tested | Core logic covered | All functions + edge cases |
| Mandos Coverage | No scenarios | Some endpoints covered | All endpoints covered | All endpoints + error paths |
| Access Control Tests | No auth tests | Owner-only tested | All roles tested | All roles + negative tests |
| Money Flow Tests | No payment tests | Happy path only | All payment endpoints | Happy + error + edge amounts |
| Chain Simulator | Not available | Setup exists, not run | Basic flows tested | Cross-shard + reorg tested |

### Scoring Formula
- Start at 5 (baseline for "tests exist and pass").
- +1 for each category rated Good (6-8).
- +2 for each category rated Excellent (9-10).
- -1 for each category rated Poor (0-2).
- -2 if Chain Simulator is not available.
- Cap at 10, floor at 1.

## 6. Output Format

### Test Quality Report
```
Test Execution:
  cargo test: [pass/fail/skip counts]
  Mandos scenarios: [pass/fail counts]
  Chain Simulator: [available: Y/N] [pass/fail if available]

Coverage Assessment:
  Endpoints with Mandos: [N/total] ([%])
  Payable endpoints fully tested: [N/total]
  Access control negative tests: [N/total]
  State transition checks: [N/total]

Category Scores:
  Unit Tests: [0-10]
  Mandos Coverage: [0-10]
  Access Control Tests: [0-10]
  Money Flow Tests: [0-10]
  Chain Simulator: [0-10]

Overall Score: [1-10]

Gaps:
- [list of endpoints without tests]
- [list of missing negative tests]
- [list of untested payment scenarios]
```

## Completion Criteria
Test quality assessment is complete when:
1. All tests have been executed (cargo test + Mandos).
2. Every category in the scoring rubric has been evaluated.
3. Overall score is calculated.
4. Gaps are documented.

Overview

This skill is a practical handbook for testing MultiversX smart contracts across three layers: RustVM unit tests, Mandos integration scenarios, and the chain simulator. It explains what to test where, how to structure scenarios, and how to score test quality to drive measurable improvements.

How this skill works

I describe where each test layer lives and what it inspects: unit tests verify internal logic and edge math inside #[cfg(test)] modules; Mandos scenarios (.scen.json) exercise endpoints and validate storage/state transitions; the chain simulator runs system-level flows including cross-shard and reorg behavior. The guide also gives a repeatable reporting format and a scoring rubric to quantify coverage and gaps.

When to use it

  • When implementing or changing any #[endpoint] to guarantee integration behavior.
  • For payable endpoints to validate token acceptance, wrong token, zero amounts, and unauthorized callers.
  • During development to catch logic regressions quickly with RustVM unit tests.
  • Before release to validate cross-shard interactions, reorgs, and Node API behavior in the chain simulator.
  • To produce a measurable test quality report for audits or review cycles.

Best practices

  • Require a Mandos scenario for every public endpoint; include checkState steps for state transitions.
  • Keep unit tests fast and focused: test pure logic, math edge cases, and private helpers.
  • Write explicit negative tests for access control and expect specific error codes (e.g., error 4 for unauthorized).
  • For payable endpoints, cover correct token, wrong token, zero amount, and unauthorized caller scenarios.
  • Use cargo test-gen to generate Rust wrappers for Mandos and run them via cargo test for CI integration.

Example use cases

  • Verifying a token transfer endpoint: unit tests for internal accounting, Mandos for on-chain flow and checkState.
  • Testing owner-only functions: a Mandos positive case plus a negative caller case expecting error 4.
  • Covering a payable marketplace listing: tests for valid payment, wrong token, zero value, and balance updates.
  • Simulating a cross-shard payout and a reorg using the chain simulator to ensure eventual consistency.
  • Producing a Test Quality Report before an audit to document gaps and assign remediation tasks.

FAQ

Do view functions need Mandos tests?

No. View functions can be covered by unit tests since they read internal state and are fast to run.

What must a Mandos scenario include for an endpoint?

At minimum: setState to initialize accounts, scCall to invoke the endpoint, and checkState to verify expected storage or balances.