Containerized Automated Tests using Google Cloud Platform

Continuous Testing in a serverless setup using Cloud Build, Container Registry and Cloud Storage

Ashish Ghosh
5 min readNov 20, 2020

In this article, we will talk about how one can easily containerize and run automated test cases as part of CI — CD using Google Cloud Platform ‍🚀

I won’t discuss too much on why we should containerize our automated tests. That’s a topic which I have covered extensively in one of my previous articles. You can read it here.

Here we will directly dig deep into the Google Cloud services that we can use and see how we can effectively use them.

What we need :

  1. Google Cloud Source Repository — This is the version control system where you can check in your test automation code. [Details]
  2. Google Cloud Build — This is a serverless CI/CD platform used for building/testing the code. [Details]
  3. Google Container Registry — This is where we store, manage and deploy containers. [Details]
  4. Google Cloud Storage — This is where we store data and artifacts. [Details]
  5. Google Kubernetes Engine — This is a secure and managed Kubernetes service. [Details].

Using Google Kubernetes Engine service we will deploy Selenium Grid, in our case Zalenium. I have explained a detailed step by step procedure in a separate article which you can read from here :

https://ghoshasish99.medium.com/zalenium-in-google-cloud-using-kubernetes-and-helm-1e313daceba3

The workflow to be discussed is as follows :

Step 1 : Create your automated tests and push the code to Google Cloud Source Repositories

Log on to GCP console → Search for Source Repositories → Click on [Add repository] → Fill details → Click [Create]

Steps to create Google Cloud Source Repository

Install Google Cloud SDK on your local machine to access Google Services using command line

To push your code to Source Repository, use regular git commands :

git add .
git commit -m "Your_Commit_Message"
git push

Your file structure should tentatively have these 3 important files, however you can make modifications according to your needs :

(root directory name)
|-- cloudbuild.yml
|-- Dockerfile
|-- docker-compose.yml

We will deal with the details of the cloudbuild.yml in Step 4 and docker-compose.yml in Step 5.

Step 2: Create a Bucket in Google Cloud Storage for storing build artifacts (Test Execution Reports)

Go to GCP console → Search for Cloud Storage→ Click on [ Create bucket] → Fill details → Click [ Create]

Steps to create Google Cloud Storage Bucket

Step 3: Create a Google Container Registry for storing the automated test images

Go to GCP console→ Search for Container Registry→ Click on [Create repository] → Fill details → Click [ Create]

Container Registry : testautomation is created

Step 4: Set up a Google Cloud Build for building docker image for automated test and push it to Container Registry.

Before setting up the Cloud Build, we should take a step back and look at the cloudbuild.yml, I briefly mentioned above. The Cloud Build requires a yaml file which should contain the details of the activities that are expected to be performed during the ‘build’ process. Our cloudbuild.yml should look somewhat like this :

steps:# Build a docker image
- name: 'gcr.io/cloud-builders/docker'
args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/testautomation', '--cache-from', 'gcr.io/$PROJECT_ID/testautomation', '.' ]
# Push the docker image to container registry
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/testautomation']
# Execute the tests by invoking docker compose
- name: 'docker/compose:1.15.0'
args: ['run', 'automation']
env:
- 'PROJECT_ID=$PROJECT_ID'
- 'PROJECT_NAME=${_PROJ}'
- 'RELEASE_NAME=${_RELEASE}'
- 'TESTSET_NAME=${_TESTSET}'
- 'GRID_URL=${_GRIDURL}'
# Store the Test Results in cloud storage
artifacts:
objects:
location: 'gs://testautomationbuildartifacts/ExecutionResults'
paths: ['Results/Latest/*.txt','Results/Latest/summary.html']
images:
- 'gcr.io/$PROJECT_ID/testautomation'

As obvious from the cloudbuild.yml above, there are 4 important activities that we are performing :

  1. In 1st Step, we are building the test automation image and tagging it
  2. In 2nd Step, we are pushing the image to GCR
  3. In 3rd Step, we are executing the tests by invoking docker compose
  4. In 4th Step, we are publishing the test results and pushing them to Cloud Storage

If we observe carefully in the cloudbuild.yml, we are using some environment variables like ${_PROJ}, ${_RELEASE} etc. We will define these variables shortly.

So lets go ahead and create the Cloud Build.

Go to GCP console → Search for Cloud Build→ Click on [Triggers] → Click on [CREATE TRIGGER] → Fill details (Some important configurations are listed below)→ Click [CREATE] → To run the trigger, click [Run]

  1. Trigger Name and Description
  2. Event : Push to a branch
  3. Source
    Repository : Name of the Cloud Source Repository
    Branch : master (or the name of the branch where your latest code is)

4. Build Configuration : Specify the yaml configuration file

5. Add Variables : Define the environment variables here

Environment Variables and their corresponding values

Please note, the Selenium GRID URL details can be obtained by following the steps mentioned in this article.

If everything goes well, your Cloud Build will succeed and you will be able to see the following:

Outcomes of the build stage

Furthermore, to view the execution logs of the tests you can navigate to the execution details of the build :

To view the test execution videos, you can navigate to the Zalenium Dashboard which, as referred earlier, can be set up following this article.

Voila!!

You have successfully set up a complete end to end containerized automated test execution using Google Cloud Services.

Happy testing!😊

Checkout the following GitHub link to view the code and the files used in the above setup:

--

--

Ashish Ghosh

Test Automation Architect @ING Bank. Tech enthusiast. Innovation champion.