Integration Tests
Integration tests are tests that include the integration between components, and so test a larger scope such as other contracts.
The Soroban Rust SDK makes it just as easy to integration test by providing utilities for testing against real contracts fetched from mainnet, testnet, or the local file system.
How to Write Integration Testsβ
The following is an example of a test that includes a dependency contract into the test, rather than mock it. The test is written to test the increment-with-pause contract and the pause contract. The contract has an increment
function that increases a counter value by one on every invocation. The contract depends on the pause contract to control whether the increment functionality is paused.
The following tests set up the increment-with-pause
contract, as well as import and register the real pause contract using it's wasm file.
-
Use the [stellar contract fetch] command to fetch the dependency contract that's already deployed on testnet or mainnet.
stellar contract fetch --id C... > pause.wasm
-
Import the contract into the tests with the
contractimport!
macro.mod pause {
soroban_sdk::contractimport!(file = "pause.wasm");
} -
Write a test similar to the following that registers not only the increment-with-pause contract, but also the pause contract that was imported. The test checks that when the pause contract is not paused that the increment contract operates as expected. When it is paused, the increment function errors. Once it's unpaused the function operates as expected.
#[test]
fn test() {
let env = Env::default();
let pause_id = env.register(pause::WASM, ());
let pause_client = pause::Client::new(&env, &pause_id);
let contract_id = env.register(
IncrementContract,
IncrementContractArgs::__constructor(&pause_id),
);
let client = IncrementContractClient::new(&env, &contract_id);
pause_client.set(&false);
assert_eq!(client.increment(), 1);
pause_client.set(&true);
assert_eq!(client.try_increment(), Err(Ok(Error::Paused)));
pause_client.set(&false);
assert_eq!(client.increment(), 2);
}
Most tests, whether they're unit, mocks, or integration tests, will look very similar to the test above. The tests will do four things:
- Create an environment, the
Env
. - Register the contract(s) to be tested.
- Invoke functions using the generated client.
- Assert the outcome.
Guides in this category:
ποΈ Unit Tests
Unit tests are small tests that test smart contracts.
ποΈ Mocking
Mocking dependency contracts in tests.
ποΈ Integration Tests
Integration testing uses dependency contracts instead of mocks.
ποΈ Fuzzing
Fuzzing and property testing to find unexpected behavior.
ποΈ Differential Tests
Differential testing detects unintended changes.
ποΈ Differential Tests with Test Snapshots
Differential testing using automatic test snapshots.
ποΈ Mutation Testing
Mutation testing finds code not tested.
ποΈ Test Authorization
Write tests that test contract authorization.
ποΈ Test Events
Write tests that test contract events.