{"id":37178934,"url":"https://github.com/johnfarmers/go-unit-tester","last_synced_at":"2026-01-14T20:50:44.956Z","repository":{"id":241859179,"uuid":"807972317","full_name":"JohnFarmers/go-unit-tester","owner":"JohnFarmers","description":"Go Unit Tester is a small Golang library that provided a utility functions for unit testing.","archived":false,"fork":false,"pushed_at":"2024-06-05T07:46:38.000Z","size":37,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-06-19T04:07:36.114Z","etag":null,"topics":["go","golang","golang-library","golang-package","unit-test","unit-testing","unittest","util","utilities","utility","utility-library","utils","utils-lib"],"latest_commit_sha":null,"homepage":"","language":"Go","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/JohnFarmers.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2024-05-30T06:15:43.000Z","updated_at":"2024-06-12T06:41:43.000Z","dependencies_parsed_at":"2024-06-19T04:12:16.080Z","dependency_job_id":null,"html_url":"https://github.com/JohnFarmers/go-unit-tester","commit_stats":null,"previous_names":["johnfarmers/go-unit-tester"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/JohnFarmers/go-unit-tester","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnFarmers%2Fgo-unit-tester","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnFarmers%2Fgo-unit-tester/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnFarmers%2Fgo-unit-tester/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnFarmers%2Fgo-unit-tester/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JohnFarmers","download_url":"https://codeload.github.com/JohnFarmers/go-unit-tester/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnFarmers%2Fgo-unit-tester/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28434500,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T18:57:19.464Z","status":"ssl_error","status_checked_at":"2026-01-14T18:52:48.501Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["go","golang","golang-library","golang-package","unit-test","unit-testing","unittest","util","utilities","utility","utility-library","utils","utils-lib"],"created_at":"2026-01-14T20:50:44.308Z","updated_at":"2026-01-14T20:50:44.930Z","avatar_url":"https://github.com/JohnFarmers.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GO Unit Tester\r\n\r\nGo Unit Tester is a small Golang library that provided a utility functions for unit testing.\r\n\r\n## Usages\r\n\r\n### Setting up:\r\nRun the following command to install the module.\r\n\r\n```sh\r\ngo get github.com/JohnFarmers/go-unit-tester\r\n```\r\n\r\n#### Example:\r\n\r\nLet's say you have project structure like this(be sure to also create `unittester/unittester.go` file as shown below):\r\n\r\n```\r\n| go-example-project\r\n|   \u003e unittester\r\n|       - unittester.go\r\n|   \u003e mathUtil\r\n|       - mathUtil.go\r\n|   main.go\r\n```\r\n\r\nIf your project have a file `mathUtil.go` with the following methods:\r\n\r\n```go\r\npackage mathUtil\r\n\r\nfunc Add(a int, b int) int {\r\n\treturn a + b\r\n}\r\n\r\nfunc Subtract(a int, b int) int {\r\n\treturn a - b\r\n}\r\n\r\nfunc Multiply(a int, b int) int {\r\n\treturn a * b\r\n}\r\n```\r\n\r\nIn `unittester.go` file you will have to create `inti()` function, this function will automatically be call once before the `main()` function. Be sure to import the module `\"github.com/JohnFarmers/go-unit-tester\"` first and then you can put your unit test code here.\r\n\r\n```go\r\npackage unittester\r\n\r\nimport (\r\n    test \"github.com/JohnFarmers/go-unit-tester\"\r\n    mth \"go-example-project/mathUtil\"\r\n)\r\n\r\nfunc init() {\r\n\t// You can perform unit test like this.\r\n\ttest.UnitTest(mth.Add, []interface{}{5}, []interface{}{2, 3}, false, true)\r\n\ttest.UnitTest(mth.Subtract, []interface{}{2}, []interface{}{10, 8}, false, true)\r\n\ttest.UnitTest(mth.Multiply, []interface{}{10}, []interface{}{5, 2}, false, true)\r\n}\r\n```\r\n\r\nLastly, in order for the `init()` function to be call, all you have to do is go to your `main.go` and import the package that the `init()` is in:\r\n\r\n```go\r\npackage main\r\n\r\nimport (\r\n\t_ \"go-example-project/unittester\"\r\n)\r\n\r\nfunc main() {\r\n    //Your code here.\r\n}\r\n```\r\n\r\nNote: the `_` before the package path is needed. If you don't include the `_`, the imported package will be remove when you save the file.\r\n\r\nOnce everything is done, you can use the command to run your project and the unit test code should be running before your `main()`.\r\n\r\n```sh\r\ngo run .\r\n```\r\n\r\n### Method definitions:\r\n\r\n#### UnitTest\r\n\r\n`UnitTest(function interface{}, expected []interface{}, params []interface{}, checkOutputTypeOnly bool, detailedPassLog bool) bool`\r\n\r\n#### Description\r\nPerform a unit test on the given function by checking if the real outputs is the same as the expected outputs or not and print out error if any unexpected outputs/behavior occured.\r\n\r\n#### Parameters\r\n\r\n| Name | Type | Description |      \r\n| ------------- |------| ------------- |\r\n| `function` | `interface{}` | The function to preform a unit test on. |\r\n| `expected` | `[]interface{}` | The expected outputs of the function. |\r\n| `params` | `[]interface{}` | The inputs of the function. |\r\n| `checkOutputTypeOnly` | `bool` | Determine whether or not to check the type of the output only and ignore it's values. If `true`, when checking the output, it will only check if the type of the outputs match the expected outputs. If `false`, it will check both type and value. |\r\n| `detailedPassLog` | `bool` | Determine whether or not to print the full outputs of the tested function if the test is pass. |\r\n\r\n#### Return types: `bool`\r\n\r\n#### Example:\r\n\r\n```go\r\nimport (\r\n    test \"github.com/JohnFarmers/go-unit-tester\"\r\n)\r\n\r\n// If you want to unit test this function.\r\nfunc Add(a int, b int) int {\r\n    return a + b\r\n}\r\n\r\n// You can do so like this.\r\ntest.UnitTest(Add, []interface{}{5}, []interface{}{2, 3}, false, true)\r\n```\r\n\r\nThe function call above perform a unit test on function `Add(a int, b int)` with `5` as an expected output and also with `2` and `3` as an inputs.\r\n\r\n#### UnitTestWithMultipleOutputCase\r\n\r\n`UnitTestWithMultipleOutputCase(function interface{}, expectedOutputs [][]interface{}, params []interface{}, checkOutputTypeOnly bool, detailedPassLog bool) bool`\r\n\r\n#### Description\r\nPerform a unit test on the given function by checking if the real outputs is the same as the expected outputs or not and print out error if any unexpected outputs/behavior occured.\r\n\r\nThis method is for checking a function that can have multiple output cases.\r\n\r\n#### Parameters\r\n\r\n| Name | Type | Description |      \r\n| ------------- |------| ------------- |\r\n| `function` | `interface{}` | The function to preform a unit test on. |\r\n| `expected` | `[][]interface{}` | The expected outputs of the function in each cases. |\r\n| `params` | `[]interface{}` | The inputs of the function. |\r\n| `checkOutputTypeOnly` | `bool` | Determine whether or not to check the type of the output only and ignore it's values. If `true`, when checking the output, it will only check if the type of the outputs match the expected outputs. If `false`, it will check both type and value. |\r\n| `detailedPassLog` | `bool` | Determine whether or not to print the full outputs of the tested function if the test is pass. |\r\n\r\n#### Return types: `bool`\r\n\r\n#### Example:\r\n\r\n```go\r\nimport (\r\n\t\"math/rand/v2\"\r\n\ttest \"github.com/JohnFarmers/go-unit-tester\"\r\n)\r\n\r\n// If you want to unit test this function.\r\nfunc RandNum() int {\r\n    return rand.IntN(3)\r\n}\r\n\r\n// You can do so like this.\r\ntest.UnitTestWithMultipleOutputCase(\r\n    RandNum,\r\n    [][]interface{}{\r\n        []interface{}{0}, \r\n        []interface{}{1}, \r\n        []interface{}{2},\r\n    },\r\n    []interface{}{},\r\n    false,\r\n    true,\r\n)\r\n```\r\n\r\nSince the function `RandNum()` above can output either 0, 1, or 2. We can have multiple expected outputs, so that we can check for all of them.\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnfarmers%2Fgo-unit-tester","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjohnfarmers%2Fgo-unit-tester","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnfarmers%2Fgo-unit-tester/lists"}