{"id":25776860,"url":"https://github.com/mikemybytes/junit5-formatted-source","last_synced_at":"2025-02-27T06:06:58.914Z","repository":{"id":50228297,"uuid":"514033298","full_name":"mikemybytes/junit5-formatted-source","owner":"mikemybytes","description":"Format-driven parameterized tests for JUnit 5","archived":false,"fork":false,"pushed_at":"2024-04-29T23:25:26.000Z","size":151,"stargazers_count":65,"open_issues_count":5,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-04-30T00:31:55.446Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mikemybytes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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}},"created_at":"2022-07-14T20:04:59.000Z","updated_at":"2024-03-31T14:25:57.000Z","dependencies_parsed_at":"2024-02-03T11:26:55.224Z","dependency_job_id":"e46165ba-a620-4e3e-8d27-d0183e713428","html_url":"https://github.com/mikemybytes/junit5-formatted-source","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikemybytes%2Fjunit5-formatted-source","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikemybytes%2Fjunit5-formatted-source/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikemybytes%2Fjunit5-formatted-source/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikemybytes%2Fjunit5-formatted-source/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mikemybytes","download_url":"https://codeload.github.com/mikemybytes/junit5-formatted-source/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240987435,"owners_count":19889335,"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":[],"created_at":"2025-02-27T06:01:32.465Z","updated_at":"2025-02-27T06:06:58.907Z","avatar_url":"https://github.com/mikemybytes.png","language":"Java","funding_links":[],"categories":["测试"],"sub_categories":[],"readme":"# JUnit 5 FormattedSource\n\n![](https://img.shields.io/github/license/mikemybytes/junit5-formatted-source)\n![](https://img.shields.io/github/v/release/mikemybytes/junit5-formatted-source)\n![](https://img.shields.io/maven-central/v/com.mikemybytes/junit5-formatted-source)\n![](https://img.shields.io/github/actions/workflow/status/mikemybytes/junit5-formatted-source/build.yml)\n\nThis library extends [JUnit 5](https://github.com/junit-team/junit5) with a new way of writing [parameterized tests](https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests).\nIt allows defining test case arguments in a human-readable way, following a user-defined format. Additionally, it automatically\ntakes care of the test case names, so the input definition is also what will be presented in the test execution output.\n\n```java\nclass CalculatorTest {\n    \n    private final Calculator calculator = new Calculator();\n\n    @FormattedSourceTest(format = \"{0} + {1} = {2}\", lines = {\n            \"1 + 2 = 3\",\n            \"3 + 4 = 7\"\n    })\n    void calculatesSum(int a, int b, int expectedSum) {\n        Assertions.assertEquals(expectedSum, calculator.sum(a, b));\n    }\n    \n}\n```\n\nOutput:\n```\ncalculatesSum(int, int, int) ✔\n├─ 1 + 2 = 3 ✔\n└─ 3 + 4 = 7 ✔\n```\n\nOf course, _JUnit 5 FormattedSource_ can do even more!\n\n## Installing\n\n### Requirements\n- Java 11+\n- JUnit 5.8.0+\n\n_Note: The library does not introduce any dependencies other than the JUnit 5._\n\n### Maven\n\n```xml\n    \u003cdependency\u003e\n        \u003cgroupId\u003ecom.mikemybytes\u003c/groupId\u003e\n        \u003cartifactId\u003ejunit5-formatted-source\u003c/artifactId\u003e\n        \u003cversion\u003e1.0.1\u003c/version\u003e\n        \u003cscope\u003etest\u003c/scope\u003e\n    \u003c/dependency\u003e\n```\n\n### Gradle\n\n```groovy\n    testImplementation \"com.mikemybytes:junit5-formatted-source:1.0.1\"\n```\n\n### Java Platform Module System (JPMS)\n\nJava module name: `com.mikemybytes.junit5.formatted` ([descriptor](junit5-formatted-source/src/main/java/module-info.java), \n[example usage](junit5-formatted-source-tests/src/test/java/module-info.java))\n\n## User Guide\n\nDetails and usage examples can be found in the project's [User Guide](docs/user-guide.md).\n\n## Yet another argument source?\n\nThe project has been inspired by the built-in [`@CsvSource` annotation](https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests-sources-CsvSource),\nwhich allows writing not only [data table tests](https://mikemybytes.com/2021/10/19/parameterize-like-a-pro-with-junit-5-csvsource/)\nbut also specification-like test case definitions:\n\n```java\nclass CsvSourceSpecificationTest {\n    \n    @ParameterizedTest(name = \"{0} maps to {1}\")\n    @CsvSource(delimiterString = \"maps to\", textBlock = \"\"\"\n        'foo' maps to 'bar'\n        'junit' maps to 'jupiter'\n        \"\"\")\n    void mapsOneValueToAnother(String input, String expectedValue) {\n        // ...\n    }\n    \n}\n```\n\nYet, the `@CsvSource` limits the user to only one `delimiterString`, which effectively means supporting \nonly two arguments at a time. Additionally, selected delimiter must be repeated within the `@ParameterizedTest`'s `name`\nparameter in order to appear in the test execution output.\n\nUsing `@FormattedSource` allows to forget about these limitations and write less code:\n\n```java\nclass FormattedSourceSpecificationTest {\n    \n    @FormattedSourceTest(format = \"{0} maps to {1} using rule {2}\", textBlock = \"\"\"\n        'foo' maps to 'bar' using rule 486\n        'junit' maps to 'jupiter' using rule 44\n        \"\"\")\n    void mapsOneValueToAnother(String input, String expectedValue, int expectedRuleId) {\n        // ...\n    }\n    \n}\n```\n\nTest case names are automatically generated based on provided specification:\n```\nmapsOneValueToAnother(String, String, int) ✔\n├─ 'foo' maps to 'bar' using rule 486 ✔\n└─ 'junit' maps to 'jupiter' using rule 44 ✔\n```\n\n## `@FormattedSourceTest` vs `@FormattedSource`\n\nThe library comes with two annotations. `@FormattedSource` is just a standard [JUnit 5 argument source](https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests-sources) \nthat has to be combined with `@ParameterizedTest`. It also does not influence generated test case names automatically:\n\n```java\nclass DurationEncodingTest {\n    \n    @ParameterizedTest(name = \"encodes {0} seconds as {1}\")\n    @FormattedSource(format = \"encodes {0} seconds as {1}\", lines = {\n            \"encodes 15 seconds as 'PT15S'\",\n            \"encodes 180 seconds as 'PT3M'\",\n            \"encodes 172800 seconds as 'PT48H'\"\n    })\n    void encodesDurationAsIso8601(long seconds, String expected) {\n        // ...\n    }\n    \n}\n```\n\nThe equivalent `@FormattedSourceTest` simply results in a less verbose code:\n```java\nclass DurationEncodingShorterTest {\n    \n    // @ParameterizedTest already included (with test case name!)\n    @FormattedSourceTest(format = \"encodes {0} seconds as {1}\", lines = {\n            \"encodes 15 seconds as 'PT15S'\",\n            \"encodes 180 seconds as 'PT3M'\",\n            \"encodes 172800 seconds as 'PT48H'\"\n    })\n    void encodesDurationAsIso8601(long seconds, String expected) {\n        // ...\n    }\n    \n}\n```\n\n## Building from source\n\nThe project comes with [Maven Wrapper](https://maven.apache.org/wrapper/), so it can be built even without Maven\ninstalled locally. There's no need to pass any additional properties.\n\nThe minimum Java version required to build the project is Java 17. Produced artifacts will be binary-compatible\nwith Java 11+.\n\n### Build\n\n```\n./mvnw clean verify\n```\n\n### Build \u0026 install\n\n```\n./mvnw clean install\n```\n\n### License\n\nThe project is distributed under the [MIT license](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikemybytes%2Fjunit5-formatted-source","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmikemybytes%2Fjunit5-formatted-source","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikemybytes%2Fjunit5-formatted-source/lists"}