Functional Testing Examples

With functional testing you can easily test your backend functions individually, reducing the need to build your own system for triggering and testing your backend code.

You can use functional testing to test a variety of scenarios, such as functions with:

HTTP requests

Below is an example of a function with an HTTP request that performs basic arithmetic operations based on the request path and query parameters:

HTTP function example

To test HTTP functions, specify the HTTP request object by replacing the placeholder data in the request template. For example, when testing a post function, replace the placeholder JSON object in the body property with a JSON object you want to test with.

The following properties are included in the request templates:

  • For get and delete functions: path, headers, and query
  • For put and post functions: path, headers, and body
  • For options functions: path and headers
  • For use functions: path, headers, query, and method

HTTP function parameters showing request template

Destructured parameters

You can pull values from arrays from objects into distinct variables using destructured parameters. The following function destructures the parameters { factor1, factor2 } directly from the specified object and returns their product:

Function with destructured parameters

To test a function with destructured parameters, place parameter values in a JSON object inside an array:

Destructured parameters test input

Missing arguments

When you set default values in your function declarations, you can test those functions without needing to provide all of the arguments. Here is an example of a function that uses default values for factor2 and factor3.

Function with default parameter values

To test a function without providing all of the arguments, place the arguments in an array in the Set Parameters section, and leave out at least one of the arguments that has a default value.

For example, the multiplyWithDefaults() function above was tested 3 times with the following arguments:

  • [1,2,3]
  • [1,2]
  • [1]

The following results were returned:

  • 1 * 2 * 3 = 6
  • 1 * 2 * 2 = 4
  • 1 * -1 * 2 = -2

Default parameters test results

JavaScript 'arguments' objects

The JavaScript arguments object can accept any number of arguments in a function. Here is an example of a function that uses the arguments object to find the sum of all the arguments provided.

Function using arguments object

To test a function containing the JavaScript arguments object, place the arguments in an array in the Set Parameters section:

Arguments object parameters

Date objects

When working with functional testing, keep in mind that JSON has no native Date type. The following function accepts a Date object as its parameter.

Function accepting Date object parameter

To test a function with a Date parameter, specify the value as a stringified date in the JSON using one of the following syntaxes:

Copy
Copy

Date object test parameters using stringified date

See also

Did this help?