Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

soroban-testkit-assert

Fluent matchers for contract events, authorizations and storage, so assertions read like the behaviour you expect.

Packagesoroban-testkit-assert
Modulesevents · invocation · auth · balance · storage · budget_diff · budget · panic_msg · snapshot
Stylechainable builder

API at a glance

MethodAsserts
EventMatcher::assert_emitted()At least one matching event exists
EventMatcher::assert_not_emitted()No matching event exists
EventMatcher::assert_none_match(pred)No matching event satisfies a predicate
EventMatcher::assert_count(n)Exactly n events were emitted
EventMatcher::assert_count_within(bound)The number of matching events falls within a range (or equals a count)
EventMatcher::assert_topic_count(topic, bound)The number of events carrying topic falls within a range (or equals a count)
assert_event_count_range!(env, 3..=5)Macro form of assert_count_within, over an Env or an EventLog
assert_event_topic_count!(env, "Transfer", 2)Macro form of assert_topic_count, over an Env or an EventLog
EventMatcher::assert_data_matches(pred)One event’s payload satisfies a predicate
EventMatcher::from_contract(addr)Restricts matches to one contract address
EventMatcher::with_topic(t)Restricts matches to events carrying that topic symbol
EventLog::collect()Gathers one invocation’s events into a set spanning calls
EventLog::matcher()A matcher over everything that log collected
assert_event_emitted!(env, contract, topic, { field: value, .. })Asserts at least one matching event was emitted with expected contract, topic(s), and data fields
assert_event_data_schema!(env, topic, { field: Type, .. })Checks matching event maps for required fields, exact keys, and deserializable types
assert_emitted_matches!(env, { .. })Asserts emitted events matching glob wildcards (*, ?) or regex topic patterns and data fields
assert_no_contract_error!(client.try_call(..))Unwraps a successful client call or reports its decoded Soroban error
assert_call_success_with_value!(client.try_call(..), expected)Checks decoded call success and return-value equality
assert_contract_error!(client.try_call(..), MyError::Variant)The call failed with exactly that #[contracterror] variant
assert_host_error!(client.try_call(..), ScErrorType::Auth)The call failed with a host error of that category, category and code, or exact Error
assert_panic_msg!(expr, "text") / contains: / glob: / regex:The expression panicked with a message matching a substring, glob (*, ?) or regular expression, including a contract panic surfaced through a host error
AuthMatcher::assert_no_auth_required()The call needed no authorizations
AuthMatcher::assert_auth_count(n)Exactly n authorizations were recorded
AuthMatcher::assert_matches()One recorded authorization fits the configured invoker, function, and arguments
assert_auth_matches!(env, { .. })The same, with the invoker, function, and argument pattern inline
assert_auth_strict!(env, [ .. ])Every expected authorization matches exactly one record, with no records left over
assert_auth_invoker_distinct!(expected, auths)Verifies that an invocation was authorized exclusively by the declared authorized address
AuthSubTreeMatcher::assert_matches()A matching authorization sub-tree appears at any depth in recorded auths
assert_auth_sub_tree_matches!(env, { .. })Inline sub-tree matcher with argument and child wildcards and side-by-side failure trees
assert_auth_tree!(env, { .. })Validates hierarchical authorization call-trees down to arbitrary depth with visual tree diffs
AuthTreeMockBuilder::new(..)Programmatic builder for mock authorization call-trees
assert_storage_key_deleted!(env, contract, key)A key is gone from instance, persistent and temporary storage
assert_storage_key_deleted!(env, contract, key, tier)A key is gone from one named tier
assert_storage_empty!(env, contract)No live storage entry survives in any tier
assert_storage_empty!(env, contract, tier)A named tier holds no live entry
AuthSubInvocationMatcher::assert_matches()An authorized sub-call matching contract, function, and arguments appears at any depth
assert_auth_sub_invocation!(env, { .. })Inline sub-invocation matcher isolating intermediate contract calls at any depth
StorageQuotaMatcher::assert_matches()Asserts storage tier counts satisfy exact quotas or upper bounds
assert_storage_tier_count!(env, contract, { .. })Macro enforcing allocation quotas across instance, persistent, and temporary tiers
assert_balance_conserved!(..)Asserts total token balance conservation across accounts, fee pools, and reserves
assert_budget_diff_within!(..)Asserts relative gas savings between baseline and optimized closures with side-by-side failure diff table
assert_snapshot_matches!(name, value, env)Serializes contract state, events or return values to canonical JSON and compares with a committed snapshot
SnapshotOptions::new()Exact comparison by default; opt into address normalization and recursive field removal

Event matching

Replace raw tuple iteration with a fluent API:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::events::EventMatcher;

// Assert at least one event was emitted
EventMatcher::new(&env).assert_emitted();

// Assert exact count
EventMatcher::new(&env).assert_count(3);

// Filter by contract and topic
EventMatcher::new(&env)
    .from_contract(&contract_id)
    .with_topic("Transfer")
    .assert_emitted();
}
Scope: the invocation that just ran

EventMatcher::new(&env) reads env.events().all(), and in SDK v28 that returns the events of the most recent contract invocation. Assert right after each call, as the examples here do. To judge several calls as one set, collect them into an EventLog — same filters, same assertions, wider scope.

Reading events in v28

env.events().all() now returns ContractEvents. Call .events() on it to get the slice, and import soroban_sdk::testutils::Events as _ — the method is feature-gated behind testutils. Testkit applies the contract and topic filters for you.

Asserting across several invocations

A multi-step test usually wants a sentence about a whole sequence: “these three calls emitted exactly two Transfer events, and nothing was ever refunded”. EventLog gathers the events of each call the test means to judge, and hands the set to the same matcher:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::events::{EventLog, EventMatcher};

let mut log = EventLog::new(&env);

client.transfer(&from, &to, &100);
log.collect();
client.close_offer(&from);
log.collect();

// Every filter and assertion reads the sequence as one set.
log.matcher().assert_count(2);
log.matcher().with_topic("Transfer").assert_count(1);
log.matcher().from_contract(&contract_id).assert_emitted();
log.matcher().with_topic("Refund").assert_not_emitted();

// The latest call alone still works, unchanged.
EventMatcher::new(&env).with_topic("CloseOffer").assert_count(1);
}

Collection is explicit because the SDK gives a test no per-invocation hook: a call nobody collected from adds nothing to the log, and a call that published nothing adds nothing either. That is what lets an assertion over a log mean “not anywhere in this test” rather than “not in the last call”.

MethodGives
EventLog::new(&env)An empty log
log.collect()Appends the events of the invocation that just ran
log.matcher()An EventMatcher over everything collected so far
log.topics()Vec<Vec<String>> — one entry per event, its topic symbols
log.events()The collected ContractEvent values
log.len() / log.is_empty()How much has been gathered

Events stay in call order — one contiguous run per collected invocation, in the order the contract published them — so topics() is what a sequence assertion reads:

#![allow(unused)]
fn main() {
let sequence = log
    .topics()
    .iter()
    .map(|topics| topics.join("."))
    .collect::<Vec<_>>()
    .join("|");
assert_eq!(sequence, "Transfer|CloseOffer");
}

