Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/minizilla/gostman
Postman nuances in Go Test
https://github.com/minizilla/gostman
api api-development go gostman postman testing
Last synced: 3 months ago
JSON representation
Postman nuances in Go Test
- Host: GitHub
- URL: https://github.com/minizilla/gostman
- Owner: minizilla
- License: mit
- Created: 2021-06-22T10:47:23.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-11-18T06:18:18.000Z (about 1 year ago)
- Last Synced: 2024-09-30T08:04:09.901Z (3 months ago)
- Topics: api, api-development, go, gostman, postman, testing
- Language: Go
- Homepage:
- Size: 28.3 KB
- Stars: 13
- Watchers: 3
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gostman [![Go Reference](https://pkg.go.dev/badge/github.com/minizilla/gostman.svg)](https://pkg.go.dev/github.com/minizilla/gostman) [![main](https://github.com/minizilla/gostman/actions/workflows/main.yaml/badge.svg)](https://github.com/minizilla/gostman/actions/workflows/main.yaml)
[Postman](https://www.postman.com/) nuances in [Go](https://golang.org/) Test.
## Install
Just import gostman to your test package.
```go
import github.com/minizilla/gostman
```## Usage
Always run the runtime in the `TestMain`.
```go
func TestMain(m *testing.M) {
os.Exit(gostman.Run(m))
}
```*Optional*. Create gostman environment file `.gostman.env.yml` if using variable.
```yml
myenv:
var1: "variable 1"
var2: "variable 2"
another_env:
var1: "another variable 1"
var2: "another variable 2"
```Create usual Test function `TestXxx`.
```go
func TestRequest(t *testing.T) {
gm := gostman.New(t)// every request run in the subtests
gm.GET("Request", "url", func(r *gostman.Request) {
// just code usual go code for Pre-request Scriptvalue := gm.V("var1") // get variable
gm.SetV("var2", value) // set variabler.Params( /*sets Params here*/ )
r.Authorization( /*sets Authorization here*/ )
r.Headers( /*sets Headers here*/ )
r.Body( /*sets Body here*/ )
r.Send( /*send the request and tests the result here*/ )
})// create another request
gm.POST("Another Request", "url", func(r *gostman.Request) {})
```Leverage `go test` command to run the above requests.
```sh
go test # run all gostman requests in the package
go test -run Request # run all collection in the TestRequest
go test -run Request/AnotherRequest # run only AnotherRequest
go test -run Request -env myenv # run request and use "myenv" environment
go test -run Request -setenv myenv # run request, use "myenv" environment and set it for the future request
```## The Runtime
Gostman generate file `.gostman.runtime.yml` to store runtime config and variable and change often after each request.
It is **recommended** to ignore it in the `.gitignore`.```gitignore
*.gostman.runtime.yml
```## Flags
- All `go test` flags
- `-env {env}` Select environment define in `.gostman.env.yml`
- `-setenv {env}` Select environment define in `.gostman.env.yml` and set it for the future request
- `-reset` Reset `.gostman.runtime.yml`
- `-debug` Run gostman in debug mode## Example
More in the examples folder.
```go
package gostman_testimport (
"encoding/json"
"net/http"
"net/url"
"os"
"testing""github.com/minizilla/gostman"
"github.com/minizilla/testr"
)func TestMain(m *testing.M) {
os.Exit(gostman.Run(m))
}func TestRequest(t *testing.T) {
gm := gostman.New(t)gm.GET("Params", "https://postman-echo.com/get", func(r *gostman.Request) {
r.Params(func(v url.Values) {
v.Set("foo", "bar")
})r.Send(func(t *testing.T, req *http.Request, res *http.Response) {
defer res.Body.Close()assert := testr.New(t)
assert.Equal(res.StatusCode, http.StatusOK)var resp = struct {
Args map[string]string `json:"args"`
}{}
err := json.NewDecoder(res.Body).Decode(&resp)
assert.ErrorIs(err, nil)
assert.Equal(resp.Args["foo"], "bar")
})
})
}
```