feat: support run the specify test cases instead of all (#50)
Co-authored-by: Rick <linuxsuren@users.noreply.github.com>
This commit is contained in:
parent
45a4d5166a
commit
607818fd4b
19
cmd/run.go
19
cmd/run.go
|
@ -34,6 +34,7 @@ type runOption struct {
|
|||
report string
|
||||
reportIgnore bool
|
||||
level string
|
||||
caseItems []string
|
||||
}
|
||||
|
||||
func newDefaultRunOption() *runOption {
|
||||
|
@ -71,6 +72,7 @@ See also https://github.com/LinuxSuRen/api-testing/tree/master/sample`,
|
|||
flags.DurationVarP(&opt.duration, "duration", "", 0, "Running duration")
|
||||
flags.DurationVarP(&opt.requestTimeout, "request-timeout", "", time.Minute, "Timeout for per request")
|
||||
flags.BoolVarP(&opt.requestIgnoreError, "request-ignore-error", "", false, "Indicate if ignore the request error")
|
||||
flags.BoolVarP(&opt.reportIgnore, "report-ignore", "", false, "Indicate if ignore the report output")
|
||||
flags.Int64VarP(&opt.thread, "thread", "", 1, "Threads of the execution")
|
||||
flags.Int32VarP(&opt.qps, "qps", "", 5, "QPS")
|
||||
flags.Int32VarP(&opt.burst, "burst", "", 5, "burst")
|
||||
|
@ -91,6 +93,8 @@ func (o *runOption) preRunE(cmd *cobra.Command, args []string) (err error) {
|
|||
default:
|
||||
err = fmt.Errorf("not supported report type: '%s'", o.report)
|
||||
}
|
||||
|
||||
o.caseItems = args
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -100,7 +104,7 @@ func (o *runOption) runE(cmd *cobra.Command, args []string) (err error) {
|
|||
o.context = cmd.Context()
|
||||
o.limiter = limit.NewDefaultRateLimiter(o.qps, o.burst)
|
||||
defer func() {
|
||||
cmd.Printf("consume: %s\n", time.Now().Sub(o.startTime).String())
|
||||
cmd.Printf("consume: %s\n", time.Since(o.startTime).String())
|
||||
o.limiter.Stop()
|
||||
}()
|
||||
|
||||
|
@ -113,6 +117,10 @@ func (o *runOption) runE(cmd *cobra.Command, args []string) (err error) {
|
|||
}
|
||||
}
|
||||
|
||||
if o.reportIgnore {
|
||||
return
|
||||
}
|
||||
|
||||
// print the report
|
||||
var reportErr error
|
||||
var results runner.ReportResultSlice
|
||||
|
@ -194,6 +202,10 @@ func (o *runOption) runSuite(suite string, dataContext map[string]interface{}, c
|
|||
}
|
||||
|
||||
for _, testCase := range testSuite.Items {
|
||||
if !testCase.InScope(o.caseItems) {
|
||||
continue
|
||||
}
|
||||
|
||||
// reuse the API prefix
|
||||
if strings.HasPrefix(testCase.Request.API, "/") {
|
||||
testCase.Request.API = fmt.Sprintf("%s%s", testSuite.API, testCase.Request.API)
|
||||
|
@ -204,11 +216,6 @@ func (o *runOption) runSuite(suite string, dataContext map[string]interface{}, c
|
|||
case <-stopSingal:
|
||||
return
|
||||
default:
|
||||
// reuse the API prefix
|
||||
if strings.HasPrefix(testCase.Request.API, "/") {
|
||||
testCase.Request.API = fmt.Sprintf("%s%s", testSuite.API, testCase.Request.API)
|
||||
}
|
||||
|
||||
setRelativeDir(suite, &testCase)
|
||||
o.limiter.Accept()
|
||||
|
||||
|
|
|
@ -63,6 +63,10 @@ func TestRunSuite(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestRunCommand(t *testing.T) {
|
||||
fooPrepare := func() {
|
||||
gock.New(urlFoo).Get("/bar").Reply(http.StatusOK).JSON("{}")
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
|
@ -78,14 +82,25 @@ func TestRunCommand(t *testing.T) {
|
|||
}, {
|
||||
name: "file not found",
|
||||
args: []string{"--pattern", "fake"},
|
||||
hasErr: false,
|
||||
}, {
|
||||
name: "normal case",
|
||||
args: []string{"-p", simpleSuite},
|
||||
prepare: func() {
|
||||
gock.New(urlFoo).Get("/bar").Reply(http.StatusOK).JSON("{}")
|
||||
},
|
||||
hasErr: false,
|
||||
prepare: fooPrepare,
|
||||
}, {
|
||||
name: "report ignore",
|
||||
args: []string{"-p", simpleSuite, "--report-ignore"},
|
||||
prepare: fooPrepare,
|
||||
}, {
|
||||
name: "specify a test case",
|
||||
args: []string{"-p", simpleSuite, "fake"},
|
||||
}, {
|
||||
name: "invalid api",
|
||||
args: []string{"-p", "testdata/invalid-api.yaml"},
|
||||
hasErr: true,
|
||||
}, {
|
||||
name: "invalid schema",
|
||||
args: []string{"-p", "testdata/invalid-schema.yaml"},
|
||||
hasErr: true,
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
name: Simple
|
||||
api: "{{.api}"
|
||||
items:
|
||||
- request:
|
||||
api: /bar
|
||||
name: bar
|
|
@ -0,0 +1,6 @@
|
|||
name: Simple
|
||||
api: "{{.api}"
|
||||
item:
|
||||
- requests:
|
||||
api: /bar
|
||||
method: POSt
|
|
@ -17,6 +17,20 @@ type TestCase struct {
|
|||
Clean Clean `yaml:"clean" json:"-"`
|
||||
}
|
||||
|
||||
// InScope returns true if the test case is in scope with the given items.
|
||||
// Returns true if the items is empty.
|
||||
func (c *TestCase) InScope(items []string) bool {
|
||||
if len(items) == 0 {
|
||||
return true
|
||||
}
|
||||
for _, item := range items {
|
||||
if item == c.Name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Prepare does the prepare work
|
||||
type Prepare struct {
|
||||
Kubernetes []string `yaml:"kubernetes"`
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
package testing_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
atesting "github.com/linuxsuren/api-testing/pkg/testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestInScope(t *testing.T) {
|
||||
testCase := &atesting.TestCase{Name: "foo"}
|
||||
assert.True(t, testCase.InScope(nil))
|
||||
assert.True(t, testCase.InScope([]string{"foo"}))
|
||||
assert.False(t, testCase.InScope([]string{"bar"}))
|
||||
}
|
Loading…
Reference in New Issue