A failure over a log reports the aggregate, which is the count of calls the assertion actually saw:

assertion `left == right` failed: Expected 3 events, found 2
panicked at 'Expected an event whose data matches, checked 2 event(s) with data [1, 2]'

Negative assertions

Proving something did not happen is the other half of event testing — a rejected transfer should emit no Transfer event, a closed offer should publish nothing at all:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::events::EventMatcher;

// Nothing in scope was published
EventMatcher::new(&env).assert_not_emitted();

// A specific topic stayed silent
EventMatcher::new(&env)
    .from_contract(&contract_id)
    .with_topic("Transfer")
    .assert_not_emitted();

// Anything the topic filter cannot express
EventMatcher::new(&env).assert_none_match(|event| {
    matches!(event.type_, soroban_sdk::xdr::ContractEventType::Diagnostic)
});
}

assert_none_match hands you the raw ContractEvent, so a predicate can inspect the event type or the data payload rather than only the topics.

Both failures quote the event they were not supposed to find:

panicked at 'Expected no events to be emitted, found 1 — unexpected event: topics [Transfer], type Contract'
A negative assertion reads its matcher's scope

EventMatcher::new(&env).assert_not_emitted() proves the most recent invocation published nothing matching. Proving a topic never appeared across a test is the EventLog case: collect each call, then log.matcher().with_topic("Refund").assert_not_emitted().

Event count ranges and topic frequencies

An exact assert_count(n) breaks the day a batch call starts emitting one extra fee or audit event, even though the behaviour under test did not change. Two macros state what the test actually depends on instead: that the total falls in a range, and that a given topic appeared a given number of times.

#![allow(unused)]
fn main() {
use soroban_testkit_assert::{assert_event_count_range, assert_event_topic_count};

client.batch_transfer(&from, &recipients); // 2 transfers + auxiliary events

// Total events within a range: any standard range works.
assert_event_count_range!(env, 3..=5);
assert_event_count_range!(env, 2..);
assert_event_count_range!(env, ..=10);

// An exact topic frequency, or a range of them.
assert_event_topic_count!(env, "transfer", 2);
assert_event_topic_count!(env, "fee", 0..=1);
assert_event_topic_count!(env, "refund", 0);

// Restrict either one to the events of one contract.
assert_event_count_range!(env, from = token_id, 3..);
assert_event_topic_count!(env, from = token_id, "transfer", 2);
}

The bound is an exact count (2) or any range over integers: a..b, a..=b, a.., ..b, ..=b, or ... Integer literals need no suffix. A topic counts the same way with_topic matches: an event counts once when any of its topics is that symbol, and symbols are case sensitive.

The first argument is anything that implements EventSource: an Env (the most recent invocation) or an EventLog (every call it collected), by value or by reference. That makes the macros the natural way to bound a whole multi-step test:

#![allow(unused)]
fn main() {
let mut log = EventLog::new(&env);
for batch in batches {
    client.batch_transfer(&from, &batch);
    log.collect();
}
assert_event_count_range!(log, 6..=12);
assert_event_topic_count!(log, "transfer", 6);
}

Both checks also exist as matcher methods, which compose with the existing filters:

#![allow(unused)]
fn main() {
EventMatcher::new(&env)
    .from_contract(&token_id)
    .with_topic("transfer")
    .assert_count_within(2..=3);

EventMatcher::new(&env).assert_topic_count("fee", 1);
}

Failure diagnostics

A failure prints the bound, the observed count, the scope that was read, and a table of every topic signature in scope with how many events carried it, so you see what was emitted instead. Symbol topics are spelled out. Every other topic shows as its type (<address>, <i128>, …), so transfers to different recipients share one row:

Event topic count mismatch: expected topic "Transfer" exactly 2 time(s), observed 0 (topic "Transfer" was not observed)
Scope: most recent invocation
| Observed topics     | Count |
|---------------------|-------|
| transfer, <address> |     2 |
| fee                 |     1 |
| audit               |     1 |
| total               |     4 |

The table lists signatures in the order they first appeared. It honours from = … / from_contract but deliberately ignores the topic being counted, because the useful question on failure is “what did come out of the call?”.

Asserting on payload data

Topics say which event fired; the data says what happened. assert_data_matches hands the predicate one EventData per event in scope, after the contract and topic filters:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::events::EventMatcher;

// A single-value payload, read as the type it is
EventMatcher::new(&env)
    .with_topic("transfer")
    .assert_data_matches(|data| data.deserialize::<i128>() == Some(1_000));

// A struct published with `contractevent` arrives as a map keyed by field name
EventMatcher::new(&env)
    .from_contract(&contract_id)
    .with_topic("incremented")
    .assert_data_matches(|data| {
        data.field("new_count")
            .and_then(|value| value.deserialize::<u32>())
            == Some(12)
    });

// A `contracttype` struct deserializes whole, when a test wants all of it
EventMatcher::new(&env)
    .with_topic("moved")
    .assert_data_matches(|data| data.deserialize::<Moved>() == Some(expected));

// Anything the typed views cannot express: the payload as the ledger stored it
EventMatcher::new(&env)
    .with_topic("flag")
    .assert_data_matches(|data| matches!(data.raw(), soroban_sdk::xdr::ScVal::Bool(true)));
}
ViewReturnsUse it when
deserialize::<T>()Option<T>the payload is one value of a known type — a number, Address, Vec/Map, or a contracttype struct
field("name")Option<EventData>the payload is a map, which is what a contractevent struct becomes, and one field is the point
fields()Vec<String>the payload is a map and you want to enumerate the field names in ledger order
raw()&ScValneither fits, or the test is about the shape itself

deserialize yields None rather than panicking, so one predicate can probe a payload without assuming it. field composes with it: data.field("amount").and_then(|value| value.deserialize::<i128>()).

fields() returns the field names actually present on the ledger in order:

#![allow(unused)]
fn main() {
EventMatcher::new(&env)
    .with_topic("moved")
    .assert_data_matches(|data| data.fields() == ["amount", "to"]);
}
A contractevent struct is not a contracttype struct

The struct declared with contractevent gets an Event implementation, not ledger conversions, so deserialize::<Incremented>() will not compile against it. Read its fields with field. Note too that the SDK publishes event maps sparse by default: a field whose value is None is absent from the map rather than stored as void, so field returning None covers both "no such field" and "field left empty".

A failed data assertion prints the payloads it was handed, which is the difference between a message you can read and a re-run:

panicked at 'Expected an event whose data matches, checked 4 event(s) with data [7, 9000, true, "widget"]'
panicked at 'Expected an event whose data matches, checked 1 event(s) with data [{"amount": 500, "to": Contract(CAAAAAA...FCT4)}]'

Scalars print their values, maps and vectors print their entries, an address prints as its strkey (abbreviated above, printed whole in a real message), and a shape too rare to spell out falls back to its XDR debug form, so the message never hides the value it rejected. When no event is in scope the message says so instead of claiming the data was wrong.

Checking an event payload schema

assert_event_data_schema! validates the complete map payload of every event matching the topic. It reports missing or mistyped fields individually and rejects extra keys, which helps catch accidental indexer schema changes:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::assert_event_data_schema;
use soroban_sdk::Address;

