https://github.com/ing-bank/gintestutil
Utilities for writing unit-tests with Gin
https://github.com/ing-bank/gintestutil
context gin go golang test testing unit utilities utility
Last synced: 2 months ago
JSON representation
Utilities for writing unit-tests with Gin
- Host: GitHub
- URL: https://github.com/ing-bank/gintestutil
- Owner: ing-bank
- License: mit
- Created: 2023-03-23T10:50:49.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-03-16T14:51:56.000Z (over 2 years ago)
- Last Synced: 2025-05-30T14:51:13.624Z (about 1 year ago)
- Topics: context, gin, go, golang, test, testing, unit, utilities, utility
- Language: Go
- Homepage: https://pkg.go.dev/github.com/ing-bank/gintestutil
- Size: 28.3 KB
- Stars: 1
- Watchers: 7
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🦁 Gin Test Utils
[](https://github.com/ing-bank/gintestutil/actions/workflows/test.yaml)


Small utility functions for testing Gin-related code.
Such as the creation of a gin context and wait groups with callbacks.
## ⬇️ Installation
`go get github.com/ing-bank/gintestutil`
## 📋 Usage
### Context creation
```go
package main
import (
"net/http"
"testing"
"github.com/ing-bank/gintestutil"
)
type TestObject struct {
Name string
}
func TestProductController_Post_CreatesProducts(t *testing.T) {
// Arrange
context, writer := gintestutil.PrepareRequest(t,
gintestutil.WithJsonBody(t, TestObject{Name: "test"}),
gintestutil.WithMethod(http.MethodPost),
gintestutil.WithUrl("https://my-website.com"),
gintestutil.WithHeaders(http.Header{"X-Example": []string{"A", "B"}}),
gintestutil.WithUrlParams(map[string]any{"category": "barbecue"}),
gintestutil.WithQueryParams(map[string]any{"force": "true"}))
// [...]
}
```
### Response Assertions
```go
package main
import (
"github.com/stretchr/testify/assert"
"net/http"
"testing"
"github.com/ing-bank/gintestutil"
)
type TestObject struct {
Name string
}
func TestProductController_Index_ReturnsAllProducts(t *testing.T) {
// Arrange
context, writer := gintestutil.PrepareRequest(t)
// [...]
// Assert
var actual []TestObject
if gintestutil.Response(t, &actual, http.StatusOK, writer.Result()) {
assert.Equal(t, []TestObject{}, actual)
}
}
```
### Hooks
```go
package main
import (
"github.com/gin-gonic/gin"
"github.com/ing-bank/gintestutil"
"net/http"
"net/http/httptest"
"time"
"testing"
)
func TestHelloController(t *testing.T) {
// Arrange
ginContext := gin.Default()
// create expectation
expectation := gintestutil.ExpectCalled(t, ginContext, "/hello-world")
ginContext.GET("/hello-world", func(context *gin.Context) {
context.Status(http.StatusOK)
})
// create webserver
ts := httptest.NewServer(ginContext)
// Send request to webserver path
_, _ = http.Get(fmt.Sprintf("%s/hello-world", ts.URL))
// Wait for expectation in bounded time
if ok := gintestutil.EnsureCompletion(t, expectation); !ok {
// do something
}
}
```
## 🚀 Development
1. Clone the repository
2. Run `make tools` to install necessary tools
3. Run `make t` to run unit tests
4. Run `make fmt` to format code
4. Run `make lint` to lint your code
You can run `make` to see a list of useful commands.
## 🔭 Future Plans
Nothing here yet!