There are two distinct types of Cloud Functions: HTTP functions and background 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 background 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
Unit tests
Here are unit tests for the Pub/Sub-triggered function above:
Node.js
Python
Go
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
Integration tests
Here are integration tests for the Pub/Sub-triggered function above:
Node.js
To run the integration tests for this function, complete the following steps:
Node.js
Run the test with the following command:
mocha test/sample.integration.pubsub.test.js --exit
System tests
Here are system tests for this function:
Node.js
Python
Go
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 nodejs8 --trigger-topic YOUR_PUBSUB_TOPIC
You can use the following values for the--runtime
flag to use different Node.js versions:nodejs6
(deprecated)nodejs8
nodejs10
(beta)
Python
gcloud functions deploy hello_pubsub --runtime python37 --trigger-topic YOUR_PUBSUB_TOPIC
Go
gcloud functions deploy HelloPubSub --runtime go111 --trigger-topic YOUR_PUBSUB_TOPIC
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
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
Unit tests
Here are unit tests for the storage-triggered function above:
Node.js
Python
Go
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
Integration tests
Here are integration tests for the storage-triggered function above:
Node.js
Run the integration tests with the following command:
Node.js
mocha test/sample.integration.storage.test.js --exit
System tests
These are the system tests for the storage-triggered function above:
Node.js
Python
Go
Deploy your function with the following command:
Node.js
gcloud functions deploy helloGCS --runtime nodejs8 --trigger-bucket YOUR_GCS_BUCKET_NAMEYou can use the following values for the
--runtime
flag to use different Node.js versions:
nodejs6
(deprecated)nodejs8
nodejs10
(beta)
Python
gcloud functions deploy hello_gcs --runtime python37 --trigger-bucket YOUR_GCS_BUCKET_NAME
Go
gcloud functions deploy HelloGCS --runtime go111 --trigger-bucket YOUR_GCS_BUCKET_NAME
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