feat: support to print the JSON schema of the TestSuite struct (#31)
This commit is contained in:
parent
578e64bac9
commit
a55b86b51e
|
@ -0,0 +1,25 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/invopop/jsonschema"
|
||||
"github.com/linuxsuren/api-testing/pkg/testing"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func createJSONSchemaCmd() (c *cobra.Command) {
|
||||
c = &cobra.Command{
|
||||
Use: "json",
|
||||
Short: "Print the JSON schema of the test suites struct",
|
||||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||
var data []byte
|
||||
schema := jsonschema.Reflect(&testing.TestSuite{})
|
||||
if data, err = json.MarshalIndent(schema, "", " "); err == nil {
|
||||
cmd.Println(string(data))
|
||||
}
|
||||
return
|
||||
},
|
||||
}
|
||||
return
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package cmd_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/linuxsuren/api-testing/cmd"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestJSONSchemaCmd(t *testing.T) {
|
||||
c := cmd.NewRootCmd()
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
c.SetOut(buf)
|
||||
|
||||
c.SetArgs([]string{"json"})
|
||||
err := c.Execute()
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, strings.Contains(buf.String(), "schema"))
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/linuxsuren/api-testing/pkg/version"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
@ -11,9 +13,10 @@ func NewRootCmd() (c *cobra.Command) {
|
|||
Use: "atest",
|
||||
Short: "API testing tool",
|
||||
}
|
||||
c.SetOut(os.Stdout)
|
||||
c.Version = version.GetVersion()
|
||||
c.AddCommand(createInitCommand(),
|
||||
createRunCommand(), createSampleCmd(),
|
||||
createServerCmd())
|
||||
createServerCmd(), createJSONSchemaCmd())
|
||||
return
|
||||
}
|
||||
|
|
2
go.mod
2
go.mod
|
@ -24,8 +24,10 @@ require (
|
|||
github.com/google/uuid v1.3.0 // indirect
|
||||
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
|
||||
github.com/huandu/xstrings v1.3.3 // indirect
|
||||
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 // indirect
|
||||
github.com/imdario/mergo v0.3.11 // indirect
|
||||
github.com/inconshreveable/mousetrap v1.0.1 // indirect
|
||||
github.com/invopop/jsonschema v0.7.0 // indirect
|
||||
github.com/mitchellh/copystructure v1.0.0 // indirect
|
||||
github.com/mitchellh/reflectwalk v1.0.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
|
|
5
go.sum
5
go.sum
|
@ -543,6 +543,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
|
|||
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||
github.com/huandu/xstrings v1.3.3 h1:/Gcsuc1x8JVbJ9/rlye4xZnVAbEkGauT8lbebqcQws4=
|
||||
github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
|
||||
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 h1:i462o439ZjprVSFSZLZxcsoAe592sZB1rci2Z8j4wdk=
|
||||
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
|
||||
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
|
||||
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
|
||||
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
|
||||
|
@ -550,6 +552,8 @@ github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA=
|
|||
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
||||
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
|
||||
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/invopop/jsonschema v0.7.0 h1:2vgQcBz1n256N+FpX3Jq7Y17AjYt46Ig3zIWyy770So=
|
||||
github.com/invopop/jsonschema v0.7.0/go.mod h1:O9uiLokuu0+MGFlyiaqtWxwqJm41/+8Nj0lD7A36YH0=
|
||||
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
|
||||
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
|
@ -598,6 +602,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
|
|||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
|
|
|
@ -2,19 +2,19 @@ package testing
|
|||
|
||||
// TestSuite represents a set of test cases
|
||||
type TestSuite struct {
|
||||
Name string `yaml:"name"`
|
||||
API string `yaml:"api"`
|
||||
Items []TestCase `yaml:"items"`
|
||||
Name string `yaml:"name" json:"name"`
|
||||
API string `yaml:"api,omitempty" json:"api,omitempty"`
|
||||
Items []TestCase `yaml:"items" json:"items"`
|
||||
}
|
||||
|
||||
// TestCase represents a test case
|
||||
type TestCase struct {
|
||||
Name string
|
||||
Name string `yaml:"name" json:"name"`
|
||||
Group string
|
||||
Prepare Prepare `yaml:"prepare"`
|
||||
Request Request `yaml:"request"`
|
||||
Expect Response `yaml:"expect"`
|
||||
Clean Clean `yaml:"clean"`
|
||||
Prepare Prepare `yaml:"prepare" json:"-"`
|
||||
Request Request `yaml:"request" json:"request"`
|
||||
Expect Response `yaml:"expect" json:"expect"`
|
||||
Clean Clean `yaml:"clean" json:"-"`
|
||||
}
|
||||
|
||||
// Prepare does the prepare work
|
||||
|
@ -24,22 +24,22 @@ type Prepare struct {
|
|||
|
||||
// Request represents a HTTP request
|
||||
type Request struct {
|
||||
API string `yaml:"api"`
|
||||
Method string `yaml:"method"`
|
||||
Query map[string]string `yaml:"query"`
|
||||
Header map[string]string `yaml:"header"`
|
||||
Form map[string]string `yaml:"form"`
|
||||
Body string `yaml:"body"`
|
||||
BodyFromFile string `yaml:"bodyFromFile"`
|
||||
API string `yaml:"api" json:"api"`
|
||||
Method string `yaml:"method,omitempty" json:"method,omitempty" jsonschema:"enum=GET,enum=POST,enum=PUT,enum=DELETE"`
|
||||
Query map[string]string `yaml:"query" json:"query,omitempty"`
|
||||
Header map[string]string `yaml:"header" json:"header,omitempty"`
|
||||
Form map[string]string `yaml:"form" json:"form,omitempty"`
|
||||
Body string `yaml:"body" json:"body,omitempty"`
|
||||
BodyFromFile string `yaml:"bodyFromFile" json:"bodyFromFile,omitempty"`
|
||||
}
|
||||
|
||||
// Response is the expected response
|
||||
type Response struct {
|
||||
StatusCode int `yaml:"statusCode"`
|
||||
Body string `yaml:"body"`
|
||||
Header map[string]string `yaml:"header"`
|
||||
BodyFieldsExpect map[string]interface{} `yaml:"bodyFieldsExpect"`
|
||||
Verify []string `yaml:"verify"`
|
||||
StatusCode int `yaml:"statusCode" json:"bodyFromFile,omitempty"`
|
||||
Body string `yaml:"body" json:"body,omitempty"`
|
||||
Header map[string]string `yaml:"header" json:"header,omitempty"`
|
||||
BodyFieldsExpect map[string]interface{} `yaml:"bodyFieldsExpect" json:"bodyFieldsExpect,omitempty"`
|
||||
Verify []string `yaml:"verify" json:"verify,omitempty"`
|
||||
}
|
||||
|
||||
// Clean represents the clean work after testing
|
||||
|
|
Loading…
Reference in New Issue