{"id":20791464,"url":"https://github.com/keploy/go-sdk","last_synced_at":"2025-05-05T21:21:29.945Z","repository":{"id":38186859,"uuid":"422222602","full_name":"keploy/go-sdk","owner":"keploy","description":"Official Keploy Go SDK 🔵","archived":false,"fork":false,"pushed_at":"2024-02-19T09:13:34.000Z","size":8692,"stargazers_count":45,"open_issues_count":3,"forks_count":32,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-30T23:51:06.304Z","etag":null,"topics":["data-mocking","go","golang","hacktoberfest","hacktoberfest-2023","keploy","mock","mock-server","mocking","nocode","stub-backend","stubbing","testing","testing-framework","testing-tools","vcr","vcr-go"],"latest_commit_sha":null,"homepage":"https://keploy.io","language":"Go","has_issues":false,"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/keploy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-28T13:49:40.000Z","updated_at":"2024-08-31T14:58:45.000Z","dependencies_parsed_at":"2024-06-18T15:43:35.008Z","dependency_job_id":null,"html_url":"https://github.com/keploy/go-sdk","commit_stats":null,"previous_names":[],"tags_count":63,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keploy%2Fgo-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keploy%2Fgo-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keploy%2Fgo-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keploy%2Fgo-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/keploy","download_url":"https://codeload.github.com/keploy/go-sdk/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252577116,"owners_count":21770734,"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":["data-mocking","go","golang","hacktoberfest","hacktoberfest-2023","keploy","mock","mock-server","mocking","nocode","stub-backend","stubbing","testing","testing-framework","testing-tools","vcr","vcr-go"],"created_at":"2024-11-17T15:44:46.139Z","updated_at":"2025-05-05T21:21:29.925Z","avatar_url":"https://github.com/keploy.png","language":"Go","readme":"# Keploy Go-SDK\n\nThis is the client SDK for the [Keploy](https://github.com/keploy/keploy) testing platform. You can use this to generate realistic mock/stub files for your applications.\n\n## Contents\n\n1. [Installation](#installation)\n2. [Usage](#usage)\n3. [Mocking/Stubbing for unit tests](#mockingstubbing-for-unit-tests)\n4. [Code coverage by the API tests](#code-coverage-by-the-api-tests)\n\n## Installation\n\n```bash\ngo get -u github.com/keploy/go-sdk/v2\n```\n\n## Usage\n\n### Get coverage for keploy automated tests\nThe code coverage for the keploy API tests using the `go-test` integration. \nKeploy can be integrated in your CI pipeline which can add the coverage of your keploy test. \n\n### Create mocks/stubs for your unit-test\n\nThese mocks/stubs are realistic and frees you up from writing them manually. Keploy creates `readable/editable` mocks/stubs yaml files which can be referenced in any of your unit-tests tests. An example is mentioned in [Mocking/Stubbing for unit tests](#mockingstubbing-for-unit-tests) section\n\n1. Install [keploy](https://github.com/keploy/keploy#quick-installation) binary\n2. **Record**: To record you can import the keploy mocking library and set the mode to record mode and run you databases. This should generate a file containing the mocks/stubs.\n\n```go\nimport(\n    \"github.com/keploy/go-sdk/v2/keploy\"\n)\n\n// Inside your unit test\n...\nerr := keploy.New(keploy.Config{\n\tMode: keploy.MODE_RECORD, // It can be MODE_TEST or MODE_OFF. Default is MODE_TEST. Default MODE_TEST\n    Name: \"\u003cstub_name/mock_name\u003e\" // TestSuite name to record the mock or test the mocks\n\tPath: \"\u003clocal_path_for_saving_mock\u003e\", // optional. It can be relative(./internals) or absolute(/users/xyz/...)\n\tMuteKeployLogs: false, // optional. It can be true or false. If it is true keploy logs will be not shown in the unit test terminal. Default: false\n\tdelay: 10, // by default it is 5 . This delay is for running keploy\n})\n...\n```\n\nAt the end of the test case you can add the following function which will terminate keploy if not keploy will be running even after unit test is run\n\n```go\nkeploy.KillProcessOnPort()\n```\n\n3. **Mock**: To mock dependency as per the content of the generated file (during testing) - just set the `Mode` config to `keploy.MODE_TEST` eg:\n\n```go\nerr := keploy.New(keploy.Config{\n\tMode: keploy.MODE_TEST,\n\tName: \"\u003cstub_name/mock_name\u003e\"\n\tPath: \"\u003clocal_path_for_saving_mock\u003e\",\n\tMubeKeployLogs: false,\n\tdelay: 10,\n})\n```\n\n## Mocking/Stubbing for unit tests\n\nMocks/Stubs can be generated for external dependency calls of go unit tests as `readable/editable` yaml files using Keploy.\n\n### Example\n\n```go\npackage main\n\nimport (\n\t\"bytes\"\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"net/http\"\n\t\"net/http/httptest\"\n\t\"testing\"\n\n\t\"github.com/gin-gonic/gin\"\n\t\"github.com/keploy/go-sdk/v2/keploy\"\n)\n\nfunc setup(t *testing.T) {\n\terr := keploy.New(keploy.Config{\n\t\tName:           \"TestPutURL\",\n\t\tMode:           keploy.MODE_RECORD, // change to MODE_TEST when you run in test mode\n\t\tPath:           \"/home/ubuntu/dont_touch/samples-go/gin-mongo\",\n\t\tMuteKeployLogs: false,\n\t\tDelay:          15,\n\t})\n\tif err != nil {\n\t\tt.Fatalf(\"error while running keploy: %v\", err)\n\t}\n\tdbName, collection := \"keploy\", \"url-shortener\"\n\tclient, err := New(\"localhost:27017\", dbName)\n\tif err != nil {\n\t\tpanic(\"Failed to initialize MongoDB: \" + err.Error())\n\t}\n\tdb := client.Database(dbName)\n\tcol = db.Collection(collection)\n}\n\nfunc TestPutURL(t *testing.T) {\n\n\tdefer keploy.KillProcessOnPort()\n\tsetup(t)\n\n\tr := gin.Default()\n\tr.GET(\"/:param\", getURL)\n\tr.POST(\"/url\", putURL)\n\n\tdata := map[string]string{\n\t\t\"url\": \"https://www.example.com\",\n\t}\n\tpayload, err := json.Marshal(data)\n\tif err != nil {\n\t\tt.Fatalf(\"rfe: %v\\n\", err)\n\t}\n\n\treq, err := http.NewRequest(http.MethodPost, \"/url\", bytes.NewBuffer(payload))\n\tif err != nil {\n\t\tt.Fatalf(\"Couldn't create request: %v\\n\", err)\n\t}\n\treq.Header.Set(\"Content-Type\", \"application/json\")\n\n\tw := httptest.NewRecorder()\n\tr.ServeHTTP(w, req)\n\n\t// Checking if the URL was successfully shortened and stored\n\tif w.Code != http.StatusOK {\n\t\tt.Fatalf(\"Expected HTTP 200 OK, but got %v\", w.Code)\n\t}\n\n\tvar response map[string]interface{}\n\terr = json.Unmarshal(w.Body.Bytes(), \u0026response)\n\tif err != nil {\n\t\tt.Fatalf(\"Failed to unmarshal response: %v\\n\", err)\n\t}\n\tfmt.Println(\"response-url\" + response[\"url\"].(string))\n\n\tif response[\"url\"] == nil || response[\"ts\"] == nil {\n\t\tt.Fatalf(\"Response did not contain expected fields\")\n\t}\n}\n```\n\n## Code coverage by the API tests\n\nThe percentage of code covered by the recorded tests is logged if the test cmd is ran with the go binary and `withCoverage` flag. The conditions for the coverage is:\n1. The go binary should be built with `-cover` flag.\n2. The application should have a graceful shutdown to stop the API server on `SIGTERM` or `SIGINT` signals. Or if not call the **GracefulShutdown** from the main function of your go program. Ex:\n```go\nfunc main() {\n\n\tport := \"8080\"\n\n\tr := gin.Default()\n\n\tr.GET(\"/:param\", getURL)\n\tr.POST(\"/url\", putURL)\n\t// should be called before starting the API server from main()\n\tkeploy.GracefulShutdown()\n\n\tr.Run()\n}\n```\nThe keploy test cmd will look like:\n```sh\nkeploy test -c \"PATH_TO_GO_COVER_BIANRY\" --withCoverage\n```\nThe coverage files will be stored in the directory.\n```\nkeploy\n├── coverage-reports\n│   ├── covcounters.befc2fe88a620bbd45d85aa09517b5e7.305756.1701767439933176870\n│   ├── covmeta.befc2fe88a620bbd45d85aa09517b5e7\n│   └── total-coverage.txt\n├── test-set-0\n│   ├── mocks.yaml\n│   └── tests\n│       ├── test-1.yaml\n│       ├── test-2.yaml\n│       ├── test-3.yaml\n│       └── test-4.yaml\n```\nCoverage percentage log in the cmd will be:\n```sh\n🐰 Keploy: 2023-12-07T08:53:14Z         INFO    test/test.go:261\n        test-app-url-shortener          coverage: 78.4% of statements\n```\n\nAlso the go-test coverage can be merged along the recorded tests coverage by following the steps:\n```sh\ngo test -cover ./... -args -test.gocoverdir=\"PATH_TO_UNIT_COVERAGE_FILES\"\n\ngo tool covdata textfmt -i=\"PATH_TO_UNIT_COVERAGE_FILES\",\"./keploy/coverage-reports\" -o coverage-profile\n\ngo tool cover -func coverage-profile\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeploy%2Fgo-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkeploy%2Fgo-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeploy%2Fgo-sdk/lists"}