Skip to content

Commit 6edcec4

Browse files
committed
自动化回归测试:新增对通过颜色对对比结果分级
1 parent 1c7f711 commit 6edcec4

File tree

3 files changed

+136
-3
lines changed

3 files changed

+136
-3
lines changed

apijson/JSONResponse.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,125 @@ var JSONResponse = {
160160
fullName = fullName.substring(index + 1);
161161
}
162162
return fullName;
163+
},
164+
165+
166+
167+
COMPARE_NO_STANDARD: -1,
168+
COMPARE_EQUAL: 0,
169+
COMPARE_KEY_MORE: 1,
170+
COMPARE_VALUE_CHANGE: 2,
171+
COMPARE_KEY_LESS: 3,
172+
COMPARE_TYPE_CHANGE: 4,
173+
COMPARE_NUMBER_TYPE_CHANGE: 3,
174+
COMPARE_CODE_CHANGE: 4,
175+
176+
/**测试compare: 对比 新的请求与上次请求的结果
177+
0-相同,无颜色;
178+
1-对象新增字段或数组新增值,绿色;
179+
2-值改变,蓝色;
180+
3-对象缺少字段/整数变小数,黄色;
181+
4-类型/code改变,红色;
182+
*/
183+
compareResponse: function(target, real) {
184+
if (target == null || target.code == null) {
185+
return JSONResponse.COMPARE_NO_STANDARD; //未上传对比标准(正确的结果)
186+
}
187+
if (target.code != real.code) {
188+
return JSONResponse.COMPARE_CODE_CHANGE;
189+
}
190+
191+
delete target.code;
192+
delete real.code;
193+
194+
delete target.message;
195+
delete real.message;
196+
197+
return JSONResponse.compare(target, real);
198+
},
199+
200+
/**测试compare: 对比 新的请求与上次请求的结果
201+
0-相同,无颜色;
202+
1-对象新增字段或数组新增值,绿色;
203+
2-值改变,蓝色;
204+
3-对象缺少字段/整数变小数,黄色;
205+
4-类型/success/errCode改变,红色;
206+
*/
207+
compare: function(target, real) {
208+
if (target == null) {
209+
return real == null ? JSONResponse.COMPARE_EQUAL : JSONResponse.COMPARE_KEY_MORE;
210+
}
211+
if (real == null) { //少了key
212+
return JSONResponse.COMPARE_KEY_LESS;
213+
}
214+
215+
var type = typeof target;
216+
if (type != typeof real) { //类型改变
217+
return JSONResponse.COMPARE_TYPE_CHANGE;
218+
}
219+
220+
var max = JSONResponse.COMPARE_EQUAL;
221+
var each = JSONResponse.COMPARE_EQUAL;
222+
if (target instanceof Array) { // JSONArray
223+
if (target.length != real.length) {
224+
max = JSONResponse.COMPARE_VALUE_CHANGE;
225+
}
226+
227+
var length = Math.min(target.length, real.length); //多或少都不影响
228+
229+
//TODO 需要把所有值合并,得到最全的,至于类型,Java是很稳定的,同样的key在所有Object的type都相同。
230+
for (var i = 0; i < length; i++) { //遍历并递归下一层
231+
each = JSONResponse.compare(target[i], real[i]);
232+
if (max < each) {
233+
max = each;
234+
}
235+
if (max >= JSONResponse.COMPARE_TYPE_CHANGE) {
236+
break;
237+
}
238+
}
239+
}
240+
else if (target instanceof Object) { // JSONObject
241+
var tks = Object.keys(target);
242+
var key;
243+
for (var i = 0; i< tks.length; i++) { //遍历并递归下一层
244+
key = tks[i];
245+
if (key == null) {
246+
continue;
247+
}
248+
249+
each = JSONResponse.compare(target[key], real[key]);
250+
if (max < each) {
251+
max = each;
252+
}
253+
if (max >= JSONResponse.COMPARE_TYPE_CHANGE) {
254+
break;
255+
}
256+
}
257+
258+
259+
if (max < JSONResponse.COMPARE_KEY_MORE) { //多出key
260+
for (var k in real) {
261+
if (k != null && tks.indexOf(k) < 0) {
262+
max = JSONResponse.COMPARE_KEY_MORE;
263+
}
264+
}
265+
}
266+
}
267+
else { // Boolean, Number, String
268+
if (type == 'number') { //数字类型由整数变为小数
269+
if (String(target).indexOf('.') < 0 && String(real).indexOf('.') >= 0) {
270+
return JSONResponse.COMPARE_NUMBER_TYPE_CHANGE;
271+
}
272+
}
273+
274+
if (target !== real) { //值不同
275+
return JSONResponse.COMPARE_VALUE_CHANGE;
276+
}
277+
}
278+
279+
return max;
163280
}
164281

