Automated Tests: Setup using Github Actions

Automated Tests: Setup using Github Actions

Why do we need Automated Testing?

In large codebases, if your project is being deployed directly from the main or master branch, you do not want to push/merge code that might potentially break your project. As a maintainer, you need to make sure that any changes proposed in a pull request do not break production.

To ensure this you can write test cases that ensure certain functionality works as intended. You can also set up your project in a way that runs these test cases every time someone makes a pull request. This would make it easier for you as a maintainer to approve certain changes.

Introduction to Github Actions

Github Actions lets you run workflows whenever a certain event occurs.

Workflow

A workflow is a configurable automated process that runs whenever a certain event occurs. An event can be the creation of a pull request or pushing (to a certain branch of your choice). It can also be something like the creation of an issue.

Workflows are created inside .github/workflows folder in your project. Each workflow can be defined in a separate YAML file(.yml or .yaml).

Examples of cases where workflows can be created-

  • Automating testing

  • Continuous Integration

  • Continuous Delivery and Deployment

  • Automating building and pushing of docker images to a private registry

  • Sending automated messages to contributors on successful merging of a PR or opening of an issue etc.

Writing test cases

Let's create a simple calculator app and write some tests to help us visualise automated testing later on.

function add(a, b) {
  return a + b;
}
function subtract(a, b) {
  return a - b;
}

function multiply(a, b) {
  return a * b;
}
function divide(a, b) {
  return a / b;
}
module.exports = { add, subtract, multiply, divide };
const { add, subtract, multiply, divide } = require("../index");

describe("test calculator", () => {
  test("test add", () => {
    expect(add(2, 4)).toEqual(6);
  });
  test("test subtract", () => {
    expect(subtract(2, 4)).toEqual(-2);
  });
  test("test multiply", () => {
    expect(multiply(2, 4)).toEqual(8);
  });
  test("test divide", () => {
    expect(divide(4, 2)).toEqual(2);
  });
});

Setup automated testing

Create rule to block direct pushes to the repository

If multiple people are working on the same github repository, it might be a good idea to block direct pushing to the main branch(or whichever branch is being used in production) of the repository and instead raise pull requests for every change. This would give other collaborators time to review the change carefully and not leading to breaking production.

You can create a rule to block direct pushing to any branch ^

Here's what happens when I try to push directly to the main branch-

This will ensure even people with direct access create pull requests and this can ensure everyone goes through the automated tests and reviews before merging.

Create testing workflow

  • Create a testing.yml file inside .github/workflow folder, this will be the file where you configure which branch you want to target, what events do u want it to run on, install and run scripts etc. There are templates available for

  • Here is my testing.yml with some comments:

name: Node.js CI

on:
  pull_request: #event is opening of a Pull requst
    branches: [main] # the workflow works on the main branch

jobs:
  build:
    runs-on: ubuntu-latest 

    strategy:
      matrix:
        node-version: [16.x] # node version where u want the test to run

    steps: #executes the following steps one by one
      - uses: actions/checkout@v3
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm ci # installs dependencies
      - run: npm test # runs the test script

Result

For the following demo, someone raised a PR changing the subtract function into addition. This will cause the workflow to fail.

This can help the person making the PR identify what needs to be fixed. After the change is fixed, the testcases will pass, and the pull request can be merged safely.

In this blog we learned how to setup a workflow for automated testing using Github Actions.

Thanks for reading !!