Testing Event-Driven Functions
There are two distinct types of Cloud Functions: HTTP functions and event-driven functions. Each type has its own testing requirements.
A function's test structure depends on which Google Cloud resources that function uses. In turn, a function's resource use depends on how that function is triggered.
This document describes how to test event-driven Cloud Functions. See Testing HTTP Functions for information on how to test HTTP functions.
Pub/Sub-triggered functions
Pub/Sub-triggered function tests are structured differently depending on where the tested function is running.
Here is an example of a Pub/Sub-triggered function that prints "Hello, World":
Node.js
Python
Go
Java
C#
Ruby
PHP
Unit tests
Here are unit tests for the Pub/Sub-triggered function above:
Node.js
Python
Go
Java
C#
Ruby
PHP
Run the unit tests with the following command:
Node.js
mocha test/sample.unit.pubsub.test.js --exit
Python
pytest sample_pubsub_test.py
Go
go test -v ./hello_pubsub_test.go
Java
Maven usesmvn test
to run unit tests.
mvn test
C#
dotnet test
PHP
To run the unit tests, use PHPUnit.
Integration tests
Here are integration tests for the Pub/Sub-triggered function above:
Node.js
Python
Java
PHP
You can run the integration tests for this function as follows:
Node.js
mocha test/sample.integration.pubsub.test.js --exit
Python
pytest sample_pubsub_test_integration.py
Java
Maven usesmvn verify
to run integration tests.
mvn verify -Dit.test=ExampleIT
PHP
To run the integration tests, use PHPUnit.
System tests
Here are system tests for this function:
Node.js
Python
Go
Java
Run the system tests by following these instructions:
In your Google Cloud project, select a Pub/Sub topic to subscribe to. If you provide the name of a Pub/Sub topic that does not exist, it is created automatically.
Next, deploy your functions using the following command:
Node.js
gcloud functions deploy helloPubSub \ --runtime nodejs18 \
You can use the following values for the
--trigger-topic YOUR_PUBSUB_TOPIC--runtime
flag to specify your preferred Node.js version:nodejs18
(recommended)nodejs20
(preview)nodejs16
nodejs14
nodejs12
nodejs10
Python
gcloud functions deploy hello_pubsub \ --runtime python311 \
You can use the following values for the
--trigger-topic YOUR_PUBSUB_TOPIC--runtime
flag to specify your preferred Python version:python311
(recommended)python310
python39
python38
python37
Go
gcloud functions deploy HelloPubSub \ --runtime go120 \
You can use the following values for the
--trigger-topic YOUR_PUBSUB_TOPIC--runtime
flag to specify your preferred Go version:go120
(recommended)go119
go118
go116
go113
Java
gcloud functions deploy java-hello-pubsub \ --entry-point functions.HelloPubSub \ --runtime java17 \ --memory 512MB \
You can use the following values for the
--trigger-topic YOUR_PUBSUB_TOPIC--runtime
flag to specify your preferred Java version:java17
(recommended)java11
where
YOUR_PUBSUB_TOPIC
is the name of the Pub/Sub topic you want your functions to subscribe to.Run the system tests with the following command:
Node.js
export FUNCTIONS_TOPIC=YOUR_PUBSUB_TOPIC mocha test/sample.system.pubsub.test.js --exit
Python
export FUNCTIONS_TOPIC=YOUR_PUBSUB_TOPIC pytest sample_pubsub_test_system.py
Go
export FUNCTIONS_TOPIC=YOUR_PUBSUB_TOPIC go test -v ./hello_pubsub_system_test.go
Java
export FUNCTIONS_TOPIC=YOUR_PUBSUB_TOPIC mvn verify -Dit.test=ExampleSystemIT
where
YOUR_PUBSUB_TOPIC
is the name of the Pub/Sub topic you want your functions to subscribe to.
Storage-triggered functions
Tests for storage-triggered functions are similar in structure to their Pub/Sub-triggered counterparts. Like Pub/Sub-triggered function tests, storage-triggered function tests are structured differently depending on where the tested function is hosted.
Here is an example of a storage-triggered function:
Node.js
Python
Go
Java
C#
Ruby
PHP
Unit tests
Here are unit tests for the storage-triggered function above:
Node.js
Python
Go
Java
C#
Ruby
PHP
Run the unit tests with the following command:
Node.js
mocha test/sample.unit.storage.test.js --exit
Python
pytest sample_storage_test.py
Go
go test -v ./hello_cloud_storage_test.go
Java
Maven usesmvn test
to run unit tests.
mvn test
C#
dotnet test
PHP
To run the unit tests, use PHPUnit.
Integration tests
Here are integration tests for the storage-triggered function above:
Node.js
Java
PHP
You can run the integration tests for this function as follows:
Node.js
mocha test/sample.integration.storage.test.js --exit
Java
Maven usesmvn verify
to run integration tests:
mvn verify -Dit.test=ExampleIT
System tests
These are the system tests for the storage-triggered function above:
Node.js
Python
Go
Java
Deploy your function with the following command:
Node.js
gcloud functions deploy helloGCS \ --runtime nodejs18 \You can use the following values for the
--trigger-bucket YOUR_GCS_BUCKET_NAME
--runtime
flag to specify your preferred Node.js version:
nodejs18
(recommended)nodejs20
(preview)nodejs16
nodejs14
nodejs12
nodejs10
Python
gcloud functions deploy hello_gcs \ --runtime python311 \You can use the following values for the
--trigger-bucket YOUR_GCS_BUCKET_NAME
--runtime
flag to specify your preferred Python version:
python311
(recommended)python310
python39
python38
python37
Go
gcloud functions deploy HelloGCS \ --runtime go120 \You can use the following values for the
--trigger-bucket YOUR_GCS_BUCKET_NAME
--runtime
flag to specify your preferred Go version:
go120
(recommended)go119
go118
go116
go113
Java
gcloud functions deploy java-hello-gcs \ --entry-point functions.HelloGcs \ --runtime java17 \ --memory 512MB \You can use the following values for the
--trigger-bucket YOUR_GCS_BUCKET_NAME
--runtime
flag to specify your preferred Java version:
java17
(recommended)java11
where YOUR_GCS_BUCKET_NAME
is the Cloud Storage bucket
you want to monitor. Note that this must reference a bucket that exists in the
same Google Cloud project that the function is deployed to.
Run system tests with the following commands:
Node.js
export BUCKET_NAME=YOUR_GCS_BUCKET_NAME mocha test/sample.system.storage.test.js --exit
Python
export BUCKET_NAME=YOUR_GCS_BUCKET_NAME pytest sample_storage_test_system.py
Go
export BUCKET_NAME=YOUR_GCS_BUCKET_NAME go test -v ./hello_cloud_storage_system_test.go
Java
Maven usesmvn verify
to run system tests:
mvn verify -Dit.test=ExampleSystemIT