Locally test your function
This guide shows you how to locally call a function from source code
using a gcloud functions
command. To learn how to locally develop and test a
Cloud Run functions using the Functions Framework, see
Local functions development.
To support quick iteration and debugging for functions that were created with
the Cloud Functions v2 API, you can use the call
command in the command-line interface. This lets you directly invoke a function
to ensure it is behaving as expected. This causes the function to execute
immediately, even though it may have been deployed to respond to a specific
event.
Test your function with Google Cloud CLI
To directly invoke your function using the gcloud CLI, use
the gcloud functions call
command and supply any data
your function expects as JSON in the --data
argument. For example:
gcloud functions call YOUR_FUNCTION_NAME \
--region=REGION --gen2 \
--data '{"name":"Kalani"}'
Replace:
- YOUR_FUNCTION_NAME: the name of the function you're testing
- REGION: the Google Cloud region your function is deployed to
The --data
argument is sent to your function as follows:
- For HTTP functions, the data you supply is sent as the body of a POST request.
- For event-driven functions, the data is passed directly as the event data to your function.
For more information, see the gcloud functions call
documentation.
Cloud Pub/Sub event-driven function example
This example shows how to directly invoke an event-driven function triggered by Pub/Sub events:
Node.js
Python
Go
Java
C#
Ruby
PHP
To invoke the function directly, send a
PubsubMessage
,
which expects base64-encoded data, as the event data:
Node.js
DATA=$(printf 'Hello!'|base64) && gcloud functions call helloPubSub --data '{"data":"'$DATA'"}'
Python
DATA=$(printf 'Hello!'|base64) && gcloud functions call hello_pubsub --data '{"data":"'$DATA'"}'
Go
DATA=$(printf 'Hello!'|base64) && gcloud functions call HelloPubSub --data '{"data":"'$DATA'"}'
Java
DATA=$(printf 'Hello!'|base64) && gcloud functions call java-hello-pubsub --data '{"data":"'$DATA'"}'
C#
DATA=$(printf 'Hello!'|base64) && gcloud functions call csharp-hello-pubsub --data '{"data":"'$DATA'"}'
Ruby
DATA=$(printf 'Hello!'|base64) && gcloud functions call hello_pubsub --data '{"data":"'$DATA'"}'
PHP
DATA=$(printf 'Hello!'|base64) && gcloud functions call helloworldPubsub --data '{"data":"'$DATA'"}'
This CLI example uses bash
or sh
syntax. It works in Linux and Mac
environments but not Windows.