Skip to content

Commit 64ea63a

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

File tree

28 files changed

+894
-31
lines changed

28 files changed

+894
-31
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tests/
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "test_case_attr"
3+
version = "0.1.0"
4+
edition = "2024_07"
5+
6+
[dependencies]
7+
starknet = "2.9.3"
8+
9+
[dev-dependencies]
10+
snforge_std = { path = "../../../../../../snforge_std" }
11+
12+
[[target.starknet-contract]]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#[test]
2+
#[test_case(3, 4, 7)]
3+
fn function_without_params() {
4+
let _x = 10;
5+
}
6+
7+
#[test]
8+
#[test_case(3, 4, 7)]
9+
fn function_with_invalid_params_count(x: felt252, y: felt252) {
10+
let _x = 10;
11+
}
12+
13+
#[test]
14+
#[test_case(name: array![1, 2, 3], 3, 4, 7)]
15+
fn invalid_name_arg(x: felt252, y: felt252, expected: felt252) {
16+
let _x = 10;
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "test_case"
3+
version = "0.1.0"
4+
edition = "2024_07"
5+
6+
[dependencies]
7+
starknet = "2.10.0"
8+
9+
[dev-dependencies]
10+
snforge_std = { path = "../../../../../snforge_std" }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#[starknet::contract]
2+
pub mod HelloStarknet {
3+
#[storage]
4+
struct Storage {}
5+
}
6+
7+
8+
pub fn add(a: felt252, b: felt252) -> felt252 {
9+
a + b
10+
}
11+
12+
pub fn fib(a: felt252, b: felt252, n: felt252) -> felt252 {
13+
match n {
14+
0 => a,
15+
_ => fib(b, a + b, n - 1),
16+
}
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use test_case::fib;
2+
3+
#[test]
4+
#[test_case(0, 1, 3)]
5+
#[test_case(0, 1, 100000)]
6+
fn test_fib_with_threshold(a: felt252, b: felt252, n: felt252) {
7+
let threshold: u256 = 10;
8+
let res = fib(a, b, n);
9+
let res: u256 = res.try_into().unwrap();
10+
assert!(res > threshold, "result should be greater than threshold");
11+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use test_case::add;
2+
3+
#[test]
4+
#[test_case(1, 2, 3)]
5+
#[test_case(3, 4, 7)]
6+
#[available_gas(l2_gas: 40000000)]
7+
fn with_available_gas(a: felt252, b: felt252, expected: felt252) {
8+
let result = add(a, b);
9+
assert!(result == expected);
10+
}
11+
12+
#[available_gas(l2_gas: 10000)]
13+
#[test_case(1, 2, 3)]
14+
#[test_case(3, 4, 7)]
15+
#[test]
16+
fn with_available_gas_exceed_limit(a: felt252, b: felt252, expected: felt252) {
17+
let result = add(a, b);
18+
assert!(result == expected);
19+
}
20+
21+
22+
#[test]
23+
#[should_panic(expected: 'panic message')]
24+
#[test_case(1, 2, 3)]
25+
#[test_case(3, 4, 7)]
26+
fn with_should_panic(a: felt252, b: felt252, expected: felt252) {
27+
let x: i8 = -1;
28+
assert(x > 0, 'panic message');
29+
}
30+
31+
#[test]
32+
#[test_case(1, 2)]
33+
#[test_case(3, 4)]
34+
#[fuzzer]
35+
fn with_fuzzer(a: felt252, b: felt252) {
36+
add(a, b);
37+
}
38+
39+
#[test_case(1, 2)]
40+
#[test_case(3, 4)]
41+
#[test]
42+
#[fuzzer]
43+
fn with_fuzzer_different_order(a: felt252, b: felt252) {
44+
add(a, b);
45+
}
46+
47+
#[test]
48+
#[test_case(1, 2, 3)]
49+
#[ignore]
50+
#[test_case(3, 4, 7)]
51+
fn with_ignore(a: felt252, b: felt252, expected: felt252) {
52+
let result = add(a, b);
53+
assert!(result == expected);
54+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use test_case::add;
2+
3+
#[test]
4+
#[test_case(1, 2, 3)]
5+
#[test_case(3, 4, 7)]
6+
#[test_case(5, 6, 11)]
7+
fn simple_addition(a: felt252, b: felt252, expected: felt252) {
8+
let result = add(a, b);
9+
assert!(result == expected);
10+
}
11+
12+
#[test]
13+
#[test_case(name: "one_and_two", 1, 2, 3)]
14+
#[test_case(name: "three_and_four", 3, 4, 7)]
15+
#[test_case(name: "five_and_six", 5, 6, 11)]
16+
fn addition_with_name_arg(a: felt252, b: felt252, expected: felt252) {
17+
let result = add(a, b);
18+
assert!(result == expected);
19+
}
20+

crates/forge/tests/e2e/fuzzing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ fn no_fuzzer_attribute() {
311311
assert_stdout_contains(
312312
output,
313313
indoc! {r"
314-
error: Plugin diagnostic: #[test] function with parameters must have #[fuzzer] attribute
314+
error: Plugin diagnostic: #[test] function with parameters must have #[fuzzer] or #[test_case] attribute
315315
--> [..]no_attribute.cairo:1:1
316316
#[test]
317317

0 commit comments

Comments
 (0)