165282

283+
166284
}

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
<ul v-show="isRemoteShow" class="historys" style="width: 100%;height: 100%;overflow: hidden;overflow-y: scroll;padding-bottom: 50px">
9696
<li v-for="(item, index) in remotes" >
9797
<a href="javascript:void(0)" @click="restore(item)" > {{(item.version > 0 ? 'V' + item.version : 'V*') + ' ' + item.name + ' ' + item.url}}</a>
98-
<div v-show="item.compare != null && item.compare > 0" style="position: absolute;right: 30px;display: inline-block;">
98+
<div :style="{ background: item.compareColor }" v-show="item.compare != null && item.compare > 0" style="position: absolute;right: 30px;display: inline-block;">
9999
<button @click="handleTest(false, index, item)" style="position: relative;color: red">错误,已解决</button>
100100
<svg class="icon" style="position: relative;margin-left: 10px" @click="downloadTest(index, item)">
101101
<use xlink:href="svg/icon.svg#export-txt"></use>

js/main.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
remotes: [],
132132
tests: [],
133133
testProcess: '',
134+
compareColor: '#0000',
134135
isDelayShow: false,
135136
isSaveShow: false,
136137
isExportShow: false,
@@ -1183,7 +1184,7 @@
11831184
1-对象新增字段或数组新增值,绿色;
11841185
2-值改变,蓝色;
11851186
3-对象缺少字段,黄色;
1186-
4-类型改变,红色;
1187+
4-类型/success/errCode改变,红色;
11871188
*/
11881189
test: function () {
11891190
var baseUrl = App.getBaseUrl() || ''
@@ -1230,14 +1231,28 @@
12301231
response = JSON.stringify(res.data || {})
12311232

12321233
var it = item || {} //请求异步
1233-
it.compare = it.response == response ? 0 : 4
1234+
it.compare = JSONResponse.compareResponse(it.response == null ? null : JSON.parse(it.response), res.data)
12341235
console.log('i = ' + index + '; item.name = ' + it.name + '; item.compare = ' + it.compare)
12351236
// if (it.compare > 0) {
12361237
// alert('i = ' + index + '; item.name = ' + it.name + '; item.compare = ' + it.compare
12371238
// + '; it.response = \n' + it.response
12381239
// + '\n\n\n res.data = \n' + response
12391240
// )
12401241
// }
1242+
switch (it.compare) {
1243+
case JSONResponse.COMPARE_KEY_MORE:
1244+
it.compareColor = 'green'
1245+
break;
1246+
case JSONResponse.COMPARE_VALUE_CHANGE:
1247+
it.compareColor = 'blue'
1248+
break;
1249+
case JSONResponse.COMPARE_KEY_LESS:
1250+
it.compareColor = 'yellow'
1251+
break;
1252+
case JSONResponse.COMPARE_TYPE_CHANGE:
1253+
it.compareColor = 'red'
1254+
break;
1255+
}
12411256

12421257
App.testProcess = index <= allCount ? '' : '正在测试: ' + index + '/' + allCount
12431258

0 commit comments

Comments
 (0)