Continuous Testing — GitHub Actions
In this article we will see how we can achieve continuous testing using GitHub Actions.
Ever since Microsoft acquired GitHub in 2018, things have not been the same in the Microsoft world. Microsoft had not been known to contribute too much towards the open source community. However, this new move “heightened Microsoft’s focus on open-source development, aimed to increase enterprise use of GitHub and bring Microsoft’s developer tools and services to new audiences”.
By joining forces with GitHub, CEO Satya Nadella said, “we strengthen our commitment to developer freedom, openness and innovation.”
A very heavy focus of Microsoft Azure DevOps pipeline team is being channelized towards actively developing GitHub Actions and integrations around it. Its hard to say if Microsoft will make GitHub Actions as their only CI-CD pipeline mechanism in the future, but there is a lot of buzz in the internet surrounding this.
Speculations are running wild as Microsoft is investing heavily on Azure DevOps migration to GitHub Actions. An example is this application which parses an Azure DevOps build yaml to a GitHub Actions yaml.
Home - Pipelines To Actions
Paste your Azure Pipelines YAML into the left textbox, then use the "Process YAML" button to generate GitHub Actions…
But we as Test Automation engineers need to be ahead of the curve, and learn both worlds.
So what actually are GitHub Actions ?
GitHub Actions allow you to configure responses to any event (known as GitHub Event) that happens IN or TO a repository. These events can range from Pull Requests, joining of a new Contributor, creation of Issues, merging of Pull Requests etc.
Some examples :
- If a new Contributor joins, you can configure a GitHub Action to show him/her a nice Welcome Message!
- If a new Issue is created, you can create an automated workflow using GitHub Actions, to sort → label → assign → run a script to reproduce.
- Using GitHub Actions, you can configure a full CI-CD implementation.
In this article we will create a simple GitHub Actions Workflow to execute automated tests written in Cypress. The code and the details are available in the following GitHub Repo.
Enough talk, lets get started.
Step 1: Check in your code to GitHub and navigate to the Actions
Step 2: GitHub automatically detects the type of project present in your repository and accordingly suggests to you the corresponding Workflow type. Choose the one that suits you. You will of course be able to edit it later.
I chose the Node.js card and clicked on [Set up this workflow].
This will automatically create a folder structure .github/workflows and will create a yaml file inside it which will determine how the workflow will work. For me, the yaml structure is like this :
branches: [ main ]
branches: [ main ]jobs:
runs-on: ubuntu-latest steps:
- uses: actions/checkout@v2 - name: Use Node.js 10.x
node-version: 10.x - name: Install dependencies
run: npm install - name: Run Cypress Test
run: npm run cy:run - name: Upload Results
Let’s go through the yaml segments individually to understand what each of them stands for.
The following depicts a GitHub event that triggers the workflow. So this means whenever there is a push or pull request into the [main] branch , the workflow will start.
branches: [ main ]
branches: [ main ]
The following depicts the kind of OS we are choosing for our workflow.
Checking out the code
The following will checkout the code from the repository. More details can be found here.
Setting up of Node environment
The following part will set up Node 10.x environment. Further details can be found here.
- name: Use Node.js 10.x
Install project dependencies
The following part will install the dependencies after reading it from the package.json file present in your root directory.
- name: Install dependencies
run: npm install
Execute the tests
The following part will execute cypress tests.
- name: Run Cypress Test
run: npm run cy:run
cy:run is the name of script to execute the tests. This is present in package.json
"cypress:open": "cypress open",
"cy:run": "npx cypress run"
Upload the Test Results
The following part will upload the test results to Artifacts Blob from where they can be downloaded later. Further details can be found here.
- name: Upload Results
Step 3: Click on [Start Commit] button. This will immediately trigger the workflow.
The entire execution will run on GitHub-hosted virtual machines, unless it is configured to run on user hosted machines.
If all goes well, you should be able to see that the build has successfully executed and all the steps of your pipeline are marked completed.
Step 4: View Logs and Results
The logs can be viewed against each step. For me against the “Run Cypress Test” step, the logs are visible as follows:
The results, as described earlier, are uploaded to GitHub blob and can be downloaded from the “Artifacts” as shown below.
You have successfully set up an automated test execution workflow using GitHub Actions!