client.transfer(&from, &to, &100);

assert_event_data_schema!(&env, "transfer", {
    amount: i128,
    from: Address,
    to: Address,
});
}

Types are checked by Soroban value deserialization, including i128, u64, Address, Symbol, and bool. A failure names each offending field and shows the expected type and observed type/value. The assertion requires at least one event matching the topic.

Declarative event assertion with assert_event_emitted!

assert_event_emitted! provides ergonomic declarative assertions for contract events. It filters by contract ID, topic name(s), and data payload fields, asserting that at least one matching event was published:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::assert_event_emitted;

client.increment(&caller, &5);

// Assert by contract, topic, and struct/map fields:
assert_event_emitted!(&env, &contract_id, "incremented", {
    caller: caller,
    new_count: 5u32,
});

// Assert with multiple topics:
assert_event_emitted!(&env, &contract_id, ["counter", "incremented"], {
    new_count: 5u32,
});

// Assert by contract and topic only:
assert_event_emitted!(&env, &contract_id, "incremented");
}
Scope: the invocation that just ran

Like EventMatcher::new(&env), assert_event_emitted! inspects env.events().all(), which in Soroban SDK v28 returns events from the most recent contract invocation only. Invoke the assertion immediately after the contract call that should emit the event.

When multiple events match the filters, the assertion succeeds (at least one match). If no event matches, it panics with a detailed diagnostic message displaying what was expected versus all events emitted during the invocation:

panicked at 'assert_event_emitted! failed: no matching event found.
Expected event:
  contract: Contract(CAAAAA...)
  topic(s): ["incremented"]
  data: { new_count: 5u32 }
Emitted events (1 in scope):
  [1] contract: Contract(CAAAAA...), topics: [counter, incremented], data: {"caller": Contract(...), "new_count": 10}'

Regex and wildcard topic matching (assert_emitted_matches!)

When contract events contain prefixes, variable identifiers, or structured multi-part topics (e.g. Transfer:XLM, Order:Created:104), exact string topic matching can be brittle. assert_emitted_matches! allows matching topics using wildcard glob patterns (*, ?) and regular expressions, while validating data payload fields in the same assertion:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::assert_emitted_matches;

// Match with wildcard globs across single or multi-part topics
assert_emitted_matches!(env, {
    contract: dex_id,
    topic: "Trade:*",
    data: { amount: 500u64 },
});

// Match with regular expression pattern
assert_emitted_matches!(env, {
    contract: dex_id,
    topic: r"^Order:(Filled|Cancelled)$",
});

// Works across EventLog collections spanning multiple invocations
assert_emitted_matches!(event_log, {
    topic: "Trade:XLM",
});
}

On assertion failure, assert_emitted_matches! reports every recorded event with its emitting contract, formatted topics, and raw payload data.

Successful contract calls

Use the generated client’s try_* call with these macros instead of a bare unwrap(). A host failure is decoded into its category, code, and known name (for example storage/3: MissingValue); contract-specific errors retain their code and are identified as unregistered unless decoded separately:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::{
    assert_call_success_with_value, assert_no_contract_error,
};

// Assert success and keep the returned value for further checks.
let balance = assert_no_contract_error!(client.try_balance(&account));
assert!(balance > 0);

// Assert that a successful invocation returned exactly the expected value.
assert_call_success_with_value!(client.try_transfer(&from, &to, &100), ());
}

Both macros evaluate the call once and distinguish a returned contract/host error from an invocation failure. assert_no_contract_error! returns the successful value; assert_call_success_with_value! compares it with the expected value.

Asserting exact contract and host errors

#[should_panic] passes for any panic, so a test written to prove “unauthorized callers are refused” keeps passing when the call starts failing on a budget overflow instead. assert_contract_error! takes the result of a try_* client call and fails unless it carries exactly the expected variant:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::assert_contract_error;

assert_contract_error!(
    client.try_transfer(&sender, &receiver, &1000),
    TokenError::InsufficientBalance
);
}

It handles every arm of the SDK v28 try_* shape, Result<Result<T, ConversionError>, Result<E, InvokeError>>:

The call returnedThe macro
Err(Ok(e)) with e == expectedpasses
Err(Ok(e)) with another variantfails, naming both variants with code and category
Err(Err(InvokeError::Contract(code)))fails: a code the client’s error enum has no variant for
Err(Err(InvokeError::Abort))fails: a panic or host failure
Ok(..)fails: the call unexpectedly succeeded, printing the returned value
assert_contract_error! failed: the call failed with a different error
  expected: InsufficientBalance (code 1, category contract)
  received: Unauthorized (code 2, category contract)
  decoded:  Soroban Error [2]: unregistered contract error — add it to an ErrorRegistry to read it as text

The expected value can be the contract’s error enum or a raw soroban_sdk::Error::from_contract_error(code); codes and categories come from DecodedError in soroban-testkit-core.

Host errors

assert_host_error! matches a host error at the precision you need:

#![allow(unused)]
fn main() {
use soroban_sdk::xdr::{ScErrorCode, ScErrorType};
use soroban_testkit_assert::assert_host_error;

assert_host_error!(outcome, ScErrorType::Storage);                             // any storage error
assert_host_error!(outcome, ScErrorType::Storage, ScErrorCode::MissingValue);  // exact category and code
assert_host_error!(outcome, Error::from_contract_error(101));                  // an exact Error value
}

The last form is how to assert an error code that propagated from a different contract — a token’s InsufficientBalance surfacing through a pool whose client only knows AmmError.

Three things the host decides, not the macro:

  • A #[contracterror] client can only carry contract codes; a host failure arrives as InvokeError::Abort with its category lost. The diagnostic says so — call env.try_invoke_contract::<_, soroban_sdk::Error>(..) to keep it.
  • A host failure inside a native (non-Wasm) test contract surfaces as context/InvalidAction: the host catches it as a failed contract frame.
  • Budget exhaustion is unrecoverable and escalates to a panic even through try_*; catch it with #[should_panic(expected = "Error(Budget, ExceededLimit)")].

check_contract_error and check_host_error return the diagnostic as a Result<(), String> instead of panicking.

Asserting the error code behind a panic

A client method that is not a try_* call panics when the invocation fails, and the host renders the failure inside the panic text:

HostError: Error(Contract, #101)
HostError: Error(Auth, InvalidAction)

#[should_panic(expected = "...")] only checks that the text contains a substring. assert_contract_call_panics_with_error_code! catches the panic, parses the packed Error(..), and compares the decoded error with an exact expectation:

#![allow(unused)]
fn main() {
use soroban_sdk::xdr::{ScErrorCode, ScErrorType};
use soroban_testkit_assert::assert_contract_call_panics_with_error_code;

// A numeric #[contracterror] code.
assert_contract_call_panics_with_error_code!(client.withdraw(&1_000), VaultError::Locked as u32);

// An exact host error category and code.
assert_contract_call_panics_with_error_code!(
    client.withdraw(&1_000),
    ScErrorType::Auth,
    ScErrorCode::InvalidAction
);
}

The call is evaluated once, inside catch_unwind. The assertion fails cleanly when the call returns normally, when the panic carries no Soroban error (the diagnostic quotes the panic), and when the code or category differs. A mismatch prints both errors with their decoded category, numeric code and variant name:

assert_contract_call_panics_with_error_code! failed: the call panicked with a different error
  expected: Error(Contract, #101) — decoded: code 101, category contract
  received: Error(Contract, #2) — decoded: code 2, category contract, variant contract code 2
  panic:    HostError: Error(Contract, #2)

check_panic_error_code returns the diagnostic as a Result<(), String> without panicking, for tests that assert on the message itself.

Matching a panic message (assert_panic_msg!)

#[should_panic(expected = "...")] applies to the whole test, passes for a panic raised anywhere in it, and only does a substring match. assert_panic_msg! scopes the expectation to a single expression, runs that expression exactly once inside catch_unwind, and matches the message it panicked with by substring, glob, or regular expression. On a match it evaluates to a CapturedPanic you can inspect further.

Matching modes

The mode is chosen by a keyword. The macro never guesses it from what the pattern contains, so "a*b" with no keyword is a literal substring:

FormModeRule
assert_panic_msg!(expr, "text")substringThe message contains text (same rule as #[should_panic(expected)])
assert_panic_msg!(expr, contains: "text")substringThe same rule, written out
assert_panic_msg!(expr, glob: "pat")globThe whole message matches; * is any run of characters (newlines included), ? exactly one character
assert_panic_msg!(expr, regex: r"pat")regexregex crate syntax; unanchored unless the pattern uses ^/$

The pattern can be any expression that gives a &str or String, so format! with runtime values works. A closure written in place (|| expr or move || expr) is called once, not just constructed.

#![allow(unused)]
fn main() {
use soroban_testkit_assert::assert_panic_msg;

fn withdraw(amount: u64) {
    assert!(amount <= 100, "withdrawal of {amount} exceeds the limit of 100");
}

// Substring
assert_panic_msg!(withdraw(250), "exceeds the limit");
assert_panic_msg!(withdraw(250), contains: "exceeds the limit");

// Glob: the whole message, with * and ?
assert_panic_msg!(withdraw(250), glob: "withdrawal of * exceeds the limit of ???");

// Regex
assert_panic_msg!(withdraw(250), regex: r"^withdrawal of \d+ exceeds the limit of \d+$");

// Dynamic values in the pattern, and inspecting what was caught
let amount = 250;
let captured = assert_panic_msg!(withdraw(amount), contains: format!("withdrawal of {amount}"));
assert_eq!(captured.message(), "withdrawal of 250 exceeds the limit of 100");
}

Panics from panic! and from failed assert!, assert_eq! and unwrap all work. A bare assert!(ready) or panic!("literal") carries a &'static str payload and a formatted message a String; both are read.

Contract panics and host diagnostics

A contract called through its generated client does not unwind with the contract’s own payload. The test host catches the contract’s panic, turns it into a host error, and re-panics with a String like this:

HostError: Error(WasmVm, InvalidAction)

Event log (newest first):
   ...
   3: [Failed Diagnostic Event (not emitted)] contract:CA..., topics:[log], data:["caught panic 'withdrawal of 250 exceeds the limit of 100' from contract function 'Symbol(withdraw)'", 250]
   ...

CapturedPanic splits that text into parts. host_error holds the Error(..) from the first line, and contract_panics holds the message from each caught panic '..' entry. A pattern passes if it matches any of the candidates below, tried in this order:

  1. each contract panic message;
  2. the host error (Error(WasmVm, InvalidAction), Error(Contract, #7), …);
  3. the HostError: ... header line;
  4. the full raw panic text.

That is how a whole-message glob or anchored regex can match the contract’s own words even though the raw payload also carries the event log:

#![allow(unused)]
fn main() {
let captured = assert_panic_msg!(
    client.withdraw(&250),
    glob: "withdrawal of * exceeds the limit of 100"
);
assert_eq!(captured.host_error.as_deref(), Some("Error(WasmVm, InvalidAction)"));

// An error raised with panic_with_error! carries a code, not text:
assert_panic_msg!(client.locked(), glob: "Error(Contract, #7)");
}

For a Rust panic raised by test-side code, the raw message is the only candidate.

Failure diagnostics

A failure says which of four things went wrong.

The invocation did not panic:

assert_panic_msg! failed: the invocation completed without panicking
  expected a panic matching (contains): "exceeds"

No candidate matched. The failure prints the expected pattern, its mode, and the actual message. For a host panic it also prints the host error and each contract panic message:

assert_panic_msg! failed: the panic message did not match
  expected (glob): "deposit of *"
  origin: Soroban host error
  host error: Error(WasmVm, InvalidAction)
  contract panic message: "withdrawal of 250 exceeds the limit of 100"
  actual (full host panic):
    | HostError: Error(WasmVm, InvalidAction)
    | ...

The payload has no text. This happens with std::panic::panic_any(42u32) and similar. The failure reports the payload as having no message instead of matching the pattern against an empty string.

The regex pattern is invalid. The failure prints the compile error, and the invocation is never run.

check_panic_msg(capture_panic(|| ...), &PanicPattern::glob("...")) gives you the same result as a Result<CapturedPanic, PanicMsgFailure> without panicking. PanicPattern::matches_text and panic_msg::glob_match let you test the matching on its own.

Limitations

  • The contract’s own panic text is only available from the host’s diagnostic event log. Env::default() in testutils records that log. If diagnostics are off, only the host error is available, and a mismatch reports contract panic message: unavailable instead of matching against empty text. The crate’s tests cover this case.
  • panic_with_error! and #[contracterror] returns carry a numeric code, not text. Match them by their Error(Contract, #N) rendering, or use assert_contract_call_panics_with_error_code! to compare the decoded code.
  • The text is matched exactly as the host renders it in the event log. A contract message containing quotes or control characters may appear escaped.
  • Only the native test host has been exercised. A Wasm contract built with panic = "abort" traps without a message, so all you get there is the host error.
  • The default panic hook still prints the caught panic to stderr. The macro does not replace the process-wide hook, because doing that is not safe while tests run in parallel.

Authorization matching

Verify who had to sign for a call, and for what. A recorded authorization carries the invoker, the contract function, and the arguments require_auth demanded, and the macro names all three:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::assert_auth_matches;

client.increment(&caller, &1);

// The invoker and the function are enough when the arguments are not the point.
assert_auth_matches!(env, {
    invoker: caller,
    function: "increment",
});

// Arguments are matched position by position; `_` accepts any value.
assert_auth_matches!(env, {
    invoker: caller,
    function: "increment",
    args: [_, 1u32],
});
}
Scope: the invocation that just ran

assert_auth_matches! reads env.auths(), which in SDK v28 reports the authorizations of the most recent contract invocation — the same scope as EventMatcher::new. Assert right after the call, or provide the expected auth with env.mock_auths(..) / env.set_auths(..) and let the call run with real requirements instead of mock_all_auths().

The matcher behind the macro is available directly when a test builds the filter in steps:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::auth::{AuthArgs, AuthMatcher};

AuthMatcher::new(&env)
    .invoker(&admin)
    .function("set_admin")
    .assert_matches();

AuthMatcher::new(&env)
    .args(AuthArgs::new().any().value(&env, 7u32))
    .assert_matches();
}

And the coarser questions still have their own assertions:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::auth::AuthMatcher;

// Assert no auth was required
AuthMatcher::new(&env).assert_no_auth_required();

// Assert specific auth count
AuthMatcher::new(&env).assert_auth_count(2);
}

A failed match prints the expectation next to every authorization the invocation recorded, so the wrong invoker, function, or argument is visible without a re-run:

panicked at 'Expected an authorization matching { invoker Contract(C…FCT4), function "set_admin", args [_, 7] }, found 1 recorded authorization(s):
  [0] invoker Contract(C…FCT4) contract Contract(C…D2KM) function "increment" args [Contract(C…FCT4), 1]'

Strict authorization matching

Use assert_auth_strict! when a test must prove that a call authorized only the expected invokers and functions. Every listed pattern must match a distinct recorded authorization, and every record must match one listed pattern. The macro also accepts argument patterns, where _ matches any value in that position:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::assert_auth_strict;

client.set_admin(&admin, &new_admin);

assert_auth_strict!(env, [
    {
        invoker: admin,
        function: "set_admin",
        args: [new_admin],
    },
]);
}

Declare multiple authorizations as separate entries. An empty list asserts that the invocation recorded no authorizations:

#![allow(unused)]
fn main() {
assert_auth_strict!(env, [
    { invoker: first, function: "joint", args: [_, second] },
    { invoker: second, function: "joint", args: [first, _] },
]);

assert_auth_strict!(env, []);
}

If a pattern is missing or any unlisted authorization was recorded, the failure shows expected and observed authorizations side by side, then identifies missing patterns and extra records.

Exact multiset matching

assert_auth_exact_multiset! is the order-insensitive form of the same assertion. A contract that triggers several independent require_auth demands can record them in any order, and the order is not part of the SDK’s contract:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::assert_auth_exact_multiset;

client.settle(&first, &second);

assert_auth_exact_multiset!(env, [
    { invoker: second, function: "settle", args: [first, _] },
    { invoker: first, function: "settle", args: [_, second] },
]);
}

Matching is one-to-one, so this is a multiset rather than a subset rule:

  • every expected pattern must match a distinct recorded authorization;
  • every recorded authorization must be consumed by an expected pattern;
  • two identical patterns require two identical records, and an empty list asserts that nothing was authorized.

Missing and surplus authorizations both fail, with the side-by-side table followed by the unmatched expectations and the extra records.

Sub-tree authorization matching

Multi-hop routing protocols (such as aggregator -> pool -> vault) record nested AuthorizedInvocation trees in env.auths(). assert_auth_sub_tree_matches! locates and validates a sub-tree starting at any depth, so a modular test can verify an intermediate contract hop without repeating the entire outer call stack:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::assert_auth_sub_tree_matches;

aggregator.swap(&user, &pool_id, &vault_id, &500i128);

// Matches Pool -> Vault inside Aggregator -> Pool -> Vault without naming Aggregator.
assert_auth_sub_tree_matches!(env, {
    invoker: user,
    contract: pool_id,
    function: "route",
    args: [_, vault_id.clone(), 500i128],
    sub_invocations: [
        {
            contract: vault_id,
            function: "deposit",
            args: [user.clone(), 500i128],
            sub_invocations: [],
        },
    ],
});
}

Every field (invoker, contract, function, args, sub_invocations) is optional and accepts _ as a wildcard. Inside sub_invocations: [..]:

PatternMatches
Omitted or sub_invocations: _Any child invocations (zero or more)
sub_invocations: []Exactly zero child invocations (leaf node)
_Any single child invocation at that position
..Zero or more child invocations at that position
{ ... }A nested sub-tree node with its own filters and sub_invocations

The same matching is available through the chainable builder types AuthSubTree, SubInvocations, and AuthSubTreeMatcher:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::auth::{AuthArgs, AuthSubTree, AuthSubTreeMatcher, SubInvocations};

let expected = AuthSubTree::new()
    .contract(&pool_id)
    .function("route_with_fee")
    .sub_invocations(
        SubInvocations::new()
            .node(
                AuthSubTree::new()
                    .contract(&vault_id)
                    .function("deposit")
                    .args(AuthArgs::new().any().value(&env, 1_000i128)),
            )
            .any_many(),
    );

AuthSubTreeMatcher::new(&env)
    .invoker(&user)
    .sub_tree(expected)
    .assert_matches();
}

