Skip to content

Commit a3023b0

Browse files
committed
support to run test case with filter
1 parent 1d0bd60 commit a3023b0

File tree

12 files changed

+163
-6
lines changed

12 files changed

+163
-6
lines changed

cmd/run.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type runOption struct {
5151
duration time.Duration
5252
requestTimeout time.Duration
5353
requestIgnoreError bool
54+
caseFilter string
5455
thread int64
5556
context context.Context
5657
qps int32
@@ -116,6 +117,7 @@ See also https://github.com/LinuxSuRen/api-testing/tree/master/sample`,
116117
flags.DurationVarP(&opt.duration, "duration", "", 0, "Running duration")
117118
flags.DurationVarP(&opt.requestTimeout, "request-timeout", "", time.Minute, "Timeout for per request")
118119
flags.BoolVarP(&opt.requestIgnoreError, "request-ignore-error", "", false, "Indicate if ignore the request error")
120+
flags.StringVarP(&opt.caseFilter, "case-filter", "", "", "The filter of the test case")
119121
flags.StringVarP(&opt.report, "report", "", "", "The type of target report. Supported: markdown, md, html, json, discard, std, prometheus, http, grpc")
120122
flags.StringVarP(&opt.reportFile, "report-file", "", "", "The file path of the report")
121123
flags.BoolVarP(&opt.reportIgnore, "report-ignore", "", false, "Indicate if ignore the report output")
@@ -130,8 +132,10 @@ See also https://github.com/LinuxSuRen/api-testing/tree/master/sample`,
130132
return
131133
}
132134

