newman -v): latesta) pm.expect(1).to.be.equal(2)
// AssertionError is thrown here
b) pm.test('MyTest', () => {
pm.expect(1).to.be.equal(2)
})
// AssertionFailure and AssertionError is thrown here
Command / script used to run Newman:
newman.run({
collection: <collectionname>
})
Sample collection, and auxiliary files (minus the sensitive details):
{
"info": {
"_postman_id": "f0de4cb5-0714-4264-b446-16253c3f4420",
"name": "Sample Collection",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Sample Request",
"event": [
{
"listen": "test",
"script": {
"id": "fcae8c4d-4a1b-4a60-b922-b8b8d1bef6da",
"type": "text/javascript",
"exec": [
"pm.expect(1, 'Outside Test : Expect').to.be.equal(2)",
"",
"pm.test('Test', () => {",
" pm.expect(1, 'Inside Test : Expect').to.be.equal(2)",
"})"
]
}
},
{
"listen": "prerequest",
"script": {
"id": "ad8f76e9-e215-43b4-bf3d-76d969b45b30",
"type": "text/javascript",
"exec": [
""
]
}
}
],
"request": {
"method": "GET",
"header": [],
"body": {},
"url": {
"raw": "http://postman-echo.com/get?foo=bar",
"protocol": "http",
"host": [
"postman-echo",
"com"
],
"path": [
"get"
],
"query": [
{
"key": "foo",
"value": "bar"
}
]
}
},
"response": []
}
]
}
@prateeksarda Apologies for the delay here.
AssertionError
pm.expect assertions (as well as other assertions in general) work by throwing an error. When a pm.expect statement is present outside the pm.test wrapper, an assertion failure will cause an error to be thrown, causing a script error. This is what would cause your collection run to exit prematurely (if the bail flag has been set).
AssertionFailure
However, pm.expect statements inside the pm.test enclosure will merely cause that singular test to fail if there is an assertion error. While this too can cause your collection run to terminate, the termination happens more gracefully due to a test failure, and not an arbitrary error being thrown. In the second case, the script containing a failed test will continue to completion.
@kunagpal Thanks Kunal that explains it well.
Most helpful comment
@prateeksarda Apologies for the delay here.
AssertionError
pm.expectassertions (as well as other assertions in general) work by throwing an error. When apm.expectstatement is present outside thepm.testwrapper, an assertion failure will cause an error to be thrown, causing a script error. This is what would cause your collection run to exit prematurely (if the bail flag has been set).AssertionFailure
However,
pm.expectstatements inside thepm.testenclosure will merely cause that singular test to fail if there is an assertion error. While this too can cause your collection run to terminate, the termination happens more gracefully due to a test failure, and not an arbitrary error being thrown. In the second case, the script containing a failed test will continue to completion.