When a sub-tree fails to match, the assertion renders a side-by-side tree comparison highlighting missing branches with [MISSING]:

Expected authorization sub-tree did not match any recorded authorization (1 recorded):

Expected sub-tree                                            | Recorded authorization tree(s)
-------------------------------------------------------------+-------------------------------------------------------------
invoker Contract(C…1234)                                     | [0] invoker Contract(C…1234)
contract Contract(C…BBBB) function "route"                   | contract Contract(C…AAAA) function "swap" args [...]
├── contract Contract(C…CCCC) function "deposit"             | └── contract Contract(C…BBBB) function "route" args [...]
└── contract Contract(C…DDDD) function "collect" [MISSING]   |     └── contract Contract(C…CCCC) function "deposit" args [...]

Hierarchical call-tree validation and mock builder (assert_auth_tree!)

In nested multi-contract calls (e.g. Router -> Pool -> Vault -> Token), authorizations form recursive trees of AuthorizedInvocation structures. assert_auth_tree! validates an entire nested delegation hierarchy to arbitrary depth using concise sub_invokes lists:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::assert_auth_tree;

assert_auth_tree!(env, {
    invoker: user,
    function: "deposit_and_stake",
    sub_invokes: [
        {
            invoker: router,
            function: "transfer",
            args: [user, pool, 1000],
            sub_invokes: [
                { invoker: pool, function: "mint_receipt", args: [user, 1000] }
            ]
        }
    ]
});
}

To construct mock authorization trees programmatically in tests or mocks, use AuthTreeMockBuilder:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::{auth_tree::tree_depth, AuthTreeMockBuilder};

let mock_invocation = AuthTreeMockBuilder::new(router, "transfer")
    .arg(&user, &env)
    .arg(&pool, &env)
    .arg(&1000i128, &env)
    .sub_invoke(
        AuthTreeMockBuilder::new(pool, "mint_receipt")
            .arg(&user, &env)
            .arg(&1000i128, &env)
            .build(&env)
    )
    .build(&env);

assert_eq!(tree_depth(&mock_invocation), 2);
}

Isolating intermediate sub-invocations

When inspecting authorization trees from complex multi-contract calls, developers often only care about asserting that a specific intermediate contract properly authorized a sub-call, regardless of top-level invoker details.

assert_auth_sub_invocation! searches recursively across all recorded authorization trees and matches any sub-invocation matching { contract, function, args } at any depth:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::assert_auth_sub_invocation;

aggregator.swap(&user, &pool_id, &vault_id, &500i128);

// Matches Vault deposit at depth 2 without naming Aggregator or Pool:
assert_auth_sub_invocation!(env, {
    contract: vault_id,
    function: "deposit",
    args: [user.clone(), 500i128],
});

