Testing HTTP Functions
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
PHP
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
PHP
To run the unit tests, use PHPUnit.
Integration tests
These tests act as integration tests for the function above:
Node.js
Python
Java
C#
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:8081
, and does
not use environment variables. Maven uses mvn verify
to run
integration tests.
mvn verify -Dit.test=ExampleIT
C#
dotnet test
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 nodejs18 --trigger-http --allow-unauthenticatedYou can use the following values for the
--runtime
flag to specify your preferred Node.js version:
nodejs18
(recommended)nodejs20
(preview)nodejs16
nodejs14
nodejs12
nodejs10
Python
gcloud functions deploy hello_http \ --runtime python311 --trigger-http --allow-unauthenticatedYou can use the following values for the
--runtime
flag to specify your preferred Python version:
python311
(recommended)python310
python39
python38
python37
Go
gcloud functions deploy HelloHTTP \ --runtime go120 --trigger-http --allow-unauthenticatedYou can use the following values for the
--runtime
flag to specify your preferred Go version:
go120
(recommended)go119
go118
go116
go113
Java
gcloud functions deploy java-hello-http \ --entry-point functions.HelloHttp \ --runtime java17 \ --memory 512MB --trigger-http --allow-unauthenticatedYou can use the following values for the
--runtime
flag to specify your preferred Java version:
java17
(recommended)java11
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 Google Cloud project ID.