There are two distinct types of Cloud Functions. CloudEvent functions and background functions are both event-driven functions that are triggered by internal Cloud Platform events, while HTTP functions are triggered by HTTP requests.
A function's test structure depends on which Google Cloud Platform resources that function uses. In turn, a function's resource use depends on how that function is triggered.
This document describes how to test HTTP Cloud Functions. See Testing Event-Driven Functions for information on how to test event-driven functions.
HTTP-triggered functions
Like most Cloud Functions unit tests, HTTP-triggered function unit tests have a specific structure. However, system and integration tests are similar in structure—which is usually not the case for tests of other function types.
The following is an example HTTP function:
Node.js
Python
Go
Java
C#
Ruby
Unit tests
These tests act as unit tests for the HTTP-triggered function above.
Use the following command to run the unit tests:
Node.js
mocha test/sample.unit.http.test.js --exit
Python
pytest sample_http_test.py
Go
go test -v ./hello_http_test.go
Java
Maven usesmvn test
to run unit tests:
mvn test
C#
dotnet test
Ruby
bundle exec ruby functions/test/helloworld/http_test.rb
Integration tests
These tests act as integration tests for the function above:
Node.js
Python
Java
To run integration tests for HTTP functions, use the following command. You can
configure the integration test samples shown above by changing the values used
in the export
commands shown below.
Node.js
export PORT=8080 mocha test/sample.integration.http.test.js --exit
Python
export PORT=8080 pytest sample_http_test_integration.py
Java
The Java integration test example always runs atlocalhost:8080
, and does
not use environment variables. Maven uses mvn verify
to run
integration tests.
mvn verify -Dit.test=ExampleIT
System tests
These tests act as system tests for the function above:
Node.js
Python
Go
Java
To run system tests for HTTP functions, deploy your functions with the following command:
Node.js
gcloud functions deploy helloHttp \ --runtime nodejs10You can use the following values for the
--runtime
flag to specify your preferred Node.js version:
nodejs10
nodejs12
nodejs14
(public preview)
Python
gcloud functions deploy hello_http \ --runtime python38You can use the following values for the
--runtime
flag to specify your preferred Python version:
python37
python38
python39
(public preview)
Go
gcloud functions deploy HelloHTTP \ --runtime go113You can use the following values for the
--runtime
flag to specify your preferred Go version:
go111
(Deprecated)go113
Java
gcloud functions deploy java-hello-http \ --entry-point functions.HelloHttp \ --runtime java11 \ --memory 512MB
Use the following commands to test your deployed HTTP function. Note that the primary difference between system tests and integration tests for HTTP Cloud Functions is the URL where the function can be reached:
Node.js
export BASE_URL=https://YOUR_GCF_REGION-YOUR_GCP_PROJECT_ID.cloudfunctions.net/ mocha test/sample.system.http.test.js --exit
Python
export BASE_URL=https://YOUR_GCF_REGION-YOUR_GCP_PROJECT_ID.cloudfunctions.net/ pytest sample_http_test_system.py
Go
export BASE_URL=https://YOUR_GCF_REGION-YOUR_GCP_PROJECT_ID.cloudfunctions.net/ go test -v ./hello_http_system_test.go
Java
Maven usesmvn verify
to run system tests.
mvn verify -Dit.test=ExampleSystemIT
where:
YOUR_GCF_REGION
is your Cloud Functions region.YOUR_GCP_PROJECT_ID
is your Cloud project ID.