Jul 13, 2023
3 min read
Automate Unit Test Case Validation With Github Action
#github-action
#node.js
#javascript
Hi There! 👋
Sometimes you might miss validating your unit test cases before pushing your code and this might be trouble later during the project release or deployments
What if you can automate this test case validation just before you merge your code?
Yes, You can do that using Github Action.
Note: This Automation will work only if your project is on Github.
So, What is Github Action?
Github Action is a platform that allows you to automate your build, test, and deployments.
Now, Let’s see how we can setup github action to automate test case validation
Step 1: New Repository
Create a new repository on Github
Step 2: Setup a Node.js Project
Download the starter project from this repository or if you have an existing project you can use it.
For this tutorial, I am going to use the node.js project. But the workflow can be used for other type of projects as well, the only thing you that you have to do is updating shell scripts in the working based on your project
Step 3: Git Workflow Setup
Create a .github
directory and create a workflows
directory inside it.
Note the naming is important otherwise, the workflow will not be triggered
Step 4: Create a YAML File
Create a YAML file inside the workflows directory. We will be writing instructions that have to be executed.
Step 5: Write Instructions To Automate Unit Test Cases
Add the following script to the YAML file,
1name: Unit Test Cases Execution
2on: pull_request
3jobs:
4 unit-test:
5 runs-on: ubuntu-latest
6 steps:
7 - name: Clone Repository
8 uses: actions/checkout@v3
9 - name: Setup Node.js
10 uses: actions/setup-node@v3
11 with:
12 node-version: 19.9.0
13 - name: Install Dependencies
14 run: npm ci
15 - name: Execute Test Cases
16 run: npm run test
Line 1
- Name of the workflowLine 2
- On which event the workflow should be triggered like push, pull_request.Line 4
- Name of the jobLine 5
- On which runner(platform window, linux etc) we want to execute the test cases, in our case we are using ubuntu. Check out other runnersLine 6 - 16
- Steps to be executed when running the jobLine 7 - 8
- It is the First Step, and here we are cloning the repository into the runner. To clone the repository we are using existing github action calledactions/checkout@v3
.Line 9 - 12
- To Install node into the runner, by default the runner will come with node pre installed, At the time of writing this blog the default node version the runner use is V18.16.1. Since I want a specific version we are reinstalling the node with specific version V19.9.0Line 13 - 14
- To Install the required dependencies.Line 15 - 16
- Execution of the test cases.
Now push the changes and when you rise a PR, your workflow will be started automatically and it will execute the unit test cases.