Implement basic tests for a contract
A contract's test functions can be used as a simple way to ensure a contract's functions behave as expected. The increment example contract has a function that increments a counter by one on every invocation. The corresponding test invokes that function several time, ensuring with assert_eq!(...)
the count increases as expected.
#![cfg(test)]
use super::{IncrementContract, IncrementContractClient};
use soroban_sdk::{Env};
#[test]
fn test() {
// Almost every test will begin this same way. A default Soroban environment
// is created and the contract (along with its client) is registered in it.
let env = Env::default();
let contract_id = env.register_contract(None, IncrementContract);
let client = IncrementContractClient::new(&env, &contract_id);
assert_eq!(client.increment(), 1);
assert_eq!(client.increment(), 2);
assert_eq!(client.increment(), 3);
}
Guides in this category:
📄️ Implement basic tests for a contract
Use the increment example contract to ensure a contract's functions behave as expected
📄️ Detecting Unexpected Changes with Test Snapshots
Use test snapshots to detect unexpected changes in contract behavior
📄️ Test authorized contract invocations
Use the auth example contract to ensure the authorization is carried out as expected