{"id":16677474,"url":"https://github.com/foresterre/parameterized","last_synced_at":"2025-04-07T08:19:11.712Z","repository":{"id":35970246,"uuid":"181768735","full_name":"foresterre/parameterized","owner":"foresterre","description":"🐑 JUnit style parameterized testing for Rust","archived":false,"fork":false,"pushed_at":"2024-12-10T22:28:22.000Z","size":142,"stargazers_count":24,"open_issues_count":9,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-06T05:58:19.990Z","etag":null,"topics":["hacktoberfest","parameterized","parametrized","rust","test","unit"],"latest_commit_sha":null,"homepage":"https://docs.rs/parameterized","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/foresterre.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["foresterre"],"buy_me_a_coffee":"foresterre","thanks_dev":"u/gh/foresterre"}},"created_at":"2019-04-16T21:18:52.000Z","updated_at":"2024-12-10T22:28:23.000Z","dependencies_parsed_at":"2024-04-30T19:28:52.018Z","dependency_job_id":"20ca8511-77b3-47d9-8984-87f2c7f9e54c","html_url":"https://github.com/foresterre/parameterized","commit_stats":{"total_commits":78,"total_committers":3,"mean_commits":26.0,"dds":0.0641025641025641,"last_synced_commit":"0aba078a226c2d6ba04679c059478a4ba12111ad"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foresterre%2Fparameterized","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foresterre%2Fparameterized/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foresterre%2Fparameterized/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foresterre%2Fparameterized/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/foresterre","download_url":"https://codeload.github.com/foresterre/parameterized/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247615385,"owners_count":20967184,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest","parameterized","parametrized","rust","test","unit"],"created_at":"2024-10-12T13:26:33.643Z","updated_at":"2025-04-07T08:19:11.680Z","avatar_url":"https://github.com/foresterre.png","language":"Rust","readme":"# parameterized\n\nProcedural macro based parameterized testing library.\nUseful, when you want to run a test case with many different input sets.\n\nWhen defining a parameterized test case, the `#[parameterized(...)]` attribute should be used instead of `#[test]`.\n\nThis crate was inspired by JUnit `@ParameterizedTest`.\n\nIf you consider using parameterized, you can also check out [Yare](https://github.com/foresterre/yare) which is a\nvariation on `parameterized`, which pivots the parameters, so you can define your own identifier for cases.\nAlternatively, there is [Sif](https://github.com/foresterre/sif) where each case can be defined by a\nseparate `#[case(...)`] attribute.\n\n### Example:\n\nAdditional examples can be found at the \u003ca href=\"https://github.com/foresterre/parameterized-examples\"\u003e\nparameterized-examples repository\u003c/a\u003e,\nand in the \u003ca href=\"parameterized-macro/tests\"\u003etests\u003c/a\u003e folder.\n\n```rust\nenum Fruit {\n    Apple,\n    Bramble(BrambleFruit),\n    Pear,\n}\n\ntrait NameOf {\n    fn name_of(\u0026self) -\u003e \u0026str;\n}\n\nimpl NameOf for Fruit {\n    fn name_of(\u0026self) -\u003e \u0026str {\n        match self {\n            Fruit::Apple =\u003e \"apple\",\n            Fruit::Bramble(fruit) =\u003e fruit.name_of(),\n            Fruit::Pear =\u003e \"pear\",\n        }\n    }\n}\n\nenum BrambleFruit {\n    Blackberry,\n}\n\nimpl NameOf for BrambleFruit {\n    fn name_of(\u0026self) -\u003e \u0026str {\n        match self {\n            BrambleFruit::Blackberry =\u003e \"blackberry\",\n        }\n    }\n}\n\n#[cfg(test)]\nmod tests {\n    use super::*;\n    use parameterized::parameterized;\n\n\n    #[parameterized(fruit = {\n        Fruit::Apple, Fruit::Pear, Fruit::Bramble(BrambleFruit::Blackberry)\n    }, name = {\n        \"apple\", \"pear\", \"blackberry\"\n    })]\n    fn a_fruity_test(fruit: Fruit, name: \u0026str) {\n        assert_eq!(fruit.name_of(), name)\n    }\n}\n```\n\n### Custom test attributes (e.g. tokio::test)\n\nBy default, the code generation step of the `parameterized` attribute will generate test cases marked with a `#[test]`\nattribute.\nFor example, for the parameterized test case **add5** below, the following code would be generated:\n\n**Parameterized test case**\n\n```rust\nuse parameterized::parameterized;\n\n#[parameterized(input = {\n    0, 1\n}, expected = {\n    5, 6\n})]\nfn add5(input: u32, expected: u32) {\n    assert_eq!(input + 5, expected);\n}\n```\n\n**Generated code**\n\n```rust\n#[cfg(test)]\nmod add5 {\n    use super::*;\n\n    #[test]\n    fn case_0() {\n        assert_eq!(0 + 5, 5);\n    }\n\n    #[test]\n    fn case_1() {\n        assert_eq!(1 + 5, 6);\n    }\n}\n```\n\nHowever, sometimes a different test macro is desired, for example with `#[tokio::test]`.\nTo let `#[parameterized]` use a user specified test macro, you may add the `#[parameterized_macro(...)]` attribute after\na `#[parameterized]` attribute.\nSince we use `#[tokio::test]` in this example, we also add the `async` item to the function signature (but this is of\ncourse not mandatory for other macros).\n\n**Parameterized test case with `#[parameterized_macro(...)]`**\n\n```rust,ignore\n#[parameterized(input = {\n    0, 1\n}, expected = {\n    5, 6\n})]\n#[parameterized_macro(tokio::test)]\nasync fn add5(input: u32, expected: u32) {\n    assert_eq!(input + 5, expected);\n}\n```\n\nGotchas:\n\n* The `#[parameterized_macro(...)]` must always be specified after a `#[parameterized(...)]` attribute\n* For now, only one `#[parameterized_macro(...)]` attribute per parameterized test function is supported.\n* While you can rename the parameterized attribute using import renaming (\n  e.g. `use parameterized::parameterized as pm`),\n  the `parameterized_macro` attribute cannot be renamed, since it's not actually defined as a separate macro.\n  Instead, the `parameterized` parses this attribute as well.\n\n### Imports\n\nIf you prefer not to import this library (with `use parameterized::parameterized;`) in every test module, you can put\nthe following snippet at the top of your crate root:\n\n```rust\n#[cfg(test)]\n#[macro_use]\nextern crate parameterized;\n```\n\n\u003cbr\u003e\n\n### IDE 'run test' intent\n\nIntelliJ IDEA recognizes test cases and provides context menus which allow you to run tests within a certain scope\n(such as a module or a single test case). For example, in IntelliJ you can usually run individual test cases by clicking\nthe ▶ icon in the gutter. Unfortunately, attribute macros are currently not expanded by `intellij-rust`.\nThis means that the IDE will not recognize test cases generated as a result of attribute macros (such as the\n`parameterized` macro published by this crate).\n\nA workaround can be found below (if you have a better solution, please feel free to open an issue; thank you in\nadvance!)\n\n```rust\nfn squared(input: i8) -\u003e i8 {\n    input * input\n}\n\n#[cfg(test)]\nmod tests {\n    use super::*;\n\n    use parameterized::parameterized as pm;\n    use parameterized::ide;\n        \n    mod squared_tests { // \u003c--\n        use super::*;\n\n        ide!(); // \u003c--\n    \n        #[pm(input = {\n            -2, -1, 0, 1, 2\n        }, expected = {\n            4, 1, 0, 1, 4\n        })]\n        fn test_squared(input: i8, output: i8) {\n            assert_eq(squared(input), output);\n        }\n    }\n}\n```\n\nHere we created an empty test case (using the `ide!()` macro) which will mark the surrounding module as 'containing test\ncases'. In\nthe gutter you will find the ▶ icon next to the module. This allows you to run test cases per module.\n\nNote: `intellij-rust` does expand declarative macro's (with the new macro engine which can be\nselected in the 'settings' menu), such as this `ide!` macro.\n\n\u003cbr\u003e\n\n### License\n\nLicensed under either of \u003ca href=\"LICENSE-APACHE\"\u003eApache License, Version\n2.0\u003c/a\u003e or \u003ca href=\"LICENSE-MIT\"\u003eMIT license\u003c/a\u003e at your option.\n\n\u003csub\u003e\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in this crate by you, as defined in the Apache-2.0 license, shall\nbe dual licensed as above, without any additional terms or conditions.\u003c/sub\u003e\n","funding_links":["https://github.com/sponsors/foresterre","https://buymeacoffee.com/foresterre","https://thanks.dev/u/gh/foresterre"],"categories":["\u003ca name=\"Rust\"\u003e\u003c/a\u003eRust"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fforesterre%2Fparameterized","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fforesterre%2Fparameterized","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fforesterre%2Fparameterized/lists"}