Skip to content

Commit b7f0503

Browse files
authored
feat: improve the test case creating process on ui (#515)
Co-authored-by: rick <[email protected]>
1 parent 1d5f54f commit b7f0503

File tree

6 files changed

+88
-20
lines changed

6 files changed

+88
-20
lines changed

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ const testCaseForm = reactive({
104104
suiteName: '',
105105
name: '',
106106
api: '',
107-
method: 'GET'
107+
method: 'GET',
108+
request: {}
108109
})
109110
const rules = reactive<FormRules<Suite>>({
110111
name: [{ required: true, message: 'Please input TestCase name', trigger: 'blur' }]
@@ -118,20 +119,17 @@ Magic.Keys(openNewTestCaseDialog, ['Alt+N', 'Alt+dead'])
118119
119120
const submitForm = async (formEl: FormInstance | undefined) => {
120121
if (!formEl) return
121-
await formEl.validate((valid: boolean, fields) => {
122+
await formEl.validate((valid: boolean) => {
122123
if (valid) {
123124
suiteCreatingLoading.value = true
124125
125-
API.CreateTestCase(
126-
{
126+
API.CreateTestCase({
127127
suiteName: props.name,
128128
name: testCaseForm.name,
129-
api: testCaseForm.api,
130-
method: testCaseForm.method
131-
},
132-
() => {
129+
request: testCaseForm.request
130+
}, () => {
133131
suiteCreatingLoading.value = false
134-
emit('updated', 'hello from child')
132+
emit('updated', props.name, testCaseForm.name)
135133
}
136134
)
137135
@@ -203,6 +201,7 @@ const handleAPISelect = (item: TestCase) => {
203201
if (testCaseForm.name === '') {
204202
testCaseForm.name = item.name
205203
}
204+
testCaseForm.request = item.request
206205
}
207206
208207
function paramChange() {

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ function loadTestSuites(storeName: string) {
125125
126126
d.data[k].data.forEach((item: any) => {
127127
suite.children?.push({
128-
id: k + item,
128+
id: generateTestCaseID(k, item),
129129
label: item,
130130
store: storeName,
131131
kind: suite.kind,
@@ -139,6 +139,10 @@ function loadTestSuites(storeName: string) {
139139
}
140140
}
141141
142+
function generateTestCaseID(suiteName: string, caseName: string) {
143+
return suiteName + caseName
144+
}
145+
142146
interface Store {
143147
name: string,
144148
description: string,
@@ -147,7 +151,12 @@ interface Store {
147151
const loginDialogVisible = ref(false)
148152
const stores = ref([] as Store[])
149153
const storesLoading = ref(false)
150-
function loadStores() {
154+
function loadStores(lastSuitName?: string, lastCaseName?: string) {
155+
if (lastSuitName && lastCaseName && lastSuitName !== '' && lastCaseName !== '') {
156+
// get data from emit event
157+
Cache.SetLastTestCaseLocation(lastSuitName, generateTestCaseID(lastSuitName, lastCaseName))
158+
}
159+
151160
storesLoading.value = true
152161
const requestOptions = {
153162
headers: {
@@ -203,7 +212,7 @@ function loadStores() {
203212
treeRef.value!.setCheckedKeys([targetChild.id], false)
204213
205214
testSuite.value = targetSuite.label
206-
Cache.SetCurrentStore(targetSuite.store )
215+
Cache.SetCurrentStore(targetSuite.store)
207216
testSuiteKind.value = targetChild.kind
208217
} else {
209218
viewName.value = ""

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,7 @@ function ImportTestSuite(source: ImportSource, callback: (d: any) => void) {
178178
interface TestCase {
179179
suiteName: string
180180
name: string
181-
api: string
182-
method: string
181+
request: any
183182
}
184183

185184
function CreateTestCase(testcase: TestCase,
@@ -194,10 +193,7 @@ function CreateTestCase(testcase: TestCase,
194193
suiteName: testcase.suiteName,
195194
data: {
196195
name: testcase.name,
197-
request: {
198-
api: testcase.api,
199-
method: testcase.method
200-
}
196+
request: testcase.request
201197
}
202198
})
203199
}

pkg/runner/http.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,18 @@ func (r *simpleTestCaseRunner) GetSuggestedAPIs(suite *testing.TestSuite, api st
253253
switch param.In {
254254
case "query":
255255
// TODO should have a better way to provide the initial value
256-
(&(testcase.Request)).Query[param.Name] = "todo"
256+
(&(testcase.Request)).Query[param.Name] = generateRandomValue(param)
257257
}
258258
}
259+
testcase.Name = swagger.Paths.Paths[api].Get.ID
260+
case http.MethodPost:
261+
testcase.Name = swagger.Paths.Paths[api].Post.ID
262+
case http.MethodPut:
263+
testcase.Name = swagger.Paths.Paths[api].Put.ID
264+
case http.MethodDelete:
265+
testcase.Name = swagger.Paths.Paths[api].Delete.ID
266+
case http.MethodPatch:
267+
testcase.Name = swagger.Paths.Paths[api].Patch.ID
259268
}
260269
result = append(result, testcase)
261270
}
@@ -264,6 +273,19 @@ func (r *simpleTestCaseRunner) GetSuggestedAPIs(suite *testing.TestSuite, api st
264273
return
265274
}
266275

276+
func generateRandomValue(param spec.Parameter) interface{} {
277+
switch param.Format {
278+
case "int32", "int64":
279+
return 101
280+
case "boolean":
281+
return true
282+
case "string":
283+
return "random"
284+
default:
285+
return "random"
286+
}
287+
}
288+
267289
func (r *simpleTestCaseRunner) withResponseRecord(resp *http.Response) (responseBodyData []byte, err error) {
268290
responseBodyData, err = io.ReadAll(resp.Body)
269291
r.simpleResponse = SimpleResponse{

pkg/runner/http_test.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
_ "embed"
2828

29+
"github.com/go-openapi/spec"
2930
"github.com/h2non/gock"
3031
atest "github.com/linuxsuren/api-testing/pkg/testing"
3132
"github.com/linuxsuren/api-testing/pkg/util"
@@ -558,7 +559,6 @@ func TestGetSuggestedAPIs(t *testing.T) {
558559
assert.NotEmpty(t, result)
559560
method := result[0].Request.Method
560561
assert.Equal(t, strings.ToUpper(method), method)
561-
assert.Equal(t, "todo", result[0].Request.Query["text"])
562562
}
563563

564564
func TestIsStructContent(t *testing.T) {
@@ -589,6 +589,44 @@ func TestIsStructContent(t *testing.T) {
589589
}
590590
}
591591

592+
func TestGenerateRandomValue(t *testing.T) {
593+
tests := []struct {
594+
param spec.Parameter
595+
expected interface{}
596+
}{
597+
{
598+
param: spec.Parameter{
599+
SimpleSchema: spec.SimpleSchema{
600+
Format: "int32",
601+
},
602+
},
603+
expected: 101,
604+
}, {
605+
param: spec.Parameter{
606+
SimpleSchema: spec.SimpleSchema{
607+
Format: "boolean",
608+
},
609+
},
610+
expected: true,
611+
}, {
612+
param: spec.Parameter{
613+
SimpleSchema: spec.SimpleSchema{
614+
Format: "string",
615+
},
616+
},
617+
expected: "random",
618+
},
619+
}
620+
621+
for _, tt := range tests {
622+
result := generateRandomValue(tt.param)
623+
624+
if result != tt.expected {
625+
t.Errorf("generateRandomValue(%v) = %v, expected %v", tt.param, result, tt.expected)
626+
}
627+
}
628+
}
629+
592630
const defaultSchemaForTest = `{"properties": {
593631
"name": {"type": "string"},
594632
"age": {"type": "integer"}

pkg/runner/testdata/swagger.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
"put": {
5757
"summary": "summary",
5858
"operationId": "updateUser"
59+
},
60+
"patch": {
61+
"summary": "summary",
62+
"operationId": "patchUser"
5963
}
6064
}
6165
},

0 commit comments

Comments
 (0)