|
1 | | -# gopost |
2 | | -API Development in Go |
| 1 | +# Gostman |
| 2 | + |
| 3 | +Postman like inside [Go](https://golang.org/) testing. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +Just import Gostman to your test package. |
| 8 | + |
| 9 | +```go |
| 10 | +import github.com/injustease/gostman |
| 11 | +``` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +Always run the runtime in the `TestMain`. |
| 16 | + |
| 17 | +```go |
| 18 | +func TestMain(m *testing.M) { |
| 19 | + os.Exit(gostman.Run(m)) |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +*Optional*. Create Gostman environment file `.gostman.env.yml` if using variable. |
| 24 | + |
| 25 | +```yml |
| 26 | +myenv: |
| 27 | + var1: "variable 1" |
| 28 | + var2: "variable 2" |
| 29 | +another_env: |
| 30 | + var1: "another variable 1" |
| 31 | + var2: "another variable 2" |
| 32 | +``` |
| 33 | +
|
| 34 | +Create usual Test function `TestXxx`. |
| 35 | + |
| 36 | +```go |
| 37 | +func TestRequest(t *testing.T) { |
| 38 | + gm := gostman.New(t) |
| 39 | +
|
| 40 | + // every request run in the subtests |
| 41 | + gm.GET("Request", "url", func(r *gostman.Request) { |
| 42 | + // just code usual go code for Pre-request Script |
| 43 | +
|
| 44 | + value := gm.V("var1") // get variable |
| 45 | + gm.SetV("var2", value) // set variable |
| 46 | +
|
| 47 | + r.Params( /*sets Params here*/ ) |
| 48 | +
|
| 49 | + r.Authorization( /*sets Authorization here*/ ) |
| 50 | +
|
| 51 | + r.Headers( /*sets Headers here*/ ) |
| 52 | +
|
| 53 | + r.Body( /*sets Body here*/ ) |
| 54 | +
|
| 55 | + r.Send( /*send the request and tests the result here*/ ) |
| 56 | + }) |
| 57 | +
|
| 58 | + // create another request |
| 59 | + gm.POST("Another Request", "url", func(r *gostman.Request) {}) |
| 60 | +``` |
| 61 | + |
| 62 | +Leverage `go test` command to run the above requests. |
| 63 | + |
| 64 | +```sh |
| 65 | +go test # run all Gostman requests in the package |
| 66 | +go test -run Request # run all collection in the TestRequest |
| 67 | +go test -run Request/AnotherRequest # run only AnotherRequest |
| 68 | +go test -run Request -env myenv # run request and use "myenv" environment |
| 69 | +go test -run Request -setenv myenv # run request, use "myenv" environment and set it for the future request |
| 70 | +``` |
| 71 | + |
| 72 | +## The Runtime |
| 73 | + |
| 74 | +Gostman generate file `gostman.runtime.yml` to store runtime config and variable and change often after each request. |
| 75 | +It is **recommended** to ignore it in the `.gitignore`. |
| 76 | + |
| 77 | +```gitignore |
| 78 | +*.gostman.runtime.yml |
| 79 | +``` |
| 80 | + |
| 81 | +## Flags |
| 82 | + |
| 83 | +- All `go test` flags |
| 84 | +- `-env {env}` Select environment define in `.gostman.env.yml` |
| 85 | +- `-setenv {env}` Select environment define in `.gostman.env.yml` and set it for the future request |
| 86 | +- `-reset` Reset `.gostman.runtime.yml` |
| 87 | +- `-debug` Run gostman in debug mode |
| 88 | + |
| 89 | +## Example |
| 90 | + |
| 91 | +More in the examples folder. |
| 92 | + |
| 93 | +```go |
| 94 | +package gostman_test |
| 95 | +
|
| 96 | +import ( |
| 97 | + "encoding/json" |
| 98 | + "net/http" |
| 99 | + "net/url" |
| 100 | + "os" |
| 101 | + "testing" |
| 102 | +
|
| 103 | + "github.com/billyzaelani/is" |
| 104 | + "github.com/injustease/gostman" |
| 105 | +) |
| 106 | +
|
| 107 | +func TestMain(m *testing.M) { |
| 108 | + os.Exit(gostman.Run(m)) |
| 109 | +} |
| 110 | +
|
| 111 | +func TestRequest(t *testing.T) { |
| 112 | + gm := gostman.New(t) |
| 113 | +
|
| 114 | + gm.GET("Params", "https://postman-echo.com/get", func(r *gostman.Request) { |
| 115 | + r.Params(func(v url.Values) { |
| 116 | + v.Set("foo", "bar") |
| 117 | + }) |
| 118 | +
|
| 119 | + r.Send(func(t *testing.T, req *http.Request, res *http.Response) { |
| 120 | + defer res.Body.Close() |
| 121 | +
|
| 122 | + is := is.New(t) |
| 123 | + is.Equal(res.StatusCode, http.StatusOK) |
| 124 | +
|
| 125 | + var resp = struct { |
| 126 | + Args map[string]string `json:"args"` |
| 127 | + }{} |
| 128 | + err := json.NewDecoder(res.Body).Decode(&resp) |
| 129 | + is.NoError(err) |
| 130 | + is.Equal(resp.Args["foo"], "bar") |
| 131 | + }) |
| 132 | + }) |
| 133 | +} |
| 134 | +``` |
0 commit comments