Skip to content

Commit f754adc

Browse files
Parametrized tests docs (#3697)
<!-- Reference any GitHub issues resolved by this PR --> Towards #1431 **Stack**: - #3697 ⬅ - #3669 - #3671 - #3670 ## Introduced changes Add docs ## Checklist <!-- Make sure all of these are complete --> - [ ] Linked relevant issue - [x] Updated relevant documentation - [ ] Added relevant tests - [x] Performed self-review of the code - [x] Added changes to `CHANGELOG.md`
1 parent 64ea63a commit f754adc

File tree

14 files changed

+269
-2
lines changed

14 files changed

+269
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Forge
1111

12+
#### Added
13+
14+
- `#[test_case]` attribute for parameterized testing. Read more [here](https://foundry-rs.github.io/starknet-foundry/snforge-advanced-features/parametrized-testing.html)
15+
1216
#### Changed
1317

1418
- Minimal supported `Scarb` version is now `2.10.0` (updated from `2.9.1`)
@@ -26,7 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2630

2731
#### Added
2832

29-
`sncast declare-from` command to declare a contract by fetching it from a different Starknet instance
33+
- `sncast declare-from` command to declare a contract by fetching it from a different Starknet instance
3034

3135
## [0.49.0] - 2025-09-03
3236

crates/forge/tests/data/test_case/tests/exit_first.cairo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use test_case::fib;
22

33
#[test]
44
#[test_case(0, 1, 3)]
5-
#[test_case(0, 1, 100000)]
5+
#[test_case(0, 1, 500000)]
66
fn test_fib_with_threshold(a: felt252, b: felt252, n: felt252) {
77
let threshold: u256 = 10;
88
let res = fib(a, b, n);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "parametrized_testing_advanced"
3+
version = "0.1.0"
4+
edition = "2024_07"
5+
6+
[dependencies]
7+
starknet = "2.12.0"
8+
assert_macros = "2.12.0"
9+
10+
[dev-dependencies]
11+
snforge_std = { path = "../../../snforge_std" }
12+
13+
[scripts]
14+
test = "snforge test"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[starknet::contract]
2+
pub mod HelloStarknet {
3+
#[storage]
4+
struct Storage {}
5+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#[derive(Copy, Drop)]
2+
struct User {
3+
pub name: felt252,
4+
pub age: u8,
5+
}
6+
7+
#[generate_trait]
8+
impl UserImpl of UserTrait {
9+
fn is_adult(self: @User) -> bool {
10+
return *self.age >= 18_u8;
11+
}
12+
}
13+
14+
#[test]
15+
#[test_case(User { name: 'Alice', age: 20 }, true)]
16+
#[test_case(User { name: 'Bob', age: 14 }, false)]
17+
#[test_case(User { name: 'Josh', age: 18 }, true)]
18+
fn test_is_adult(user: User, expected: bool) {
19+
assert_eq!(user.is_adult(), expected);
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "parametrized_testing_basic"
3+
version = "0.1.0"
4+
edition = "2024_07"
5+
6+
[dependencies]
7+
starknet = "2.12.0"
8+
assert_macros = "2.12.0"
9+
10+
[dev-dependencies]
11+
snforge_std = { path = "../../../snforge_std" }
12+
13+
[scripts]
14+
test = "snforge test"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[starknet::contract]
2+
pub mod HelloStarknet {
3+
#[storage]
4+
struct Storage {}
5+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn sum(x: felt252, y: felt252) -> felt252 {
2+
return x + y;
3+
}
4+
5+
#[test]
6+
#[test_case(1, 2, 3)]
7+
#[test_case(3, 4, 7)]
8+
fn test_sum(x: felt252, y: felt252, expected: felt252) {
9+
assert_eq!(sum(x, y), expected);
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "parametrized_testing_fuzzer"
3+
version = "0.1.0"
4+
edition = "2024_07"
5+
6+
[dependencies]
7+
starknet = "2.12.0"
8+
assert_macros = "2.12.0"
9+
10+
[dev-dependencies]
11+
snforge_std = { path = "../../../snforge_std" }
12+
13+
[scripts]
14+
test = "snforge test"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[starknet::contract]
2+
pub mod HelloStarknet {
3+
#[storage]
4+
struct Storage {}
5+
}

0 commit comments

Comments
 (0)