|
| 1 | +--- |
| 2 | +title: Assertion reference |
| 3 | +--- |
| 4 | + |
| 5 | +:::note |
| 6 | +- This page should be use as reference for asserts used in testcase specification files. |
| 7 | +- This page is subject to change. It is requested to check this page frequently. |
| 8 | +- Currently JSON response is only supported type for assertions. |
| 9 | +::: |
| 10 | + |
| 11 | +Assertions are integral part of [Testcase spec.](/references/testcase-reference) Following are the assertion keys supported in most latest version of Testcase spec. |
| 12 | + |
| 13 | +Assertions are used to validate data in testcase specification after http request execution has successfully returned the response. |
| 14 | + |
| 15 | +It supports a list of assertions to be supplied. Each of the list item consists of assertion object. Assertion object have following components: |
| 16 | + |
| 17 | +- `type` of assertion to be executed. |
| 18 | +- `actual` what to assert, usually a local variable or one of it's node. |
| 19 | +- `expected` what is expect value, for some `types` this is not required. |
| 20 | + |
| 21 | +e.g. |
| 22 | + |
| 23 | +```yaml |
| 24 | +... |
| 25 | +spec: |
| 26 | + asserts: |
| 27 | + - {type: AssertEqual, actual: $Response.code, expected: 201} |
| 28 | + |
| 29 | + - type: assertIsMap |
| 30 | + actual: $Response |
| 31 | +``` |
| 32 | +
|
| 33 | +
|
| 34 | +
|
| 35 | +## Assertions |
| 36 | +
|
| 37 | +Following are currently supported assertions. Variable or values supplied in the `actual` field must contain JSON. |
| 38 | + |
| 39 | +[TBD] will support other data interchange format. |
| 40 | + |
| 41 | +### AssertEqual |
| 42 | + |
| 43 | +Checks if `actual` and `expected` is equal. |
| 44 | + |
| 45 | +```yaml |
| 46 | +spec: |
| 47 | + ... |
| 48 | + asserts: |
| 49 | + - {type: AssertEqual, actual: $Response.code, expected: 201} |
| 50 | +``` |
| 51 | + |
| 52 | +### AssertNotEqual |
| 53 | + |
| 54 | +Checks if `actual` and `expected` is not equal. |
| 55 | + |
| 56 | +```yaml |
| 57 | +spec: |
| 58 | + ... |
| 59 | + asserts: |
| 60 | + - {type: AssertNotEqual, actual: $Response.code, expected: 200} |
| 61 | +``` |
| 62 | + |
| 63 | +### AssertEmpty |
| 64 | + |
| 65 | +Checks if `actual` is empty. |
| 66 | + |
| 67 | +```yaml |
| 68 | +spec: |
| 69 | + ... |
| 70 | + asserts: |
| 71 | + - {type: AssertEmpty, actual: $Response.varOne} |
| 72 | +``` |
| 73 | + |
| 74 | +### AssertFalse |
| 75 | + |
| 76 | +Checks if `actual` is _False_. |
| 77 | + |
| 78 | +```yaml |
| 79 | +spec: |
| 80 | + ... |
| 81 | + asserts: |
| 82 | + - {type: AssertFalse, actual: $Response.var_2} |
| 83 | +``` |
| 84 | + |
| 85 | +### AssertTrue |
| 86 | + |
| 87 | +Checks if `actual` is _True_. |
| 88 | + |
| 89 | +```yaml |
| 90 | +spec: |
| 91 | + ... |
| 92 | + asserts: |
| 93 | + - {type: AssertTrue, actual: $Response.varOne} |
| 94 | +``` |
| 95 | + |
| 96 | +### AssertIsInt |
| 97 | + |
| 98 | +Checks if `actual` is of type integer. |
| 99 | + |
| 100 | +```yaml |
| 101 | +spec: |
| 102 | + ... |
| 103 | + asserts: |
| 104 | + - {type: AssertIsInt, actual: $Response.varOne} |
| 105 | +``` |
| 106 | + |
| 107 | +### AssertIsString |
| 108 | + |
| 109 | +Checks if `actual` is of type string. |
| 110 | + |
| 111 | +```yaml |
| 112 | +spec: |
| 113 | + ... |
| 114 | + asserts: |
| 115 | + - {type: AssertIsString, actual: $Response.varOne} |
| 116 | +``` |
| 117 | + |
| 118 | +### AssertIsFloat |
| 119 | + |
| 120 | +Checks if `actual` is of type floating point. |
| 121 | + |
| 122 | +```yaml |
| 123 | +spec: |
| 124 | + ... |
| 125 | + asserts: |
| 126 | + - {type: AssertIsFloat, actual: $Response.varOne} |
| 127 | +``` |
| 128 | + |
| 129 | +### AssertIsBool |
| 130 | + |
| 131 | +Checks if `actual` is of type boolean, meaning either _True_ or _False_. |
| 132 | + |
| 133 | +```yaml |
| 134 | +spec: |
| 135 | + ... |
| 136 | + asserts: |
| 137 | + - {type: AssertIsBool, actual: $Response.varOne} |
| 138 | +``` |
| 139 | + |
| 140 | +### AssertCount |
| 141 | + |
| 142 | +Checks if `actual` is countable, and have the number of elements given in `expected`. One particular use-case is to test short list of paginated items. Let's say a JSON node `articles` should contains a list of 5 items, then someone would write that as: |
| 143 | + |
| 144 | +```yaml |
| 145 | +spec: |
| 146 | + ... |
| 147 | + asserts: |
| 148 | + - type: AssertCount |
| 149 | + actual: $Response.articles |
| 150 | + expected: 5 |
| 151 | +``` |
| 152 | + |
| 153 | +`AssertCount` also can be use to check a JSON object have given number of keys, as well. |
| 154 | + |
| 155 | +### AssertGreater |
| 156 | + |
| 157 | +Checks if `actual` is only greater than `expected` value. Integer, floating point, decimal values can be compared. |
| 158 | + |
| 159 | +```yaml |
| 160 | +spec: |
| 161 | + ... |
| 162 | + asserts: |
| 163 | + - type: AssertGreater |
| 164 | + actual: $Price |
| 165 | + expected: 500 |
| 166 | +``` |
| 167 | + |
| 168 | +### AssertGreaterOrEqual |
| 169 | + |
| 170 | +Checks if `actual` is greater than or equal to `expected` value. Integer, floating point, decimal values can be compared. |
| 171 | + |
| 172 | +```yaml |
| 173 | +spec: |
| 174 | + ... |
| 175 | + asserts: |
| 176 | + - type: AssertGreaterOrEqual |
| 177 | + actual: $Price |
| 178 | + expected: 500 |
| 179 | +``` |
| 180 | + |
| 181 | +### AssertLess |
| 182 | + |
| 183 | +Checks if `actual` is only less than `expected` value. Integer, floating point, decimal values can be compared. |
| 184 | + |
| 185 | +```yaml |
| 186 | +spec: |
| 187 | + ... |
| 188 | + asserts: |
| 189 | + - type: AssertLess |
| 190 | + actual: $Price |
| 191 | + expected: 500 |
| 192 | +``` |
| 193 | + |
| 194 | +### AssertLessOrEqual |
| 195 | + |
| 196 | +Checks if `actual` is less than or equal to `expected` value. Integer, floating point, decimal values can be compared. |
| 197 | + |
| 198 | +```yaml |
| 199 | +spec: |
| 200 | + ... |
| 201 | + asserts: |
| 202 | + - type: AssertLessOrEqual |
| 203 | + actual: $Price |
| 204 | + expected: 500 |
| 205 | +``` |
| 206 | + |
| 207 | +### AssertStrContains |
| 208 | + |
| 209 | +Checks if data given in `actual` is a string. If so, does it contains the substring given in `expected`. |
| 210 | + |
| 211 | +```yaml |
| 212 | +spec: |
| 213 | + ... |
| 214 | + asserts: |
| 215 | + - type: AssertStrContains |
| 216 | + actual: "testing is great for code" |
| 217 | + expected: 'code' |
| 218 | +``` |
| 219 | + |
| 220 | +### AssertIsList |
| 221 | + |
| 222 | +Checks if `actual` is of type dictionary. Usually, JSON lists are list. |
| 223 | + |
| 224 | +```yaml |
| 225 | +spec: |
| 226 | + ... |
| 227 | + asserts: |
| 228 | + - {type: AssertIsList, actual: $Response} |
| 229 | +``` |
| 230 | + |
| 231 | +### AssertListContains |
| 232 | + |
| 233 | +Checks if data given in `actual` is a list. If so, does it contains the value given in `expected`. |
| 234 | + |
| 235 | +```yaml |
| 236 | +spec: |
| 237 | + ... |
| 238 | + asserts: |
| 239 | + - type: AssertListContains |
| 240 | + actual: $Countries |
| 241 | + expected: 'GB' |
| 242 | +
|
| 243 | + - type: AssertListContains |
| 244 | + actual: $Currency |
| 245 | + expected: {"country": "GB", "currency": "GBP"} |
| 246 | +``` |
| 247 | + |
| 248 | +### AssertListHasIndex |
| 249 | + |
| 250 | +Checks if data given in `actual` is a list. If so, does it contains `expected` index. |
| 251 | + |
| 252 | +```yaml |
| 253 | +spec: |
| 254 | + ... |
| 255 | + asserts: |
| 256 | + - type: AssertListHasIndex |
| 257 | + actual: $Articles |
| 258 | + expected: 6 |
| 259 | +``` |
| 260 | + |
| 261 | +### AssertIsMap |
| 262 | + |
| 263 | +Checks if `actual` is of type dictionary. Usually, JSON objects are dictionary. |
| 264 | + |
| 265 | +```yaml |
| 266 | +spec: |
| 267 | + ... |
| 268 | + asserts: |
| 269 | + - {type: AssertIsMap, actual: $Response} |
| 270 | +``` |
| 271 | + |
| 272 | +### AssertMapContains |
| 273 | + |
| 274 | +Checks if data given in `actual` is a JSON object or dictionary. If so, does it contains a value given in `expected`. |
| 275 | + |
| 276 | +```yaml |
| 277 | +spec: |
| 278 | + ... |
| 279 | + asserts: |
| 280 | + - { type: AssertMapContains, actual: $StudentObject, expected: 10 } |
| 281 | +
|
| 282 | + - type: AssertMapContains |
| 283 | + actual: $StudentObject |
| 284 | + expected: {'teacher': {'id': 11, 'name': 'Some one'}} |
| 285 | +``` |
| 286 | + |
| 287 | +### AssertMapHasKey |
| 288 | + |
| 289 | +Checks if data given in `actual` is a JSON object or dictionary. If so, does it contains a key given in `expected`. |
| 290 | + |
| 291 | +```yaml |
| 292 | +spec: |
| 293 | + ... |
| 294 | + asserts: |
| 295 | + - { type: AssertMapHasKey, actual: $StudentObject, expected: 'name' } |
| 296 | +``` |
| 297 | + |
| 298 | +### AssertMapDoNotHasKey |
| 299 | + |
| 300 | +Checks if data given in `actual` is a JSON object or dictionary. If so, does it DOES NOT contains a key given in `expected`. |
| 301 | + |
| 302 | +```yaml |
| 303 | +spec: |
| 304 | + ... |
| 305 | + asserts: |
| 306 | + - { type: AssertMapDoNotHasKey, actual: $StudentObject, expected: 'salary' } |
| 307 | +``` |
| 308 | + |
| 309 | +### AssertMapKeyCount |
| 310 | + |
| 311 | +Checks if data given in `actual` is a JSON object or dictionary. If so, does it have same number of keys given in `expected`. |
| 312 | + |
| 313 | +```yaml |
| 314 | +spec: |
| 315 | + ... |
| 316 | + asserts: |
| 317 | + - { type: AssertMapKeyCount, actual: $StudentObject, expected: 10 } |
| 318 | +``` |
| 319 | + |
| 320 | +### AssertMapHasKeys |
| 321 | + |
| 322 | +Checks if data given in `actual` is a JSON object or dictionary. If so, does it contains all of keys given in `expected`. `expected` have to be a list of string. |
| 323 | + |
| 324 | +```yaml |
| 325 | +spec: |
| 326 | + ... |
| 327 | + asserts: |
| 328 | + - { type: AssertMapHasKeys, actual: $StudentObject, expected: ['name', 'section', 'class'] } |
| 329 | +``` |
| 330 | + |
| 331 | +### AssertMapDoNotHasKeys |
| 332 | + |
| 333 | +Checks if data given in `actual` is a JSON object or dictionary. If so, it DOES NOT contains any of keys given in `expected`. `expected` have to be a list of string. |
| 334 | + |
| 335 | +```yaml |
| 336 | +spec: |
| 337 | + ... |
| 338 | + asserts: |
| 339 | + - { type: AssertMapDoNotHasKeys, actual: $StudentObject, expected: ['salary', 'year_of_experience'] } |
| 340 | +``` |
| 341 | + |
| 342 | +### AssertMapExactKeys |
| 343 | + |
| 344 | +Checks if data given in `actual` is a JSON object or dictionary. If so, it only contains keys given in `expected`. `expected` have to be a list of string. |
| 345 | + |
| 346 | +```yaml |
| 347 | +spec: |
| 348 | + ... |
| 349 | + asserts: |
| 350 | + - type: AssertMapExactKeys |
| 351 | + actual: $StudentObject |
| 352 | + expected: ['name', 'section', 'class', 'class_teacher_id'] |
| 353 | +``` |
0 commit comments