Last active
May 11, 2016 14:13
-
-
Save formigone/410778976e01e0be3f572b43f331589b to your computer and use it in GitHub Desktop.
A simple set of utility functions to help you perform assertions on JSON output from a JMeter stress test - **no plugins required**
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var testUtil = { | |
/** | |
* Fails the assertion with the message provided. | |
*/ | |
failWith: function (msg) { | |
AssertionResult.setFailure(true); | |
AssertionResult.setFailureMessage(msg); | |
}, | |
isArray: function (obj) { | |
return obj.__proto__.constructor.name === 'Array'; | |
}, | |
isObject: function (obj) { | |
return obj.__proto__.constructor.name === 'Object'; | |
}, | |
/** | |
* If the assertion provided is FALSY, the test assertion fails with the message provided | |
*/ | |
failIf: function (falseAssertion, msg) { | |
if (falseAssertion) { | |
this.failWith(msg || JSON.stringify(falseAssertion)); | |
} | |
}, | |
/** | |
* Compares a map of { key: 'Type' } against some target object | |
* | |
* @param { Object<string, string> } shape For example, { name: 'String', age: 'Number' } | |
* @param { Object } target The object to be diffed against the provided shape | |
* @return { Object|null } If the output is null, that means the target object has the shape provided, and possibly more attributes | |
*/ | |
diffShape: function (shape, target) { | |
var mismatch = null; | |
Object.keys(shape).some(function (key) { | |
if (!target[key]) { | |
return mismatch = { | |
key: key, | |
expected: shape[key], | |
actual: String(target[key]) | |
}; | |
} | |
var targetType = target[key].__proto__.constructor.name; | |
if (targetType !== shape[key]) { | |
return mismatch = { | |
key: key, | |
expected: shape[key], | |
actual: targetType | |
}; | |
} | |
}); | |
return mismatch; | |
} | |
}; | |
// Example: | |
var apiResult = { | |
users: [ | |
{ | |
id: 23, | |
name: 'Jack', | |
age: 35, | |
friends: ['Kate', 'Sawyer'] | |
}, | |
{ | |
id: 42, | |
name: 'Jacob', | |
age: 1035, | |
friends: [] | |
} | |
] | |
} | |
var userShape = { | |
id: 'Number', | |
name: 'String', | |
age: 'Number', | |
friends: 'Array' | |
}; | |
// Usage: | |
testUtil.isArray(apiResult); | |
// > false | |
testUtil.isArray(apiResult.users); | |
// > true | |
testUtil.diffShape({users: 'Array'}, apiResult); | |
// > null | |
// apiResult has at least a "users" property, which is an Array | |
testUtil.diffShape({users: 'Array', characters: 'Array'}, apiResult); | |
// > {key: "characters", expected: "Array", actual: "undefined"} | |
// apiResult does not have an attribute of "characters", which we expected to be an Array | |
testUtil.failIf( | |
testUtil.diffShape(userShape, apiResult) | |
); | |
// > assertion passes, since diffShapes passes FALSY to failIf - which only fails if you send in TRUTH |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment