API测试套件的动态JSON检查
我目前正在开发一个开源API测试套件,该套件与一个名为 在我的测试套件中,我运行了一系列API调用,根据预期的JSON对象/数组(或任何东西)检查响应。我认为我有一个很好的结构来比较两者,同时仍然允许响应/预期是动态的 通过允许(mustache-esque)语法表示响应的动态部分,我已经计算出了动态部分。基本上,如果a值的格式为API测试套件的动态JSON检查,json,performance,api,chai,Json,Performance,Api,Chai,我目前正在开发一个开源API测试套件,该套件与一个名为 在我的测试套件中,我运行了一系列API调用,根据预期的JSON对象/数组(或任何东西)检查响应。我认为我有一个很好的结构来比较两者,同时仍然允许响应/预期是动态的 通过允许(mustache-esque)语法表示响应的动态部分,我已经计算出了动态部分。基本上,如果a值的格式为{{content},它会相应地处理它。这里的不同值将是:非空,字符串,数组,对象,和数字 此函数的sudo代码如下所示: function (response, ex
{{content}
,它会相应地处理它。这里的不同值将是:非空
,字符串
,数组
,对象
,和数字
此函数的sudo代码如下所示:
function (response, expected) {
if expected has `{{}}` syntax {
switch syntax
check response type of against expected type of ({{type}})
set flag if failure
return
} else {
if response is the same type as expected {
switch based on type of response
case string
check if response/request are equal
set flag if failure
return
case array
check if response/request are equal
set flag if failure
return
case object
for key in object
call this function with response[key] and request[key]
} else {
set flag (failure)
return
}
}
}
我认为这将正常工作,但我想检查一下,看看在使用这个检查器时是否有任何明显的遗漏,或者是否有一种更有效的方法来执行这个相当复杂的检查
提前谢谢
更新
在检查了我的sudo代码之后,看起来我遇到了一个尴尬的检查,无法给出预期的结果。上面的功能已经更新,希望现在能正常工作。经过一点研究和尝试,这就是我想到的。它可能并不完美,但由于缺乏任何其他建议,这正是我想要的:
checkJSON: (response, expected) ->
if /{{2}(STRING|NOT_NULL|ARRAY|OBJECT|NUMBER|IGNORE)}{2}/.test(expected)
switch expected
when '{{STRING}}'
console.log 'testing string!'
unless typeof(response) is 'string'
console.log 'TYPE OF WAS SUPPOSED TO BE STRING: '+ typeof(response)
@failure = true
return
when '{{NOT_NULL}}'
console.log 'testing not null!'
unless response
console.log 'TYPE OF WAS SUPPOSED TO BE NOT_NULL: '+ typeof(response)
@failure = true
return
when '{{ARRAY}}'
console.log 'testing array!'
unless Object::toString.call(response) is '[object Array]'
console.log 'TYPE OF WAS SUPPOSED TO BE OBJECT (ARRAY): '+ typeof(response)
@failure = true
return
when '{{OBJECT}}'
console.log 'testing object!'
unless typeof(response) is 'object'
console.log 'TYPE OF WAS SUPPOSED TO BE OBJECT: '+ typeof(response)
@failure = true
return
when '{{NUMBER}}'
console.log 'testing number!'
unless typeof(response) is 'number'
console.log 'TYPE OF WAS SUPPOSED TO BE NUMBER: '+ typeof(response)
@failure = true
return
when '{{IGNORE}}'
console.log 'IGNORING THIS OUTPUT!'
#not really necessary because of regex, but good to check
else
console.log 'HOW DID YOU GET HERE?!?!?!'
@failure = true
return
else
if typeof(response) is typeof(expected)
switch typeof(response)
when 'string', 'number'
unless response is expected
console.log 'Response did not equal Expected!'
@failure = true
return
when 'object'
if Object::toString.call(response) is '[object Array]'
#compare arrays
unless response.length is expected.length
console.log response.length +' was supposed to equal '+ expected.length
@failure = true
return
#need to do more checks, but this will work for now...
#LINK: http://stackoverflow.com/questions/7837456/comparing-two-arrays-in-javascript
else
for key of response
if typeof(expected[key]) != 'undefined'
@checkJSON(response[key], expected[key])
else
console.log 'KEY WAS INCORRECT!'
@failure = true
return
else
@failure = true
return
如果有人感兴趣,上面的代码在coffeeScript中