Skip to content

Commit

Permalink
feat: support run the specify test cases instead of all (#50)
Browse files Browse the repository at this point in the history
Co-authored-by: Rick <[email protected]>
  • Loading branch information
LinuxSuRen and LinuxSuRen authored Apr 23, 2023
1 parent 45a4d51 commit 607818f
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 15 deletions.
19 changes: 13 additions & 6 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type runOption struct {
report string
reportIgnore bool
level string
caseItems []string
}

func newDefaultRunOption() *runOption {
Expand Down Expand Up @@ -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")
Expand All @@ -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
}

Expand All @@ -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()
}()

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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()

Expand Down
33 changes: 24 additions & 9 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -76,16 +80,27 @@ func TestRunCommand(t *testing.T) {
},
hasErr: true,
}, {
name: "file not found",
args: []string{"--pattern", "fake"},
hasErr: false,
name: "file not found",
args: []string{"--pattern", "fake"},
}, {
name: "normal case",
args: []string{"-p", simpleSuite},
prepare: func() {
gock.New(urlFoo).Get("/bar").Reply(http.StatusOK).JSON("{}")
},
hasErr: false,
name: "normal case",
args: []string{"-p", simpleSuite},
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) {
Expand Down
6 changes: 6 additions & 0 deletions cmd/testdata/invalid-api.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: Simple
api: "{{.api}"
items:
- request:
api: /bar
name: bar
6 changes: 6 additions & 0 deletions cmd/testdata/invalid-schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: Simple
api: "{{.api}"
item:
- requests:
api: /bar
method: POSt
14 changes: 14 additions & 0 deletions pkg/testing/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
15 changes: 15 additions & 0 deletions pkg/testing/case_test.go
Original file line number Diff line number Diff line change
@@ -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"}))
}

0 comments on commit 607818f

Please sign in to comment.