// Matches intermediate Pool route without top-level wrapper details:
assert_auth_sub_invocation!(env, {
    contract: pool_id,
    function: "route",
    args: [_, vault_id, 500i128],
});
}

All fields are optional and accept _ as a wildcard:

  • contract: addr (or _ / omitted): filters calls targeting addr.
  • function: "name" (or _ / omitted): filters calls named "name".
  • args: [_, val] (or _ / omitted): matches arguments positionally with _ wildcards.
  • invoker: addr (or _ / omitted): restricts matches to authorization trees originating from addr.

The same matching is available via the chainable builder AuthSubInvocationMatcher:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::auth::{AuthArgs, AuthSubInvocationMatcher};

AuthSubInvocationMatcher::new(&env)
    .contract(&vault_id)
    .function("deposit")
    .args(AuthArgs::new().any().value(&env, 500i128))
    .assert_matches();
}

When no matching sub-invocation is found, the assertion formats the full recorded authorization tree displaying all sub-invocations:

panicked at 'Expected authorization sub-invocation matching { contract Contract(C…), function "deposit", args [_, 999] }, found no match in 1 recorded authorization tree(s):
[0] invoker Contract(C…1234)
contract Contract(C…AAAA) function "swap" args [...]
└── contract Contract(C…BBBB) function "route" args [...]
    └── contract Contract(C…CCCC) function "deposit" args [...]'

Storage assertions

Closing an account, burning a balance or cancelling an escrow should leave no storage behind. assert_storage_key_deleted! and assert_storage_empty! prove entries are gone from the ledger rather than merely rewritten:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::{assert_storage_empty, assert_storage_key_deleted};
use soroban_testkit_assert::storage::StorageTier;

// The key no longer exists in any tier.
assert_storage_key_deleted!(&env, &token, "balance");

// Only the named tier is inspected; another tier may still hold the key.
assert_storage_key_deleted!(&env, &token, "allowance", StorageTier::Persistent);

// The contract's whole storage is empty, or one tier of it is.
assert_storage_empty!(&env, &token);
assert_storage_empty!(&env, &token, StorageTier::Temporary);
}

Both assertions inspect instance, persistent and temporary storage through StorageSnapshot, with instance storage unfolded per key exactly as assert_storage_tier! reports it. The key is the snapshot’s rendered form: a symbol’s text for the usual case, but also [a, b] for a vector or {k: v} for a map, so composite keys can be named too.

Deleted is not the same as empty

These assertions are about the entry, not its value. A key rewritten to 0, set to Option::None (stored as void), or holding an empty map is still a live ledger entry and fails both assertions. Delete the key with storage().remove(&key); overwriting it with a tombstone is not enough.

A failure lists every surviving entry with the tier it lives in, its rendered value, and the XDR bytes the ledger holds:

panicked at 'Expected contract storage to be empty, but 2 entries survived:
  persistent:balance = 0 [8 bytes: 0000000300000000]
  temporary:allowance = void [4 bytes: 00000000]'

Naming a key narrows the message to that key’s survivors:

panicked at 'Expected storage key "balance" to be deleted from all tiers, but it still exists:
  persistent:balance = 0 [8 bytes: 0000000300000000]'

A contract that never wrote storage passes assert_storage_empty!, and an address that is not a contract owns no contract data, so it passes too.

Failure messages

Assertions fail with the counts they observed, which is what makes them worth using over hand-rolled loops:

assertion `left == right` failed: Expected 3 events, found 1

Storage tier quota assertions

Protocols must keep storage tier entry counts within bounded budgets: instance storage is reserved for small singleton configuration, persistent storage holds durable state, and temporary storage manages short-lived queues. assert_storage_tier_count! validates entry count quotas per tier:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::assert_storage_tier_count;

// Enforce quotas using block syntax:
assert_storage_tier_count!(env, contract_id, {
    instance: 1,           // Exactly 1 instance entry
    max_persistent: 10,    // At most 10 persistent entries
    temporary: 0,          // No temporary storage entries
});

// Or concise assignment syntax:
assert_storage_tier_count!(env, contract_id, max_instance = 3, max_persistent = 10);
}

When a quota is breached, the assertion prints an informative breakdown table listing all stored keys per tier:

Storage allocation quota exceeded for contract:
Breached tiers: persistent (found 12, expected max 10)

| Tier        | Count | Quota          | Status | Keys                                       |
|-------------|-------|----------------|--------|--------------------------------------------|
| instance    | 1     | max 5          | PASS   | "cfg"                                      |
| persistent  | 12    | max 10         | BREACH | "u_1", "u_2", "u_3", ... (total 12)        |
| temporary   | 0     | exact 0        | PASS   | (none)                                     |

| assert_budget_consumed!(..) | Bounds the most recent invocation’s CPU and memory consumption |

Storage diff assertions

The assert_storage_diff! macro provides a declarative way to assert contract storage changes between two points in time. It captures a storage snapshot before operations, executes the test logic, and then verifies the expected changes against the actual storage diff.

#![allow(unused)]
fn main() {
use soroban_testkit_assert::assert_storage_diff;
use soroban_testkit_core::storage::{StorageSnapshot, StorageTier};

// Capture storage state before operations
let before = StorageSnapshot::capture(&env, &contract_id);

// ... perform contract operations ...
client.transfer(&from, &to, &100);

// Assert storage changes declaratively
assert_storage_diff!(before, env, contract_id, {
    added: [
        { key: "new_key" },
        { key: "allowance:*", tier: StorageTier::Temporary },
    ],
    modified: [
        { key: "balances", from: 1000u64, to: 900u64 },
    ],
    removed: [
        { key: "old_key" },
    ],
    unchanged: [
        { key: "admin", tier: StorageTier::Instance },
    ],
});
}

Clauses

The macro supports four declarative clauses:

ClausePurposeExample
addedKeys that should be newly present{ key: "new_key" }
modifiedKeys whose values should change{ key: "balances", from: 1000u64, to: 900u64 }
removedKeys that should be deleted{ key: "old_key" }
unchangedKeys that should remain exactly the same{ key: "admin", tier: StorageTier::Instance }

Key matching

Keys can be exact strings or glob patterns:

  • Exact: { key: "exact_key" }
  • Prefix wildcard: { key: "prefix_*" }
  • Suffix wildcard: { key: "*_suffix" }
  • Match all: { key: "*" }

Each entry can optionally specify a storage tier: tier: StorageTier::Instance, StorageTier::Persistent, or StorageTier::Temporary.

Value matching

Values can be specified in multiple ways:

  • Exact value: { key: "count", from: 42u32, to: 43u32 }
  • Typed value (with type annotation): { key: "count", from: u32 => 42, to: u32 => 43 }
  • Wildcard (match any value): { key: "count", from: _, to: _ } (using soroban_testkit_assert::__storage_diff_value!)

Failure diagnostics

When an assertion fails, the macro prints a detailed side-by-side diff table showing expected vs actual changes:

Storage diff assertion failed:
Expected modified key 'balances' (tier: persistent): from: expected '1000', found '999'

═══════════════════════════════════════════════════════════════════════════════
                           STORAGE DIFF REPORT
═══════════════════════════════════════════════════════════════════════════════

