Types of Hook

vREST NG provides two categories of hooks:

  1. Local Hooks: Local hooks are executed only when they are linked with test case explicitly. And the test case is only marked as passed when its own assertions are passed and also if there are any associated local hooks and assertions of those linked local hooks are also passed. These hooks are useful in doing micro services testing. Two types of hooks comes under local hooks.
    1. Pre Test Case Local Hooks
    2. Post Test Case Local Hooks
  2. Non Local Hooks: Hooks which cannot be linked to test case explicitly are known as Non Local hooks. These hooks doesn't have any direct impact on the test case result. The following hooks comes under non local hooks.
    1. Pre Test Run Hooks
    2. Post Test Run Hooks
    3. Pre Test Suite Hooks
    4. Post Test Suite Hooks
    5. Pre Test Case Hooks
    6. Post Test Case Hooks
    7. Pre Test Case Iteration Hooks
    8. Post Test Case Iteration Hooks

Now, let's discuss all types of hooks in detail. vREST NG provides following types of hooks:

  1. Pre Test Run Hooks
    • This type of hooks will be executed just after creation of test run and before the execution of test cases.
    • Example use cases:
      • This can be useful in setting up resources in your eco system tools. e.g. Creating a test run in Test Rail.
      • This can be useful in setting cookie based authentication to third party tools. (Note: Authorization can also be configured via Configuration Tab >> Authorizations, similar to test cases.).
    • A special variable {{$tr}} will be available to the hook and the value of this variable will be as follows:
    {
      "details": {
        "id": "<String>", //Id of the stored Test Run if vREST NG Dashboard service is enabled otherwise random id
        "name": "<String>", //Name of the test run (date and time information)
        "createdAt": "<String>",//Test Run Creation Date and Time
        "executionSource": "<String>", //Execution Source, values can be one of APP or CLI
        "environment": "<String>", //Test Environment name configured in vREST NG
        "executor": { //if user is logged in into vREST NG
            "name": "<String>", //Test Run Executor name
            "email": "<String>" //Test Run executor email
        }
      }
    }
    
  2. Post Test Run Hooks
    • This type of hooks will be executed after the test run execution completes.
    • Example use cases:
      • This can be useful in notifying the status of the test run to your team via slack, hipchat etc.
      • This can be useful in triggering jobs on the continuous integration server if there are no failed test cases.
    • This is the most widely used hook.
    • A special variable {{$tr}} will be available to the hook and the value of this variable will be as follows:
    {
      "details": {
        "id": "<String>", //Id of the stored Test Run if vREST NG Dashboard service is enabled otherwise random id
        "name": "<String>", //Name of the test run (date and time information)
        "createdAt": "<String>",//Test Run Creation Date and Time
        "executionSource": "<String>", //Execution Source, values can be one of APP or CLI
        "environment": "<String>", //Test Environment name configured in vREST NG
        "executor": { //if user is logged in into vREST NG
            "name": "<String>", //Test Run Executor name
            "email": "<String>" //Test Run executor email
        }
      },
      "result": {
        "totalCount": "<Number>", //Total number of test cases
        "failedCount": "<Number>", //Count of failed test cases
        "passedCount": "<Number>", //Count of passed test cases
        "notExecutedCount": "<Number>", //Count of not executed test cases
        "notRunnableCount": "<Number>", //Count of not runnable test cases
        "executionTime": "<Number>", //Total execution time of the test run
        "remarks": "<String>", //Remarks if any
        "resultLink": "<String>" //Link to view the results of test cases in the test run
      }
    }
    
  3. Pre Test Suite Hooks
    • This type of hooks will be executed just before the execution of the test suite.
    • A special variable {{$ts}} will be available to the hook and the value of this variable will be as follows:
    {
      "details": {
        "id": "<String>", //Name of the test suite
        "name": "<String>" //Name of the test suite
      }
    }
    
  4. Post Test Suite Hooks
    • This type of hooks will be executed after the test suite execution completes.
    • A special variable {{$ts}} will be available to the hook and the value of this variable will be as follows:
    {
      "details": {
        "id": "<String>", //Name of the test suite
        "name": "<String>" //Name of the test suite
      },
      "result": {
        "isExecuted": "<Boolean>", //true if any of the associated test case is executed, false otherwise.
        "isPassed": "<Boolean>" //true if all of the associated test cases are passed, false otherwise.
      }
    }
    
  5. Pre Test Case Hooks
    • This type of hooks will be executed just before the execution of test case.
    • If the loop source is configured in the test case then also this hook will be executed once before the first iteration.
    • A special variable {{$tc}} will be available to the hook and the value of this variable will be as follows:
    {
      "details": {
          "id": "<String>", //Test Case Id
          "summary": "<String>", //Test Case summary
          "externalId": "<String>",//External Id of the test case if defined
          "index": "<Number>", //Current test case position starting with 1
          "loopSource": "<String>" //Loop source property of the test case.
      },
      "request": { //Test Case request definition as specified by user
          "method": "<String>", //Test Case request method, value can be one of GET, POST, PUT, PATCH, DELETE, OPTIONS.
          "url": "<String>", //Test Case URL
          "queryParams": [
              {
                  "name": "<String>", //Parameter name
                  "value": "<String>" //Parameter value
              },
              ...
          ],
          "headers": {
            "[HEADER_NAME]": "[HEADER_VALUE]"
          },
          "body": {
            "type": "<BODY_TYPE>", //body type can be one of "form-urlencoded", "json", "xml", "text", "others"
            "formParams": [
              {
                  "name": "<String>", //Parameter name
                  "value": "<String>" //Parameter value
              },
              ...
            ],
            "content": "<String>"
          }
      },
      "extensions": {
        "[EXTENSION_NAME]": {
          "[EXTENSION_FIELD_NAME]": "[EXTENSION_FIELD_VALUE]"
        }
      }
    }
    
  6. Post Test Case Hooks
    • This type of hooks will be executed after the test case execution completes.
    • If the loop source is configured in the test case then this hook will be executed once all the iterations are executed.
    • A special variable {{$tc}} will be available to the hook and the value of this variable will be as follows:
    {
      "details": {
          "id": "<String>", //Test Case Id
          "summary": "<String>", //Test Case summary
          "externalId": "<String>",//External Id of the test case if defined
          "index": "<Number>", //Current test case position starting with 1
          "loopSource": "<String>" //Loop source property of the test case.
      },
      "request": { //Test Case request definition as specified by user
          "method": "<String>", //Test Case request method, value can be one of GET, POST, PUT, PATCH, DELETE, OPTIONS.
          "url": "<String>", //Test Case URL
          "queryParams": [
              {
                  "name": "<String>", //Parameter name
                  "value": "<String>" //Parameter value
              },
              ...
          ],
          "headers": {
            "[HEADER_NAME]": "[HEADER_VALUE]"
          },
          "body": {
            "type": "<BODY_TYPE>", //body type can be one of "form-urlencoded", "json", "xml", "text", "others"
            "formParams": [
              {
                  "name": "<String>", //Parameter name
                  "value": "<String>" //Parameter value
              },
              ...
            ],
            "content": "<String>"
          }
      },
      "extensions": {
        "[EXTENSION_NAME]": {
          "[EXTENSION_FIELD_NAME]": "[EXTENSION_FIELD_VALUE]"
        }
      },
      "result": {
        "isExecuted": "<Boolean>", //true if a single iteration is executed for the test case, false otherwise.
        "isPassed": "<Boolean>" //true if all of the iterations are passed for this test case, false otherwise.
      }
    }
    
  7. Pre Test Case Iteration Hooks
    • This type of hooks will be executed for each iteration of the test case if the loop source is configured.
    • Otherwise it will be executed only once before the test execution.
    • A special variable {{$tc}} will be available to the hook and the value of this variable will be as follows:
    {
      "details": {
          "id": "<String>", //Test Case Id
          "summary": "<String>", //Test Case summary
          "externalId": "<String>",//External Id of the test case if defined
          "index": "<Number>", //Current test case position starting with 1
          "loopSource": "<String>", //Loop source property of the test case.
          "loopIndex": "<Number>" //loop iteration number (starting from 0) which is being executed
      },
      "request": { //Test Case request definition as specified by user
          "method": "<String>", //Test Case request method, value can be one of GET, POST, PUT, PATCH, DELETE, OPTIONS.
          "url": "<String>", //Test Case URL
          "queryParams": [
              {
                  "name": "<String>", //Parameter name
                  "value": "<String>" //Parameter value
              },
              ...
          ],
          "headers": {
            "[HEADER_NAME]": "[HEADER_VALUE]"
          },
          "body": {
            "type": "<BODY_TYPE>", //body type can be one of "form-urlencoded", "json", "xml", "text", "others"
            "formParams": [
              {
                  "name": "<String>", //Parameter name
                  "value": "<String>" //Parameter value
              },
              ...
            ],
            "content": "<String>"
          }
      },
      "$data": "<OBJECT>", //Template Data
      "extensions": {
        "[EXTENSION_NAME]": {
          "[EXTENSION_FIELD_NAME]": "[EXTENSION_FIELD_VALUE]"
        }
      }
    }
    
  8. Post Test Case Iteration Hooks
    • This type of hook will be executed for each iteration of the test case if the loop source is configured.
    • Otherwise it will be executed only once after the test execution.
    • This hook is different from post-test-case-hook as it contains the detailed information regarding test case execution details and assertions summary report.
    • This is the mostly used hook and can be useful in logging defect in your defect tracking tools with all the debug information. e.g. Log defects in JIRA whenever a test case or test case iteration fails.
    • A special variable {{$tc}} will be available to the hook and the value of this variable will be as follows:
    {
      "details": {
          "id": "<String>", //Test Case Id
          "summary": "<String>", //Test Case summary
          "externalId": "<String>",//External Id of the test case if defined
          "index": "<Number>", //Current test case position starting with 1
          "loopSource": "<String>", //Loop source property of the test case.
          "loopIndex": "<Number>" //loop iteration number (starting from 0) which is being executed
      },
      "request": { //Test Case request definition as specified by user
          "method": "<String>", //Test Case request method, value can be one of GET, POST, PUT, PATCH, DELETE, OPTIONS.
          "url": "<String>", //Test Case URL
          "queryParams": [
              {
                  "name": "<String>", //Parameter name
                  "value": "<String>" //Parameter value
              },
              ...
          ],
          "headers": {
            "[HEADER_NAME]": "[HEADER_VALUE]"
          },
          "body": {
            "type": "<BODY_TYPE>", //body type can be one of "form-urlencoded", "json", "xml", "text", "others"
            "formParams": [
              {
                  "name": "<String>", //Parameter name
                  "value": "<String>" //Parameter value
              },
              ...
            ],
            "content": "<String>"
          }
      },
      "$data": "<OBJECT>", //Template Data
      "extensions": {
        "[EXTENSION_NAME]": {
          "[EXTENSION_FIELD_NAME]": "[EXTENSION_FIELD_VALUE]"
        }
      },
      "result": {
        "isExecuted": "<Boolean>", //true if a single iteration is executed for the test case, false otherwise.
        "isPassed": "<Boolean>", //true if all of the iterations are passed for this test case, false otherwise.
        "isRunnable": "<Boolean>", //true if the test case was runnable otherwise false.
        "assertionSummary": "<String>", //detailed assertion summary report
        "assertionResults": [
          {
            "source": "<String>",
            "property": "<String>",
            "operator": "<String>",
            "expectedValue": "<String>",
            "actualValue": "<any>",
            "isPassed": "<Boolean>"
          },
          ...
        ],
        "resultLink": "<String>" //Link to view the result of this test case in the current test run.
      },
      "execution": {
        "remarks": "<String>",
        "request": { //The request sent by vREST NG runner
          "method": "<String>",
          "url": "<String>",
          "headers": {
            "[HEADER_NAME]": "[HEADER_VALUE]"
          },
          "body": "<String>"
        },
        "response": {
          "statusCode": "<Number>", //status code of the API response
          "headers": { //response headers
            "[HEADER_NAME]": "[HEADER_VALUE]"
          },
          "executionTime": "<Number>", //execution time in ms
          "type": "<String>", //response content type
          "content": "<String>" //response body received
        }
      }
    }
    
  9. Pre Test Case Local Hooks
    • This is a local hook. In order to execute this hook, you need to set this hook in the "Local Hooks" field available in the Details tab of the test case.
    • This hook is almost similar to Pre Test Case Iteration Hook.
    • This type of hooks will be executed for each iteration of the test case if the loop source is configured.
    • Otherwise it will be executed only once before the test execution.
    • A special variable {{$tc}} will be available to the hook and the value of this variable will be as follows:
    {
      "details": {
          "id": "<String>", //Test Case Id
          "summary": "<String>", //Test Case summary
          "externalId": "<String>",//External Id of the test case if defined
          "index": "<Number>", //Current test case position starting with 1
          "loopSource": "<String>", //Loop source property of the test case.
          "loopIndex": "<Number>" //loop iteration number (starting from 0) which is being executed
      },
      "request": { //Test Case request definition as specified by user
          "method": "<String>", //Test Case request method, value can be one of GET, POST, PUT, PATCH, DELETE, OPTIONS.
          "url": "<String>", //Test Case URL
          "queryParams": [
              {
                  "name": "<String>", //Parameter name
                  "value": "<String>" //Parameter value
              },
              ...
          ],
          "headers": {
            "[HEADER_NAME]": "[HEADER_VALUE]"
          },
          "body": {
            "type": "<BODY_TYPE>", //body type can be one of "form-urlencoded", "json", "xml", "text", "others"
            "formParams": [
              {
                  "name": "<String>", //Parameter name
                  "value": "<String>" //Parameter value
              },
              ...
            ],
            "content": "<String>"
          }
      },
      "$data": "<OBJECT>", //Template Data
      "extensions": {
        "[EXTENSION_NAME]": {
          "[EXTENSION_FIELD_NAME]": "[EXTENSION_FIELD_VALUE]"
        }
      }
    }
    
  10. Post Test Case Local Hooks
    • This is a local hook. In order to execute this hook, you need to set this hook in the "Local Hooks" field available in the Details tab of the test case.
    • This hook is almost similar to Post Test Case Iteration Hook. The only difference is test case result data will not be available in this hook.
    • This type of hook will be executed for each iteration of the test case if the loop source is configured.
    • Otherwise it will be executed only once after the test execution.
    • This hook is different from post-test-case-hook as it contains the detailed information regarding test case execution details.
    • A special variable {{$tc}} will be available to the hook and the value of this variable will be as follows:
    {
      "details": {
          "id": "<String>", //Test Case Id
          "summary": "<String>", //Test Case summary
          "externalId": "<String>",//External Id of the test case if defined
          "index": "<Number>", //Current test case position starting with 1
          "loopSource": "<String>", //Loop source property of the test case.
          "loopIndex": "<Number>" //loop iteration number (starting from 0) which is being executed
      },
      "request": { //Test Case request definition as specified by user
          "method": "<String>", //Test Case request method, value can be one of GET, POST, PUT, PATCH, DELETE, OPTIONS.
          "url": "<String>", //Test Case URL
          "queryParams": [
              {
                  "name": "<String>", //Parameter name
                  "value": "<String>" //Parameter value
              },
              ...
          ],
          "headers": {
            "[HEADER_NAME]": "[HEADER_VALUE]"
          },
          "body": {
            "type": "<BODY_TYPE>", //body type can be one of "form-urlencoded", "json", "xml", "text", "others"
            "formParams": [
              {
                  "name": "<String>", //Parameter name
                  "value": "<String>" //Parameter value
              },
              ...
            ],
            "content": "<String>"
          }
      },
      "$data": "<OBJECT>", //Template Data
      "extensions": {
        "[EXTENSION_NAME]": {
          "[EXTENSION_FIELD_NAME]": "[EXTENSION_FIELD_VALUE]"
        }
      },
      "execution": {
        "remarks": "<String>",
        "request": { //The request sent by vREST NG runner
          "method": "<String>",
          "url": "<String>",
          "headers": {
            "[HEADER_NAME]": "[HEADER_VALUE]"
          },
          "body": "<String>"
        },
        "response": {
          "statusCode": "<Number>", //status code of the API response
          "headers": { //response headers
            "[HEADER_NAME]": "[HEADER_VALUE]"
          },
          "executionTime": "<Number>", //execution time in ms
          "type": "<String>", //response content type
          "content": "<String>" //response body received
        }
      }
    }
    

Hook execution order

For a particular validation cycle, hooks are executed in the following order:

  1. Pre Test Run Hooks
  2. For each Test Suite
    1. Pre Test Suite Hooks
    2. For each Test Case
      1. Pre Test Case Hooks
      2. For each Test Case Iteration (executed once in case of no loop source property )
        1. Pre Test Case Iteration Hooks
        2. Pre Test Case Local Hooks
        3. Post Test Case Local Hooks
        4. Post Test Case Iteration Hooks
      3. Post Test Case Hooks
    3. Post Test Suite Hooks
  3. Post Test Run Hooks