You can use the Functions Framework for Python to run the function locally.
Given a function in a file named main.py like so:
def my_function(request):
return 'Hello World'
You can do:
$ pip install functions-framework
$ functions-framework --target my_function
Which will start a local development server at http://localhost:8080.
To invoke it locally for an HTTP function:
$ curl http://localhost:8080
For a background function with non-binary data:
$ curl -d '{"data": {"hi": "there"}}' -X POST \
-H "Content-Type: application/json" \
http://localhost:8080
For a background function with binary data:
$ curl -d "@binary_file.file" -X POST \
-H "Ce-Type: true" \
-H "Ce-Specversion: true" \
-H "Ce-Source: true" \
-H "Ce-Id: true" \
-H "Content-Type: application/json" \
http://localhost:8080
Answer from Dustin Ingram on Stack Overflow
» pip install functions-framework
firebase - Test Python Google Cloud Functions locally - Stack Overflow
How am I supposed to use functions-framework to test my cloud functions?
How to install google.cloud.functions_v1 in python? - Stack Overflow
What is the advantage to using the Function Framework to test my Google Cloud functions locally, instead of just invoking them directly?
Videos
You can use the Functions Framework for Python to run the function locally.
Given a function in a file named main.py like so:
def my_function(request):
return 'Hello World'
You can do:
$ pip install functions-framework
$ functions-framework --target my_function
Which will start a local development server at http://localhost:8080.
To invoke it locally for an HTTP function:
$ curl http://localhost:8080
For a background function with non-binary data:
$ curl -d '{"data": {"hi": "there"}}' -X POST \
-H "Content-Type: application/json" \
http://localhost:8080
For a background function with binary data:
$ curl -d "@binary_file.file" -X POST \
-H "Ce-Type: true" \
-H "Ce-Specversion: true" \
-H "Ce-Source: true" \
-H "Ce-Id: true" \
-H "Content-Type: application/json" \
http://localhost:8080
Update
Please, use the official emulator and serving framework from GCP https://github.com/GoogleCloudPlatform/functions-framework-python
You can install it with
pip install functions-framework
Deprecated
Based on Dustin's answer I've developed a package to serve as emulator:
pip install gcp-functions-emulator
Given you want to serve the following function
# mycloudfunction.py
def api(request):
return 'important data'
To emulate we have to call it like so:
gcpfemu <path/to/file.py> <function_name>
For example, with the code above we will call it:
gcpfemu mycloudfunction.py api
And to access the data we can use for example curl:
curl localhost:5000/api
> important data
» pip install google-cloud-functions
It seems like whenever you run `functions-framework` locally, it deploys your function on localhost on a port of your choosing. This works great if you are developing a new function and just need to test it in a quick and easy way, but how are you supposed to write code that lets you easily develop and test multiple functions over time? I'm aware that firebase might solve that, but the reason I am asking this question is to understand what the state-of-the-art is outside of firebase.
The problem is, if I have code that hits my function URL, and then I want to update the function and test it as part of my application, it seems like I have to set a per-function environment variable that swaps out the HTTP trigger uri for the localhost:PORT testing URI. That's pretty annoying when you have to have a separate env variable for each function you might test.
Surely I'm missing something here? What do people do in this situation?
EDIT: Found this github issue (https://github.com/GoogleCloudPlatform/functions-framework-nodejs/issues/23) that seems to indicate that there's no equivalent solution to firebase/the old functions emulator for serving multiple functions locally, but you can fairly easily work around it by writing a little bit of code to multiplex your functions manually. I wish this was included in the official docs rather than just in the Github Issue!
Unfortunately this is not currently possible. I've filed an internal feature request to open-source these types as a google-cloud-functions package, and will update this answer if/when this happens.
I found this library: functions-framework on GoogleCloudPlatform/functions-framework-python github repository.
As it is a namespace package, you can import it by using the following snippet:
$ pip install functions-framework
...
$ pip freeze | grep functions-framework
functions-framework==2.1.3
$ python
>>> import google.cloud.functions.context
>>> google.cloud.functions.context.Context
<class 'google.cloud.functions_v1.context.Context'>
So for your code:
import google.cloud.functions.context as functions_context
def my_function(data: Dict[Text, Any],
context: functions_context.Context) -> None:
...