Skip to content

Commit d2524ec

Browse files
Small tweaks to the unit testing how-to guide (#1095)
* Small tweaks to the unit testing how-to guide * tweaks * tweak * tweaks * tweaks * link to example * tweak
1 parent ef856ef commit d2524ec

File tree

3 files changed

+51
-28
lines changed

3 files changed

+51
-28
lines changed

docs/build/guides/testing/README.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ sidebar_class_name: sidebar-category-items-hidden
55
sidebar_position: 110
66
---
77

8-
Smart contract testing is vital to ensure safe, resilient, and accurate computation.
8+
Testing is vital to ensure that smart contracts are safe, resilient, and accurate.

docs/build/guides/testing/basic-contract-tests.mdx

-27
This file was deleted.
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: Unit Tests
3+
hide_table_of_contents: true
4+
description: Unit tests are small tests that test smart contracts.
5+
sidebar_position: 1
6+
---
7+
8+
Unit tests are small tests that test one piece of functionality within a contract.
9+
10+
The following is an example of a unit test, written to test the [increment contract]. The contract has an `increment` function, that increases a counter value by one on every invocation. The following test invokes that contract's function several times, and checks that the value increases by one.
11+
12+
```rust
13+
#![cfg(test)]
14+
use crate::{IncrementContract, IncrementContractClient};
15+
use soroban_sdk::Env;
16+
17+
#[test]
18+
fn test() {
19+
let env = Env::default();
20+
let contract_id = env.register(IncrementContract, ());
21+
let client = IncrementContractClient::new(&env, &contract_id);
22+
23+
assert_eq!(client.increment(), 1);
24+
assert_eq!(client.increment(), 2);
25+
assert_eq!(client.increment(), 3);
26+
}
27+
```
28+
Ref: https://github.com/stellar/soroban-examples/blob/main/increment/src/test.rs
29+
30+
:::tip
31+
32+
Tests are written in Rust, alongside the contract. Same tools. Same APIs. Same SDK. No context switching! Use your favorite Rust tools and libraries that you'd use for any Rust test.
33+
34+
:::
35+
36+
:::info
37+
38+
The Env created at the beginning of the test is not a simulation of the Soroban Environment. It's the same Soroban Environment that mainnet uses to execute contracts. There are only some minor differences: test utilities are enabled, and the storage backend is different.
39+
40+
:::
41+
42+
It's a simple test, but it's a complete test. There's a full environment setup, used, and torn down in the test, and it happens fast. The Rust test harness runs all the tests for a contract in parallel and each will have its own isolated contract environment.
43+
44+
Most tests, even integration tests and fuzz tests, will look very similar to this unit test. They'll do four things:
45+
1. Create an environment, the `Env`.
46+
2. Register the contract(s) to be tested.
47+
3. Invoke functions using a client.
48+
4. Assert the outcome.
49+
50+
[increment contract]: https://github.com/stellar/soroban-examples/blob/main/increment/src/lib.rs

0 commit comments

Comments
 (0)