Test Automation - How To Build a CI/CD Pipeline Using Pytest and GitHub Actions

Nir Tal
Cloud Computing
May 4, 2021

Introduction

In this article, I will explain how to build a CI/CD Testing pipeline using GitHub actions. The work on this project was developed together with Elias Shourosh.

link to the project.

The technological stack used to implement the solution is:

Programing language: Python

Testing Framework: pytest

Reporting Framework: Allure

What is Github Actions?

according to this article:

GitHub Actions is an API for cause and effect on GitHub: orchestrate any workflow, based on any event, while GitHub manages the execution, provides rich feedback, and secures every step along the way. With GitHub Actions, workflows and steps are just code in a repository, so you can create, share, reuse, and fork your software development practices.

GitHub actions is free and support public repositories. the article will be demonstrated using my public repository which contains a suite of E2E tests written in python.

Implementing the Solution

We need to create a ".github" folder into the root of our project, inside it create a workflow folder. Inside it, we create our workflows which are YAML files.

No alt text provided for this image

This is the workflow content:

No alt text provided for this image

Walkthrough

We are running the workflow on every push or PR to the main branch.

No alt text provided for this image

It runs the build on an Ubuntu instance provided by GitHub, here is the specification for Github hosted runners. Of course, there is also an option of creating custom runners. this workflow is pretty simple so I used a hosted runner.

No alt text provided for this image

now a series of workflow steps are executed in order:

  • Checking out the latest code with the following action:

No alt text provided for this image

  • Deploying a disposable selenium grid using Selenoid is done in order to simplify the run of the Selenium driver (ChromeDriver) at the runner, the grid terminates when the run is over. this is done with the following action:

No alt text provided for this image

  • Installing python with the following action:

No alt text provided for this image

  • Installing the project dependencies using pip using a bash command:

No alt text provided for this image

Installing a Pytest plugin that annotates test failures in GitHub actions by the following bash command:

No alt text provided for this image

  • Running our test suite also setting the allure result directory and that run should happen on the grid and not locally:

No alt text provided for this image

  • After the run is finished we are generating the allure report with the following action:

No alt text provided for this image

  • We deploy the report to the project Github pages with the following action:

No alt text provided for this image

The result of the run is displayed in the PR status as a check, we can determine if this check.

No alt text provided for this image

We can determine in the project settings if the check is mandatory for the push.

The result history of the run can be viewed in the action tab.

No alt text provided for this image

Allure reports can be found at the Github pages of the project:

No alt text provided for this image

In conclusion

In this article, we reviewed the creation of a CI/CD automation pipeline using GitHub actions python and pytest.

Further reading on GitHub actions features can be found in this link.

Happy testing!

Keep Reading

Newsletter EuropeClouds.com

Thank you! Your submission has been received!

Oops! Something went wrong while submitting the form