CI\CD for GCP Serverless Cloud Functions — 002 — Testing

Soliman ElSaber
8 min readNov 1, 2019

We want to run our test on the code before deploying it, so we don’t deploy a Cloud Function that breaks our serverless application.

If you just starting with CI\CD for CloudFunction on GCP, I recommend you read this article first: CI\CD for GCP Serverless Cloud Functions — 001

So, we need to have a test script for our function, and we need to run this test after we push the code to our repository.

We have a few main steps here:

  1. Writing our CloudFunction code
  2. Writing our Test Script
  3. Updating our CloudBuild Yaml file to run the test on GCP

Let us start with a simple one (always start simple). We will have a cloud function that returns the aggregation of two numbers (we still need to have some logic, not everything is Hello World!).

  • Create a new empty directory, we will call it ci-cd-cf-testing.
  • Create your index.js and package.json files for your function.
  • Create your test/sum.sample.unit.http.test.js file under the directory test for your test function.

The project structure should look like:

--

--