135+
const caseFilter = "case-filter"
136+
133137
func (o *runOption) preRunE(cmd *cobra.Command, args []string) (err error) {
134-
o.context = cmd.Context()
138+
o.context = context.WithValue(cmd.Context(), caseFilter, o.caseFilter)
135139
writer := cmd.OutOrStdout()
136140

137141
if o.reportFile != "" && !strings.HasPrefix(o.reportFile, "http://") && !strings.HasPrefix(o.reportFile, "https://") {
@@ -345,8 +349,12 @@ func (o *runOption) runSuite(loader testing.Loader, dataContext map[string]inter
345349
suiteRunner.WithOutputWriter(os.Stdout)
346350
suiteRunner.WithWriteLevel(o.level)
347351
suiteRunner.WithSuite(testSuite)
348-
runLogger.Info("run test suite", "name", testSuite.Name)
352+
caseFilter := o.context.Value(caseFilter)
353+
runLogger.Info("run test suite", "name", testSuite.Name, "filter", caseFilter)
349354
for _, testCase := range testSuite.Items {
355+
if caseFilter != nil && !strings.Contains(testCase.Name, caseFilter.(string)) {
356+
continue
357+
}
350358
if !testCase.InScope(o.caseItems) {
351359
continue
352360
}

cmd/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) {
337337
mux.HandlePath(http.MethodGet, "/swagger.json", frontEndHandlerWithLocation(o.consolePath))
338338
mux.HandlePath(http.MethodGet, "/get", o.getAtestBinary)
339339
mux.HandlePath(http.MethodPost, "/runner/{suite}/{case}", service.WebRunnerHandler)
340+
mux.HandlePath(http.MethodGet, "/api/v1/sbom", service.SBomHandler)
340341

341342
postRequestProxyFunc := postRequestProxy(o.skyWalking)
342343
mux.HandlePath(http.MethodPost, "/browser/{app}", postRequestProxyFunc)

console/atest-ui/src/views/TemplateFunctions.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,8 @@ Magic.Keys(() => {
5656
</el-table-column>
5757
</el-table>
5858
</span>
59+
<div>
60+
Powered by <a href="https://masterminds.github.io/sprig/" target="_blank">Sprig</a> and <a href="https://pkg.go.dev/text/template" target="_blank">built-in templates</a>.
61+
</div>
5962
</el-dialog>
6063
</template>

console/atest-ui/src/views/WelcomePage.vue

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
<script setup lang="ts">
2+
import { ref } from 'vue'
3+
import { API } from '../views/net'
4+
5+
interface SBOM {
6+
go: {}
7+
js: {
8+
dependencies: {}
9+
devDependencies: {}
10+
}
11+
}
12+
const sbomItems = ref({} as SBOM)
13+
API.SBOM((d) => {
14+
sbomItems.value = d
15+
})
16+
</script>
17+
118
<template>
219
<div>Welcome to use atest to improve your code quality!</div>
320
<div>Please read the following guide if this is your first time to use atest.</div>
@@ -8,4 +25,31 @@
825
<div>
926
Please get more details from the <a href="https://linuxsuren.github.io/api-testing/" target="_blank" rel="noopener">official document</a>.
1027
</div>
28+
29+
<el-divider/>
30+
31+
<div>
32+
Golang dependencies:
33+
<div>
34+
<el-scrollbar height="200px" always>
35+
<li v-for="k, v in sbomItems.go">
36+
{{ v }}@{{ k }}
37+
</li>
38+
</el-scrollbar>
39+
</div>
40+
</div>
41+
42+
<div>
43+
JavaScript dependencies:
44+
<div>
45+
<el-scrollbar height="200px" always>
46+
<li v-for="k, v in sbomItems.js.dependencies">
47+
{{ v }}@{{ k }}
48+
</li>
49+
<li v-for="k, v in sbomItems.js.devDependencies">
50+
{{ v }}@{{ k }}
51+
</li>
52+
</el-scrollbar>
53+
</div>
54+
</div>
1155
</template>

console/atest-ui/src/views/net.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,11 @@ function DownloadResponseFile(testcase,
767767
.then(callback).catch(errHandle)
768768
}
769769

770+
var SBOM = (callback: (d: any) => void) => {
771+
fetch(`/api/sbom`, {})
772+
.then(DefaultResponseProcess)
773+
.then(callback)
774+
}
770775

771776
export const API = {
772777
DefaultResponseProcess,
@@ -780,6 +785,6 @@ export const API = {
780785
FunctionsQuery,
781786
GetSecrets, DeleteSecret, CreateOrUpdateSecret,
782787
GetSuggestedAPIs,
783-
ReloadMockServer, GetMockConfig,
788+
ReloadMockServer, GetMockConfig, SBOM,
784789
getToken
785790
}

console/atest-ui/ui.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ui
2+
3+
import (
4+
_ "embed"
5+
"encoding/json"
6+
)
7+
8+
//go:embed package.json
9+
var packageJSON []byte
10+
11+
type JSON struct {
12+
Dependencies map[string]string `json:"dependencies"`
13+
DevDependencies map[string]string `json:"devDependencies"`
14+
}
15+
16+
func GetPackageJSON() (data JSON) {
17+
data = JSON{}
18+
_ = json.Unmarshal(packageJSON, &data)
19+
return
20+
}

docs/site/content/zh/latest/tasks/quickstart.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ description: 只需几个简单的步骤即可开始使用 API Testing。
66

77
本指南将帮助您通过几个简单的步骤开始使用 API Testing。
88

9-
// TBD
9+
## 执行部分测试用例
10+
11+
下面的命令会执行名称中包含 `sbom` 的所有测试用例:
12+
13+
```shell
14+
atest run -p test-suite.yaml --case-filter sbom
15+
```

docs/site/content/zh/latest/tasks/verify.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,14 @@ title = "测试用例验证"
4343
```
4444
4545
[更多用法](https://expr-lang.org/docs/language-definition#indexOf).
46+
47+
## JSON 字段判断
48+
49+
```yaml
50+
- name: sbom
51+
request:
52+
api: /sbom
53+
expect:
54+
verify:
55+
- keys(data) == ["go", "js"]
56+
```

e2e/test-suite-common.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,13 @@ items:
376376
- indexOf(data, "atest_execution_success") != -1
377377
- indexOf(data, "atest_runners_count") != -1
378378
- indexOf(data, "http_requests_total") != -1
379+
380+
- name: sbom
381+
request:
382+
api: /sbom
383+
expect:
384+
verify:
385+
- keys(data) == ["go", "js"]
386+
- len(data.go) > 0
387+
- len(data.js.dependencies) > 0
388+
- len(data.js.devDependencies) > 0

pkg/runner/http.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,8 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
176176
Value: v,
177177
})
178178
}
179-
r.log.Info("request method: %s\n", request.Method)
179+
r.log.Info("start to send request to %v with method %s\n", request.URL, request.Method)
180180
r.log.Info("request header %v\n", request.Header)
181-
r.log.Info("start to send request to %v\n", request.URL)
182181

183182
// TODO only do this for unit testing, should remove it once we have a better way
184183
if strings.HasPrefix(testcase.Request.API, "http://") {

0 commit comments

Comments
 (0)