Summary: 1 added, 1 modified, 0 removed | Failures: 1

┌──────────────────────────────────────────────────────────────────────────────┐
│ MODIFIED                                                                      │
├────────┬──────────────────────┬──────────────────────┬──────────────────────┤
│ Tier   │ Key                  │ Before               │ After                │
├────────┼──────────────────────┼──────────────────────┼──────────────────────┤
│ persis │ balances             │ 1000                 │ 999                  │ ✗ UNEXPECTED
└────────┴──────────────────────┴──────────────────────┴──────────────────────┘

Using without pre-captured snapshot

The macro can also capture the “before” snapshot automatically when called without a pre-captured snapshot:

#![allow(unused)]
fn main() {
// Captures current state as "before", then asserts after operations
assert_storage_diff!(env, contract_id, {
    added: [{ key: "new_key" }],
    modified: [{ key: "count", from: 0u32, to: 1u32 }],
});
}

Note: This form captures the current state as the “before” snapshot when the macro is invoked, so any operations before the macro call are included in the baseline.

Multi-party balance conservation

Asserting balance conservation across multi-user operations (e.g., swaps, multi-hop routes, fee splits, or batch transfers) commonly leads to repetitive boilerplate when querying balances before and after calls. The assert_balance_conserved! macro captures pre-invocation balances, runs the operation, verifies that total tokens across all tracked participants plus reserves are conserved, and accounts for explicit external mints or burns:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::assert_balance_conserved;

// Standard 2-party transfer:
assert_balance_conserved!(
    accounts: [&alice, &bob],
    balance: |addr| token_client.balance(addr),
    call: {
        token_client.transfer(&alice, &bob, &100);
    }
);

// Multi-party swap with fees, liquidity pool reserve, and custom labels:
assert_balance_conserved!(
    accounts: [
        (&alice, "Alice (Trader)"),
        (&bob, "Bob (Recipient)"),
        (&pool, "AMM Liquidity Pool"),
        (&treasury, "Protocol Fee Vault")
    ],
    balance: |addr| token_client.balance(addr),
    call: {
        router.swap(&alice, &bob, &1000);
    }
);

// Invocations involving external mints or burns:
assert_balance_conserved!(
    accounts: [&alice, &vault],
    balance: |addr| token_client.balance(addr),
    mint: 500,
    burn: 100,
    call: {
        token_client.mint(&alice, &500);
        token_client.burn(&vault, &100);
    }
);
}

When an invariant is violated (for example, if a fee calculation suffers from fractional token loss or round-off leakage), assert_balance_conserved! panics with a formatted debit/credit reconciliation table:

Balance Conservation Breach: Discrepancy = -1 (LEAK DETECTED)
Expected net delta: 0 (mints: +0, burns: -0) | Actual net delta: -1

------------------------------------------------------------------------------------------------------
| Account / Reserve            |    Pre-Balance |   Post-Balance |     Delta (Debit/Credit) |
------------------------------------------------------------------------------------------------------
| Alice                        |           1000 |            900 |                     -100 |
| Bob                          |              0 |             97 |                      +97 |
| Fee Collector                |              0 |              2 |                       +2 |
------------------------------------------------------------------------------------------------------
| Total Tracked                |           1000 |            999 |                       -1 |
| External Mints (+)           |                |                |                       +0 |
| External Burns (-)           |                |                |                       -0 |
------------------------------------------------------------------------------------------------------
| Discrepancy                  |                |                |       -1 (LEAK DETECTED) |
------------------------------------------------------------------------------------------------------

Cross-invocation token conservation (assert_token_conservation_across_invocations!)

When a protocol moves tokens across several invocations — swaps, fee splits, batch settlements — the total supply must be conserved. The assert_token_conservation_across_invocations! macro records balances before and after each invocation, credits declared protocol fees to their treasury accounts, and panics with a debit/credit reconciliation table when an invocation leaks value:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::assert_token_conservation_across_invocations;

let report = assert_token_conservation_across_invocations!(
    accounts: [(&alice, "Alice"), (&bob, "Bob")],
    balance: |address| token_client.balance(address),
    fees: [(&treasury, "Treasury", 10i128)],
    invocations: [
        { router.swap(&alice, &bob, &1000); },
        { router.settle_fees(&treasury); },
    ],
);
assert!(report.is_conserved());
}

Declared fees are internal transfers to treasury accounts and are auto-added to the tracked set. A fee that never reaches its treasury is reported as a missing credit. TokenConservationReport exposes is_conserved, invocations, and fees for further assertions.

Comparative budget differential and relative gas savings

When refactoring code paths or evaluating alternative algorithm implementations (such as linear scan vs binary search, or unindexed vs indexed storage queries), smart contract engineers need to assert that an optimized branch achieves relative gas savings without regressing on CPU instruction or memory consumption.

The assert_budget_diff_within! macro executes baseline and optimized closures, measures the differential CPU instructions and memory bytes consumed, and asserts that relative savings targets are met.

Basic comparison

In default mode, the optimized branch must consume strictly fewer instructions or memory bytes than baseline and cannot regress in either dimension:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::assert_budget_diff_within;

assert_budget_diff_within!(
    &env,
    baseline: {
        client.linear_search(&items, &target);
    },
    optimized: {
        client.binary_search(&items, &target);
    },
);
}

Enforcing relative and absolute savings

Specify target percentage reductions (min_cpu_savings_percent, min_mem_savings_percent, min_savings_percent) or absolute thresholds (min_cpu_savings, min_mem_savings):

#![allow(unused)]
fn main() {
assert_budget_diff_within!(
    env: &env,
    baseline: || client.unindexed_scan(&key),
    optimized: || client.indexed_lookup(&key),
    min_cpu_savings_percent: 25.0, // At least 25% CPU instruction reduction
    min_mem_savings_percent: 10.0, // At least 10% memory byte reduction
);
}

Space-time tradeoffs and dual environments

For operations that modify contract state or trade slight memory increases for massive CPU reductions:

#![allow(unused)]
fn main() {
// Separate environments ensure identical initial state:
assert_budget_diff_within!(
    baseline: (&env1, { client1.bubble_sort(&data) }),
    optimized: (&env2, { client2.radix_sort(&data) }),
    min_cpu_savings_percent: 50.0,
    allow_mem_regression: true, // Permit memory increase for large CPU reduction
);
}

Side-by-side consumption table on failure

When an optimized branch regresses or fails to reach the required savings target, assert_budget_diff_within! panics with a formatted side-by-side consumption table:

Budget Differential Assertion Failed!
Optimized branch failed to achieve required gas savings or exceeded baseline limits.

======================================================================================================================================
| Metric             |         Baseline |        Optimized |   Delta (Opt-Base) |          Savings (%) | Target           |  Status  |
======================================================================================================================================
| CPU Instructions   |          100,000 |           85,000 |            -15,000 |      +15.00% savings | >= 25.00%        |   FAIL   |
--------------------------------------------------------------------------------------------------------------------------------------
| Memory (Bytes)     |            4,096 |            3,840 |               -256 |       +6.25% savings | <= baseline      |   PASS   |
======================================================================================================================================

