Skip to content

Commit 607818f

Browse files
authored
feat: support run the specify test cases instead of all (#50)
Co-authored-by: Rick <[email protected]>
1 parent 45a4d51 commit 607818f

File tree

6 files changed

+78
-15
lines changed

6 files changed

+78
-15
lines changed

cmd/run.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type runOption struct {
3434
report string
3535
reportIgnore bool
3636
level string
37+
caseItems []string
3738
}
3839

3940
func newDefaultRunOption() *runOption {
@@ -71,6 +72,7 @@ See also https://github.com/LinuxSuRen/api-testing/tree/master/sample`,
7172
flags.DurationVarP(&opt.duration, "duration", "", 0, "Running duration")
7273
flags.DurationVarP(&opt.requestTimeout, "request-timeout", "", time.Minute, "Timeout for per request")
7374
flags.BoolVarP(&opt.requestIgnoreError, "request-ignore-error", "", false, "Indicate if ignore the request error")
75+
flags.BoolVarP(&opt.reportIgnore, "report-ignore", "", false, "Indicate if ignore the report output")
7476
flags.Int64VarP(&opt.thread, "thread", "", 1, "Threads of the execution")
7577
flags.Int32VarP(&opt.qps, "qps", "", 5, "QPS")
7678
flags.Int32VarP(&opt.burst, "burst", "", 5, "burst")
@@ -91,6 +93,8 @@ func (o *runOption) preRunE(cmd *cobra.Command, args []string) (err error) {
9193
default:
9294
err = fmt.Errorf("not supported report type: '%s'", o.report)
9395
}
96+
97+
o.caseItems = args
9498
return
9599
}
96100

@@ -100,7 +104,7 @@ func (o *runOption) runE(cmd *cobra.Command, args []string) (err error) {
100104
o.context = cmd.Context()
101105
o.limiter = limit.NewDefaultRateLimiter(o.qps, o.burst)
102106
defer func() {
103-
cmd.Printf("consume: %s\n", time.Now().Sub(o.startTime).String())
107+
cmd.Printf("consume: %s\n", time.Since(o.startTime).String())
104108
o.limiter.Stop()
105109
}()
106110

@@ -113,6 +117,10 @@ func (o *runOption) runE(cmd *cobra.Command, args []string) (err error) {
113117
}
114118
}
115119

120+
if o.reportIgnore {
121+
return
122+
}
123+
116124
// print the report
117125
var reportErr error
118126
var results runner.ReportResultSlice
@@ -194,6 +202,10 @@ func (o *runOption) runSuite(suite string, dataContext map[string]interface{}, c
194202
}
195203

196204
for _, testCase := range testSuite.Items {
205+
if !testCase.InScope(o.caseItems) {
206+
continue
207+
}
208+
197209
// reuse the API prefix
198210
if strings.HasPrefix(testCase.Request.API, "/") {
199211
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
204216
case <-stopSingal:
205217
return
206218
default:
207-
// reuse the API prefix
208-
if strings.HasPrefix(testCase.Request.API, "/") {
209-
testCase.Request.API = fmt.Sprintf("%s%s", testSuite.API, testCase.Request.API)
210-
}
211-
212219
setRelativeDir(suite, &testCase)
213220
o.limiter.Accept()
214221

cmd/run_test.go

+24-9
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ func TestRunSuite(t *testing.T) {
6363
}
6464

6565
func TestRunCommand(t *testing.T) {
66+
fooPrepare := func() {
67+
gock.New(urlFoo).Get("/bar").Reply(http.StatusOK).JSON("{}")
68+
}
69+
6670
tests := []struct {
6771
name string
6872
args []string
@@ -76,16 +80,27 @@ func TestRunCommand(t *testing.T) {
7680
},
7781
hasErr: true,
7882
}, {
79-
name: "file not found",
80-
args: []string{"--pattern", "fake"},
81-
hasErr: false,
83+
name: "file not found",
84+
args: []string{"--pattern", "fake"},
8285
}, {
83-
name: "normal case",
84-
args: []string{"-p", simpleSuite},
85-
prepare: func() {
86-
gock.New(urlFoo).Get("/bar").Reply(http.StatusOK).JSON("{}")
87-
},
88-
hasErr: false,
86+
name: "normal case",
87+
args: []string{"-p", simpleSuite},
88+
prepare: fooPrepare,
89+
}, {
90+
name: "report ignore",
91+
args: []string{"-p", simpleSuite, "--report-ignore"},
92+
prepare: fooPrepare,
93+
}, {
94+
name: "specify a test case",
95+
args: []string{"-p", simpleSuite, "fake"},
96+
}, {
97+
name: "invalid api",
98+
args: []string{"-p", "testdata/invalid-api.yaml"},
99+
hasErr: true,
100+
}, {
101+
name: "invalid schema",
102+
args: []string{"-p", "testdata/invalid-schema.yaml"},
103+
hasErr: true,
89104
}}
90105
for _, tt := range tests {
91106
t.Run(tt.name, func(t *testing.T) {

cmd/testdata/invalid-api.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: Simple
2+
api: "{{.api}"
3+
items:
4+
- request:
5+
api: /bar
6+
name: bar

cmd/testdata/invalid-schema.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: Simple
2+
api: "{{.api}"
3+
item:
4+
- requests:
5+
api: /bar
6+
method: POSt

pkg/testing/case.go

+14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ type TestCase struct {
1717
Clean Clean `yaml:"clean" json:"-"`
1818
}
1919

20+
// InScope returns true if the test case is in scope with the given items.
21+
// Returns true if the items is empty.
22+
func (c *TestCase) InScope(items []string) bool {
23+
if len(items) == 0 {
24+
return true
25+
}
26+
for _, item := range items {
27+
if item == c.Name {
28+
return true
29+
}
30+
}
31+
return false
32+
}
33+
2034
// Prepare does the prepare work
2135
type Prepare struct {
2236
Kubernetes []string `yaml:"kubernetes"`

pkg/testing/case_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package testing_test
2+
3+
import (
4+
"testing"
5+
6+
atesting "github.com/linuxsuren/api-testing/pkg/testing"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestInScope(t *testing.T) {
11+
testCase := &atesting.TestCase{Name: "foo"}
12+
assert.True(t, testCase.InScope(nil))
13+
assert.True(t, testCase.InScope([]string{"foo"}))
14+
assert.False(t, testCase.InScope([]string{"bar"}))
15+
}

0 commit comments

Comments
 (0)