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

Parametrization? #27

Open
svenstaro opened this issue Apr 24, 2019 · 4 comments
Open

Parametrization? #27

svenstaro opened this issue Apr 24, 2019 · 4 comments

Comments

@svenstaro
Copy link

Is there anything we can do to achieve parametrization of tests such as pytest has and rstest re-implements?

If not, take this as a feature suggestion. :)

In case you don't know what I mean: tests parametrization generates a bunch of test cases from an iterable of input parameters and injects the values into the tests.

@adaqus
Copy link

adaqus commented Jun 27, 2019

It would be really nice to have such feature. +1

@adaqus
Copy link

adaqus commented Aug 15, 2019

I can try to implement such a feature. It could meet following requirements:

  • parametrized test can be based on it/test block,
  • declare parameter names, whether they are mutable, eventually their type,
  • declare each set of parameters, each set has its name which is used to generate final test function.

Example:

it "can add" for (a: usize, b: usize, expected: usize) [
    "1 and 2" => (1, 2, 3),
    "4 and 8" => (4, 8, 12)
] {
    assert_eq!(a + b, expected);
}

which will resolve to:

#[test]
fn can_add_1_and_2() {
    let a: usize = 1;
    let b: usize = 2;
    let expected: usize = 3;

    assert_eq!(a + b, expected);
}

#[test]
fn can_add_4_and_8() {
    let a: usize = 4;
    let b: usize = 8;
    let expected: usize = 12;

    assert_eq!(a + b, expected);
}

Example 2:

it "can push" for (mut v: Vec<usize>, to_push: usize, expected: Vec<usize>) [
    "some number" => (vec![1, 24], 2, vec![1, 24, 2]),
    "another number" => (vec![3, 15], 11, vec![3, 15, 11])
] {
    v.push(to_push);
    assert_eq!(v, expected);
}

which will resolve to:

#[test]
fn can_push_some_number() {
    let mut v: Vec<usize> = vec![1, 24];
    let to_push: usize = 2;
    let expected: Vec<usize> = vec![1, 24, 2];

    assert_eq!(v, expected);
}

#[test]
fn can_push_another_number() {
    let mut v: Vec<usize> = vec![3, 15];
    let to_push: usize = 11;
    let expected: Vec<usize> = vec![3, 15, 11];

    assert_eq!(v, expected);
}

@sagebind
Copy link

sagebind commented Aug 15, 2019

That would actually be amazing! The suggested syntax looks quite natural.

@adaqus
Copy link

adaqus commented Aug 18, 2019

@utkarshkukreti what do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants