Skip to content

Commit 5c55a7e

Browse files
authored
feat: adds integration for the go-test coverage (#200)
Signed-off-by: re-Tick <[email protected]>
1 parent efcc23c commit 5c55a7e

File tree

2 files changed

+70
-327
lines changed

2 files changed

+70
-327
lines changed

β€ŽREADME.md

+60-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This is the client SDK for the [Keploy](https://github.com/keploy/keploy) testin
77
1. [Installation](#installation)
88
2. [Usage](#usage)
99
3. [Mocking/Stubbing for unit tests](#mockingstubbing-for-unit-tests)
10+
4. [Code coverage by the API tests](#code-coverage-by-the-api-tests)
1011

1112
## Installation
1213

@@ -16,6 +17,10 @@ go get -u github.com/keploy/go-sdk/v2
1617

1718
## Usage
1819

20+
### Get coverage for keploy automated tests
21+
The code coverage for the keploy API tests using the `go-test` integration.
22+
Keploy can be integrated in your CI pipeline which can add the coverage of your keploy test.
23+
1924
### Create mocks/stubs for your unit-test
2025

2126
These 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
@@ -32,7 +37,7 @@ import(
3237
...
3338
err := keploy.New(keploy.Config{
3439
Mode: keploy.MODE_RECORD, // It can be MODE_TEST or MODE_OFF. Default is MODE_TEST. Default MODE_TEST
35-
Name: "<stub_name/mock_name>" // TestSuite name to record the mock or test the mocks
40+
Name: "<stub_name/mock_name>" // TestSuite name to record the mock or test the mocks
3641
Path: "<local_path_for_saving_mock>", // optional. It can be relative(./internals) or absolute(/users/xyz/...)
3742
MuteKeployLogs: 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
3843
delay: 10, // by default it is 5 . This delay is for running keploy
@@ -142,3 +147,57 @@ func TestPutURL(t *testing.T) {
142147
}
143148
}
144149
```
150+
151+
## Code coverage by the API tests
152+
153+
The 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:
154+
1. The go binary should be built with `-cover` flag.
155+
2. 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:
156+
```go
157+
func main() {
158+
159+
port := "8080"
160+
161+
r := gin.Default()
162+
163+
r.GET("/:param", getURL)
164+
r.POST("/url", putURL)
165+
// should be called before starting the API server from main()
166+
keploy.GracefulShutdown()
167+
168+
r.Run()
169+
}
170+
```
171+
The keploy test cmd will look like:
172+
```sh
173+
keploy test -c "PATH_TO_GO_COVER_BIANRY" --withCoverage
174+
```
175+
The coverage files will be stored in the directory.
176+
```
177+
keploy
178+
β”œβ”€β”€ coverage-reports
179+
β”‚ β”œβ”€β”€ covcounters.befc2fe88a620bbd45d85aa09517b5e7.305756.1701767439933176870
180+
β”‚ β”œβ”€β”€ covmeta.befc2fe88a620bbd45d85aa09517b5e7
181+
β”‚ └── total-coverage.txt
182+
β”œβ”€β”€ test-set-0
183+
β”‚ β”œβ”€β”€ mocks.yaml
184+
β”‚ └── tests
185+
β”‚ β”œβ”€β”€ test-1.yaml
186+
β”‚ β”œβ”€β”€ test-2.yaml
187+
β”‚ β”œβ”€β”€ test-3.yaml
188+
β”‚ └── test-4.yaml
189+
```
190+
Coverage percentage log in the cmd will be:
191+
```sh
192+
🐰 Keploy: 2023-12-07T08:53:14Z INFO test/test.go:261
193+
test-app-url-shortener coverage: 78.4% of statements
194+
```
195+
196+
Also the go-test coverage can be merged along the recorded tests coverage by following the steps:
197+
```sh
198+
go test -cover ./... -args -test.gocoverdir="PATH_TO_UNIT_COVERAGE_FILES"
199+
200+
go tool covdata textfmt -i="PATH_TO_UNIT_COVERAGE_FILES","./keploy/coverage-reports" -o coverage-profile
201+
202+
go tool cover -func coverage-profile
203+
```

0 commit comments

Comments
Β (0)