-
Notifications
You must be signed in to change notification settings - Fork 238
Parametrized tests docs #3697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
franciszekjob
wants to merge
28
commits into
1431-parametrized-tests
Choose a base branch
from
1431-docs
base: 1431-parametrized-tests
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Parametrized tests docs #3697
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
dad22ef
Add docs
franciszekjob 4980f42
Update changelog
franciszekjob cff51bf
Fix tests
franciszekjob 8f6e2b5
Fix tests
franciszekjob 6430592
Update docs
franciszekjob c8fe288
Fix tests
franciszekjob d2ee2fe
Fix tests
franciszekjob a7c4a3e
Fix tests
franciszekjob 01d7ae0
Fix tests
franciszekjob afa4e47
Add snippet configs
franciszekjob afe17af
Fix test attributes docs
franciszekjob ca03f80
Increase threshold in test
franciszekjob 23b2375
Merge branch '1431-parametrized-tests' of https://github.com/foundry-…
franciszekjob 41586a1
Split test package into multiple packages
franciszekjob 4f2971f
Fix package name
franciszekjob 471f37f
Rename "Complex" -> "Advanced"
franciszekjob a4553b2
Apply code review suggestion
franciszekjob 6a98e7f
Fix tests
franciszekjob 11f3164
Fix tests
franciszekjob c4534eb
Fix test
franciszekjob 3bbb972
Update description
franciszekjob 05541bd
Add note on possible test case name collisions
franciszekjob e871781
Merge branch '1431-parametrized-tests' into 1431-docs
franciszekjob 37c8d8e
Merge branch '1431-parametrized-tests' of https://github.com/foundry-…
franciszekjob e2e2758
Apply code review suggestion
franciszekjob 74ec926
Update example test
franciszekjob 04efcf6
Merge branch '1431-docs' of https://github.com/foundry-rs/starknet-fo…
franciszekjob 9428e2c
Remove unnecessary files
franciszekjob File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "parametrized_testing_advanced" | ||
version = "0.1.0" | ||
edition = "2024_07" | ||
|
||
[dependencies] | ||
starknet = "2.12.0" | ||
assert_macros = "2.12.0" | ||
|
||
[dev-dependencies] | ||
snforge_std = { path = "../../../snforge_std" } | ||
|
||
[scripts] | ||
test = "snforge test" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#[starknet::contract] | ||
pub mod HelloStarknet { | ||
#[storage] | ||
struct Storage {} | ||
} |
20 changes: 20 additions & 0 deletions
20
docs/listings/parametrized_testing_advanced/tests/example.cairo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#[derive(Copy, Drop)] | ||
struct User { | ||
pub name: felt252, | ||
pub age: u8, | ||
} | ||
|
||
#[generate_trait] | ||
impl UserImpl of UserTrait { | ||
fn is_adult(self: @User) -> bool { | ||
return *self.age >= 18_u8; | ||
} | ||
} | ||
|
||
#[test] | ||
#[test_case(User { name: 'Alice', age: 20 }, true)] | ||
#[test_case(User { name: 'Bob', age: 14 }, false)] | ||
#[test_case(User { name: 'Josh', age: 18 }, true)] | ||
fn test_is_adult(user: User, expected: bool) { | ||
assert_eq!(user.is_adult(), expected); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "parametrized_testing_basic" | ||
version = "0.1.0" | ||
edition = "2024_07" | ||
|
||
[dependencies] | ||
starknet = "2.12.0" | ||
assert_macros = "2.12.0" | ||
|
||
[dev-dependencies] | ||
snforge_std = { path = "../../../snforge_std" } | ||
|
||
[scripts] | ||
test = "snforge test" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#[starknet::contract] | ||
pub mod HelloStarknet { | ||
#[storage] | ||
struct Storage {} | ||
} |
10 changes: 10 additions & 0 deletions
10
docs/listings/parametrized_testing_basic/tests/example.cairo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
fn sum(x: felt252, y: felt252) -> felt252 { | ||
return x + y; | ||
} | ||
|
||
#[test] | ||
#[test_case(1, 2, 3)] | ||
#[test_case(3, 4, 7)] | ||
fn test_sum(x: felt252, y: felt252, expected: felt252) { | ||
assert_eq!(sum(x, y), expected); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "parametrized_testing_fuzzer" | ||
version = "0.1.0" | ||
edition = "2024_07" | ||
|
||
[dependencies] | ||
starknet = "2.12.0" | ||
assert_macros = "2.12.0" | ||
|
||
[dev-dependencies] | ||
snforge_std = { path = "../../../snforge_std" } | ||
|
||
[scripts] | ||
test = "snforge test" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#[starknet::contract] | ||
pub mod HelloStarknet { | ||
#[storage] | ||
struct Storage {} | ||
} |
11 changes: 11 additions & 0 deletions
11
docs/listings/parametrized_testing_fuzzer/tests/example.cairo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
fn sum(x: felt252, y: felt252) -> felt252 { | ||
return x + y; | ||
} | ||
|
||
#[test] | ||
#[test_case(1, 2)] | ||
#[test_case(3, 4)] | ||
#[fuzzer(runs: 10)] | ||
fn test_sum(x: felt252, y: felt252) { | ||
assert_eq!(sum(x, y), x + y); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
docs/src/snforge-advanced-features/parametrized-testing.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# Parametrized Testing | ||
|
||
Sometimes, you want to run the same test logic with different inputs. | ||
Instead of duplicating code into separate test functions, you can use parameterized tests. | ||
|
||
Parameterized tests allow you to define multiple test cases for a single function by attaching the | ||
`#[test_case]` attribute. | ||
|
||
Each test case provides its own set of arguments, and `snforge` will automatically generate separate test instances for them. | ||
|
||
## Basic Example | ||
|
||
To turn a regular test into a parameterized one, add the `#[test_case(...)]` attribute above it. | ||
You can provide any valid Cairo expressions as arguments. | ||
|
||
Below is a simple example which checks addition of two numbers. | ||
|
||
```rust | ||
{{#include ../../listings/parametrized_testing_basic/tests/example.cairo}} | ||
``` | ||
|
||
Now run: | ||
|
||
<!-- { "package_name": "parametrized_testing_basic" } --> | ||
```shell | ||
$ snforge test | ||
``` | ||
|
||
<details> | ||
<summary>Output:</summary> | ||
|
||
```shell | ||
Collected 2 test(s) from parametrized_testing_basic package | ||
Running 0 test(s) from src/ | ||
Running 2 test(s) from tests/ | ||
[PASS] parametrized_testing_basic_integrationtest::example::test_sum_1_2_3 ([..]) | ||
[PASS] parametrized_testing_basic_integrationtest::example::test_sum_3_4_7 ([..]) | ||
Tests: 2 passed, 0 failed, 0 ignored, [..] filtered out | ||
``` | ||
</details> | ||
<br> | ||
|
||
## Naming Test Cases | ||
|
||
Each parameterized test gets its own generated name. There are two ways to control it: | ||
|
||
- **Unnamed test case** - the name is generated based on the function name and the arguments provided. | ||
|
||
```rust | ||
#[test_case(1, 2, 3)] | ||
fn test_sum(x: felt252, y: felt252, expected: felt252) { | ||
assert_eq!(sum(x, y), expected); | ||
} | ||
``` | ||
This will generate a test named `test_sum_1_2_3`. | ||
|
||
- **Named test case** - you can provide a custom name for the test case using the `name` parameter. | ||
|
||
```rust | ||
#[test_case(name: "one_plus_two", 1, 2, 3)] | ||
fn test_sum(x: felt252, y: felt252, expected: felt252) { | ||
assert_eq!(sum(x, y), expected); | ||
} | ||
``` | ||
This will generate a test named `test_sum_one_plus_two`. | ||
|
||
> 📝 **Note** | ||
> For unnamed test cases, it's possible that two different input values of the same type can generate the same test case name. | ||
> In such cases we emit a diagnostic error. | ||
> To resolve it, simply provide an explicit `name` for the case. | ||
|
||
## Advanced Example | ||
|
||
Now let's look at an addvanced example which uses structs as parameters. | ||
|
||
```rust | ||
{{#include ../../listings/parametrized_testing_advanced/tests/example.cairo}} | ||
``` | ||
|
||
Now run: | ||
|
||
<!-- { "package_name": "parametrized_testing_advanced" } --> | ||
```shell | ||
$ snforge test | ||
``` | ||
|
||
<details> | ||
<summary>Output:</summary> | ||
|
||
```shell | ||
Collected 3 test(s) from parametrized_testing_advanced package | ||
Running 3 test(s) from tests/ | ||
[PASS] parametrized_testing_advanced_integrationtest::example::test_is_adult_user_name_alice_age_20_true ([..]) | ||
[PASS] parametrized_testing_advanced_integrationtest::example::test_is_adult_user_name_josh_age_18_true ([..]) | ||
[PASS] parametrized_testing_advanced_integrationtest::example::test_is_adult_user_name_bob_age_14_false ([..]) | ||
Running 0 test(s) from src/ | ||
Tests: 3 passed, 0 failed, 0 ignored, [..] filtered out | ||
``` | ||
</details> | ||
<br> | ||
|
||
## Combining With Fuzzer Attribute | ||
|
||
`#[test_case]` can be freely combined with the `#[fuzzer]` attribute. | ||
|
||
Below is an example in which we will fuzz the test but also run the specific defined cases. | ||
|
||
```rust | ||
{{#include ../../listings/parametrized_testing_fuzzer/tests/example.cairo}} | ||
``` | ||
|
||
Now run: | ||
|
||
<!-- { "package_name": "parametrized_testing_fuzzer" } --> | ||
```shell | ||
$ snforge test | ||
``` | ||
|
||
<details> | ||
<summary>Output:</summary> | ||
|
||
```shell | ||
Collected 3 test(s) from parametrized_testing_fuzzer package | ||
Running 3 test(s) from tests/ | ||
[PASS] parametrized_testing_fuzzer_integrationtest::example::test_sum_1_2 ([..]) | ||
[PASS] parametrized_testing_fuzzer_integrationtest::example::test_sum_3_4 ([..]) | ||
[PASS] parametrized_testing_fuzzer_integrationtest::example::test_sum ([..]) | ||
Running 0 test(s) from src/ | ||
Tests: 3 passed, 0 failed, 0 ignored, 0 filtered out | ||
Fuzzer seed: [..] | ||
``` | ||
</details> | ||
<br> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.