Skip to content

feat: support run the specify test cases instead of all #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"}))
}