Failure Summary:
  • Failed to achieve target CPU savings: achieved 15.00%, expected at least 25.00%

Event Sequence Assertions

While EventLog collects events across multiple invocations, verifying that events occurred in an exact sequence—or that a specific sequence occurred with allowed interleaving—is streamlined by assert_event_sequence!.

Strict and Relaxed Sequence Modes

assert_event_sequence! supports two modes:

  • Strict (exact_sequence): Every expected event must match consecutively 1:1 in length and order without unexpected or interleaved intermediate events.
  • Relaxed (ordered_subsequence): Expected events must appear in order, allowing intermediate unrelated events between them.
#![allow(unused)]
fn main() {
use soroban_testkit_assert::assert_event_sequence;
use soroban_testkit_assert::sequence::{Relaxed, Strict};

// Relaxed mode: allows intermediate noise/transfer events
assert_event_sequence!(event_log, [
    { topic: "Deposit", contract: vault_id },
    { topic: "Mint", contract: token_id },
    { topic: "Transfer", contract: token_id },
], mode = Relaxed);

// Strict mode: exact 1:1 sequence match
assert_event_sequence!(event_log, [
    { topic: "Deposit", contract: vault_id },
    { topic: "Mint", contract: token_id },
], mode = Strict);
}

Matching Payload Data Predicates

You can match topics, contract IDs, and payload data predicates:

#![allow(unused)]
fn main() {
assert_event_sequence!(event_log, [
    {
        topic: "Deposit",
        contract: vault_id,
        data: |data| data.deserialize::<u64>() == Some(500)
    },
    { topic: "Transfer", contract: token_id },
], mode = Relaxed);
}

Detailed Failure Diagnostics

When an event sequence fails, assert_event_sequence! outputs detailed diagnostics showing matched events, divergence points, and missing expectations:

Event sequence diverged at index 1 (mode = Strict / exact_sequence):
Expected event: { topic: "Mint", contract: CAAA... }
Observed event: topics: [Noise], contract: CAAA...

Full sequence breakdown:
  [ 0] ✓ MATCHED  | Expected: { topic: "Deposit" } | Observed: topics: [Deposit]
  [ 1] ✗ DIVERGED | Expected: { topic: "Mint" }    | Observed: topics: [Noise]
  [ 2]   PENDING  | Expected: { topic: "Transfer" }| Observed: topics: [Transfer]
Next step

Filtering by contract

from_contract narrows a matcher to one contract id — essential once a test wires several contracts together.

Guide

Testing patterns

See Testing Patterns for event and auth assertions in multi-contract suites.

Budget ranges and ceilings (assert_budget_consumed!)

assert_budget_consumed! bounds what the most recent top-level invocation cost (BudgetSnapshot::last_invocation), without manual snapshot arithmetic. Each metric takes either a range or an inclusive ceiling:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::assert_budget_consumed;

client.swap(&user, &100);

assert_budget_consumed!(env, {
    max_cpu: 150_000,
    max_mem: 50_000,
});

assert_budget_consumed!(env, {
    cpu: 10_000..=15_000, // any range: a..b, a..=b, a.., ..b, ..=b
    mem: 1_000..,
});
}
KeyValueMeaning
cpuu64 rangeCPU instructions must fall inside the range
max_cpuu64CPU instructions must not exceed it (inclusive)
memu64 rangememory bytes must fall inside the range
max_memu64memory bytes must not exceed it (inclusive)

An unknown key is a compile error. On failure every breached metric is reported at once, with the observed value, the expected bound, the miss, and the miss as a percentage of the crossed bound:

assert_budget_consumed! failed:
  cpu_insns: observed 1_650, expected ..=1_500 — over the upper bound by 150 (+10.00%)
  observed totals: cpu_insns=1_650 mem_bytes=812

The same check is available without the macro: build a BudgetBounds (.cpu(..), .max_cpu(..), .mem(..), .max_mem(..)) and call check(&snapshot) or assert_within(&snapshot) on any BudgetSnapshot.

Distinct Invoker Authorization

To enforce least-privilege delegation across addresses:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::assert_auth_invoker_distinct;

// Asserts that only admin_address authorized the operation
assert_auth_invoker_distinct!(&admin_address, &[admin_address.clone()]);
}

Golden snapshots (assert_snapshot_matches!)

assert_snapshot_matches! serializes a value into canonical JSON and compares it with a committed snapshot stored at tests/snapshots/<name>.snap.json (relative to the test crate’s CARGO_MANIFEST_DIR). The first run creates the file. Later runs fail with a colored unified diff, so an added, renamed or omitted field is visible as a removed and added line with exact line numbers:

#![allow(unused)]
fn main() {
use soroban_sdk::testutils::Address as _;
use soroban_testkit_assert::{assert_snapshot_matches, SnapshotOptions};

let state = ContractState { owner: addr, total: 100 };

// Uses an explicit `Env` (works for `Val`, `ScVal`, `Map`, and `#[contracttype]` values):
assert_snapshot_matches!("state after init", state, env);

// Two-argument form uses a fresh `Env`, so pass `env` explicitly for host objects:
assert_snapshot_matches!("answer", 42u32);

// Volatile fields and generated addresses can be normalized:
let options = SnapshotOptions::new()
    .normalize_addresses(true)
    .ignore_field("updated_at");
assert_snapshot_matches!("state after init", state, env, options);
}

Because macro_rules! cannot capture an implicit caller-local identifier, the two-argument arm uses a default Env and is meant for values that do not reference host objects; use the three-argument arm with your test’s env otherwise.

The conversion is also available directly:

#![allow(unused)]
fn main() {
use soroban_testkit_assert::{assert_snapshot_matches, snapshot_json};

let json = snapshot_json(&env, &state);
assert_snapshot_matches("state after init", &json);
}

snapshot_json accepts Val, ScVal, primitives and user #[contracttype] structs through TryIntoVal. The renderer sorts map keys, escapes strings consistently, writes integers as decimal text and covers Void, Bool, the unsigned and signed integer variants (including 128-bit), Symbol, String, Bytes (lowercase hex with a 0x prefix), Address, Timepoint, Duration, Vec and Map. Other ScVal variants get a readable debug fallback. Map insertion order therefore never changes a snapshot.

normalize_addresses(true) replaces addresses with ~address0, ~address1, … in order of first appearance, so regenerated addresses do not cause drift. ignore_field("...") drops a field at every depth from both the stored and current value; timestamps and nonces are typical candidates. Note that a missing snapshot is created with normalization applied.

Two environment variables control persistence:

  • SNAPSHOT_DIR overrides the compile-time tests/snapshots location at runtime.
  • UPDATE_SNAPSHOTS=1 rewrites the snapshot on drift instead of failing, which is convenient when an intentional state change needs to be accepted.

A mismatch looks like this (red for stored lines, green for current lines, cyan for headers):

snapshot assertion failed for "state after init": stored snapshot does not match the current value
--- expected (stored): tests/snapshots/state after init.snap.json
+++ actual (current): state after init
@@ -1,4 +1,4 @@
 {
-  "owner": "alice",
+  "owner": "bob",
   "total": 100
 }
set UPDATE_SNAPSHOTS=1 to accept the new snapshot