Heartily agree! Smart Assertions are cool and I want to like them but not advanced enough and need improving.
Maybe if there was an export and import would make it easier to edit the valid values (e.g. find and replace Property Expansions)?
Here are some the Groovy script work arounds we use, if of any use?
//Assert Date comparison
expectedDateString = "2023-01-16T18:43:34.3210487"
returnedDateString = "16/01/2023"
expectedDate = Date.parse("yyyy-MM-dd'T'HH:mm:ss.SSSSSSS", expectedDateString)
returnedDate = new Date().parse("dd/MM/yyyy", returnedDateString)
assert expectedDate > returnedDate
assert expectedDate < returnedDate.plus(365)
//Assert Mapping comparison
expectedMapValue = "Hazel"
returnedMapValue = "Ha"
def genderMap = [
Hazel:'Ha',
Brown:'Br',
Blue:'Bl',
Green:'Gr'
]
assert genderMap.get(expectedMapValue) == returnedMapValue
//Assert RegEx comparison
expectedRegExValue = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+[.][a-zA-Z]{2,}\$"
returnedRegExValue = "no-reply@abc.co.uk"
assert returnedRegExValue =~ expectedRegExValue
//Assert conditional comparison
expectedValue = "123"
dependantValue = true
if (dependantValue == true) {
assert expectedValue == "123"
} else {
assert expectedValue == "ABC"
}
//Assert Contains
expectedListValues = ["animal", "vegetable", "mineral"]
returnedListValue = "animal"
assert expectedListValues.contains(returnedListValue)
//Out of order simple array
expectedArray = ['FET','SOL']
returnedArray = ['SOL','FET']
returnedArraySorted = returnedArray.sort()
assert expectedArray == returnedArraySorted
//Out of order object array
import groovy.json.*
returnedObjArrayString = """
{
"user_identifier" : [
{
"value" : "10000571",
"id_type" : {
"value" : "BARCODE",
}
},
{
"value" : "atzejr-dnkuifa",
"id_type" : {
"value" : "OTHER_ID_1",
}
}
]
}"""
returnedObjArray = new JsonSlurper().setType(JsonParserType.LAX).parseText(returnedObjArrayString)
//directAccessAValue = returnedObjArray.user_identifier[0].value
for (row in returnedObjArray.user_identifier) {
if (row.id_type.value == "BARCODE") {
assert row.value == "10000571"
}
if (row.id_type.value == "OTHER_ID_1") {
assert row.value == "atzejr-dnkuifa"
}
}
We also have a Groovy script that's a for-loop in a for-loop, for when we can auto match the sent JSON to the returned JSON and assert the values.
Related Content
- 10 years ago