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

[Testing]: Improve the test setup experience with rstest #2384

Closed
pitoniak32 opened this issue Dec 5, 2024 · 3 comments · Fixed by #2407
Closed

[Testing]: Improve the test setup experience with rstest #2384

pitoniak32 opened this issue Dec 5, 2024 · 3 comments · Fixed by #2407
Assignees
Labels
enhancement New feature or request triage:todo Needs to be traiged.

Comments

@pitoniak32
Copy link
Contributor

Related Problems?

I notice there is some setup required for tests that would like to test multiple cases with slightly different inputs.

Extra setup can lead to tests not being updated as easily, and being neglected more often than if its trivial to add a case.

I personally also find the fixture based approach easier to read / reason about, and it takes out the extra logic needed to setup a test fixture.

Describe the solution you'd like:

Try out using rstest crate as a dev dependency to improve the test writing ergonomics.

Example:

#[test]
fn merge_resource_schema_url() {
    // if both resources contains key value pairs
    let test_cases = vec![
        (Some("http://schema/a"), None, Some("http://schema/a")),
        (Some("http://schema/a"), Some("http://schema/b"), None),
        (None, Some("http://schema/b"), Some("http://schema/b")),
        (
            Some("http://schema/a"),
            Some("http://schema/a"),
            Some("http://schema/a"),
        ),
        (None, None, None),
    ];

    for (schema_url_a, schema_url_b, expected_schema_url) in test_cases.into_iter() {
        let resource_a = Resource::from_schema_url(
            vec![KeyValue::new("key", "")],
            schema_url_a.unwrap_or(""),
        );
        let resource_b = Resource::from_schema_url(
            vec![KeyValue::new("key", "")],
            schema_url_b.unwrap_or(""),
        );

        let merged_resource = resource_a.merge(&resource_b);
        let result_schema_url = merged_resource.schema_url();

        assert_eq!(
            result_schema_url.map(|s| s as &str),
            expected_schema_url,
            "Merging schema_url_a {:?} with schema_url_b {:?} did not yield expected result {:?}",
            schema_url_a, schema_url_b, expected_schema_url
        );
    }
}

vs

#[rstest]
#[case(Some("http://schema/a"), None, Some("http://schema/a"))]
#[case(Some("http://schema/a"), Some("http://schema/b"), None)]
#[case(None, Some("http://schema/b"), Some("http://schema/b"))]
#[case(
    Some("http://schema/a"),
    Some("http://schema/a"),
    Some("http://schema/a")
)]
#[case(None, None, None)]
fn merge_resource_schema_url(
    #[case] schema_url_a: Option<&'static str>,
    #[case] schema_url_b: Option<&'static str>,
    #[case] expected_schema_url: Option<&'static str>,
) {
    let resource_a =
        Resource::from_schema_url(vec![KeyValue::new("key", "")], schema_url_a.unwrap_or(""));
    let resource_b =
        Resource::from_schema_url(vec![KeyValue::new("key", "")], schema_url_b.unwrap_or(""));

    let merged_resource = resource_a.merge(&resource_b);
    let result_schema_url = merged_resource.schema_url();

    assert_eq!(
        result_schema_url.map(|s| s as &str),
        expected_schema_url,
        "Merging schema_url_a {:?} with schema_url_b {:?} did not yield expected result {:?}",
        schema_url_a,
        schema_url_b,
        expected_schema_url
    );
}

Considered Alternatives

No response

Additional Context

Some considerations might include if this will negatively impact the speed at which tests build if used frequently

@pitoniak32 pitoniak32 added enhancement New feature or request triage:todo Needs to be traiged. labels Dec 5, 2024
@lalitb
Copy link
Member

lalitb commented Dec 5, 2024

Probably related : #1552

@pitoniak32
Copy link
Contributor Author

yeah this seems related, this would help to get that ball rolling. It seems that issue has been passed around a few times. If there is interest in introducing rstest I can take a look at moving that issue forward.

@cijothomas
Copy link
Member

@pitoniak32 Lets give it a try. From the description, it looks very promising. Please start with one file and based on the feedback, we can expand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request triage:todo Needs to be traiged.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants