Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: about 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 (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-03-16T14:51:56.000Z (9 months ago)
- Last Synced: 2024-03-25T20:55:34.045Z (9 months 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: 6
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🦁 Gin Test Utils
[![Go package](https://github.com/ing-bank/gintestutil/actions/workflows/test.yaml/badge.svg)](https://github.com/ing-bank/gintestutil/actions/workflows/test.yaml)
![GitHub](https://img.shields.io/github/license/ing-bank/gintestutil)
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/ing-bank/gintestutil)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 mainimport (
"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 mainimport (
"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 mainimport (
"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 codeYou can run `make` to see a list of useful commands.
## 🔭 Future Plans
Nothing here yet!