GitHub Actions To Automate Tests

GitHub Actions To Automate Tests

ยท

3 min read

Automation is really important for performing mundane and redundant tasks like testing. When someone raises a new PR for your project, it's nice to have some tests running automatically. This saves a lot of your time in reviewing the code.

Testing can be done for detecting issues like:

  1. build fails

  2. linting/formatting errors

  3. unexpected results

  4. dependency errors...and more

If your project is maintained on GitHub then GitHub Actions can prove useful in automating things like these. With the use of GHA (abbrev. for GitHub Actions), you can easily automate tasks like:

  1. Releases

  2. Publishing package

  3. Tests

  4. Preview deployments...and a lot more!

So let's take an example to understand how you can also achieve the same for your project.

Let's say you have a Python project in which you have implemented tests using some testing framework like pytest. Now you want to run these tests automatically on every new PR that's raised for this project.

So first of all, create a file like this at the root of your project: .github/workflows/tests.yml

Now inside the tests.yml file, you can paste some code like this:

name: tests

on:
  push:
    branches:
      - master
    paths-ignore:
      - '.gitignore'
      - '**.md'
  pull_request:
    branches:
      - master
    paths-ignore:
      - '.gitignore'
      - '**.md'

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python_version: [
            '3.7',
            '3.11'
        ]
    steps:
    - uses: actions/checkout@v3

    - name: Set up Python ${{ matrix.python_version }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python_version }}

    - name: Install dependencies
      run: pip install -r requirements.txt 

    - name: pytest
      run: pytest -v
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Let's break the code step by step:

  1. name: tests define the name of this GHA

  2. on tell us that on which event should this GHA trigger. In this case, it would run on every new PR or code push that is made to the master branch. Also, notice the paths-ignore config which tells us that changes made to these files should not trigger the tests GHA.

  3. Now we can finally explain the job. We can tell it to run using which server: like in this case it's going to run on an Ubuntu machine.

  4. Using strategy -> matrix we can tell it to run the tests for different versions of Python. This is useful when you want to see if your project is compatible with the older versions or not.

  5. Now inside steps you need to tell the step-by-step procedure to follow before you can run the tests first, you need to check out the current code changes as described in the PR, then set up Python and later on install some dependencies before you can finally go ahead and run the tests.

  6. Each step has a name, a command to execute using run and some other essential config like maybe it needs some env variable. Note that the secrets.GITHUB_TOKEN is not meant to be supplied by you.

  7. If everything is fine then the tests would run successfully and you can see the logs by going to the Actions tab in your GitHub project.

There are many configurations that you can make for your own GHA based on your project. This is just to give you a headstart and show you how easy it's to set up a GHA.

Now you can start using GitHub Actions in your open source projects to level up your automation game! Thank for reading till the end...๐Ÿ˜Š

ย