Skip to content
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

imp(tests): refactor table driven tests #881

Open
rnbguy opened this issue Sep 26, 2023 · 6 comments
Open

imp(tests): refactor table driven tests #881

rnbguy opened this issue Sep 26, 2023 · 6 comments
Assignees
Labels
O: code-hygiene Objective: aims to improve code hygiene O: maintainability Objective: cause to ease modification, fault corrections and improve code understanding O: testing Objective: aims to improve testing coverage

Comments

@rnbguy
Copy link
Collaborator

rnbguy commented Sep 26, 2023

Improvement Summary

Currently, some of the rust tests contain a group of table/data-driven tests. These tests will fail if one in the group fails.

fn test_parse_raw_coin() -> Result<(), TokenTransferError> {
{
let coin = RawCoin::from_str("123stake")?;
assert_eq!(coin.denom, "stake");
assert_eq!(coin.amount, 123u64.into());
}
{
let coin = RawCoin::from_str("1a1")?;
assert_eq!(coin.denom, "a1");
assert_eq!(coin.amount, 1u64.into());
}
{
let coin = RawCoin::from_str("0x1/:.\\_-")?;
assert_eq!(coin.denom, "x1/:.\\_-");
assert_eq!(coin.amount, 0u64.into());
}
{
// `!` is not allowed
let res = RawCoin::from_str("0x!");
assert!(res.is_err());
}

We want to treat each testcases individually. If one fails, the others in the group may continue.

Proposal

The current Rust release doesn't support this feature. The common opinion in the Rust community is to keep each testcases in a separate test method, potentially using a macro.

We don't want to implement/maintain our macro. So we will use rstest as a solution. The following is a refactored code of the above example using rstest.

#[rstest]
#[case::nat("123stake", RawCoin::new(123, "stake"))]
#[case::zero("0stake", RawCoin::new(0, "stake"))]
#[case::u256_max(
"115792089237316195423570985008687907853269984665640564039457584007913129639935stake",
RawCoin::new(
[u64::MAX; 4],
"stake"
)
)]
#[case::digit_in_denom("1a1", RawCoin::new(1, "a1"))]
#[case::chars_in_denom("0x1/:._-", RawCoin::new(0, "x1/:._-"))]
#[case::ibc_denom("1234ibc/a0B1C", RawCoin::new(1234, "ibc/a0B1C"))]
fn test_parse_raw_coin(#[case] parsed: RawCoin, #[case] expected: RawCoin) {
assert_eq!(parsed, expected);
}

@rnbguy rnbguy added O: testing Objective: aims to improve testing coverage O: code-hygiene Objective: aims to improve code hygiene O: maintainability Objective: cause to ease modification, fault corrections and improve code understanding labels Sep 26, 2023
@rnbguy rnbguy self-assigned this Sep 26, 2023
@rnbguy rnbguy changed the title Refactor table driven tests imp(tests): refactor table driven tests Jan 18, 2024
@kien6034
Copy link

kien6034 commented Apr 1, 2024

hi, can i work on this

@rnbguy rnbguy added the A: blocked Admin: blocked by another (internal/external) issue or PR label Apr 2, 2024
@rnbguy
Copy link
Collaborator Author

rnbguy commented Apr 2, 2024

@kien6034 thanks for your interest! but this requires significant changes, that

@TropicalDog17
Copy link
Contributor

Hey has this issue un-blocked after #1109 @rnbguy

@rnbguy rnbguy removed the A: blocked Admin: blocked by another (internal/external) issue or PR label Jul 23, 2024
@rnbguy
Copy link
Collaborator Author

rnbguy commented Jul 23, 2024

hey @TropicalDog17, thanks for taking interest. This requires some thinking from our side. But you can work on the straight-forward cases.

@TropicalDog17
Copy link
Contributor

@rnbguy can I rewrite the tests on ibc-testkit crate?

@rnbguy
Copy link
Collaborator Author

rnbguy commented Jul 29, 2024

Don't try to refactor the complicated tests with fixtures. If you come across some tests which look like the following, you may refactor.

#[test]
fn test_my_function() {
  my_function("hello world").expect("no error");
  my_function("hello mars").expect_err("error");
}
#[rstest]
#[case("hello world")]
#[should_panic]
#[case("hello mars)]
fn test_my_function_with_rstest(value: &str) {
  my_function(value).unwrap();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O: code-hygiene Objective: aims to improve code hygiene O: maintainability Objective: cause to ease modification, fault corrections and improve code understanding O: testing Objective: aims to improve testing coverage
Projects
Status: 📥 To Do
Development

No branches or pull requests

3 participants