Introduction
In this article, I will explain how to build a Pre-Merge Testing pipeline using GitHub actions.
The project was developed together withElias Shourosh.
The technological stack used to implement the solution is:
Programing language:Python
Testing Framework:pytest
Reporting Framework:Allure
What is GitHub Actions?
according tothis 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 are free and support public repositories. The article will be demonstrated using my public repository, which contains a suite of E2E tests written in Python.
The Importance of Pre-Merge Testing
While code reviews play a vital role in catching errors, they may not be enough to guarantee overall code quality and consistency. Pre-merge testing, coupled with code reviews, is a critical step to ensure that every change to the main codebase undergoes thorough testing, preventing regressions and maintaining a high standard of code quality. Additionally, keeping the codebase consistent and adhering to coding standards can be challenging without automated tools. Manual formatting and sorting of imports become tedious as the codebase grows, leading to inconsistencies and reduced readability.
Implementing the Solution
We need to create a .github
folder at the root of our project, and inside it create a workflow folder. Inside it, we create our workflows which are YAML files.
The workflow can be found here
Walkthrough
We are running the workflow on every push or PR to the main branch.
It runs the build on an Ubuntu instance provided by GitHub, here is thespecification 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.
now a series of workflow steps are executed in order:
- Check out the latest code with the following action:
- Automatically checking all code is formatted according to standards using black:
- Automatically checking all imports are sorted according to standards using isort:
- Installing Python with the following action:
- Installing the project dependencies usingpoetry with the following steps:
- Running a single pre-merge test marked with the devRun marker (-m devRun).
in our case, it’s a simple test that just opens the base URL and ensures we actually got there:
this test could be far more complicated, but this one serves the purpose of our project.
the rest of the pipeline uploaded allure results to GitHub Pages.
Now with this pipeline, we can see that code is checked for formatting and imports ordering automatically, the workflow will not continue if these steps will fail.
our pre-merge test is also passing:
Now that the pre-merge testing has been successfully completed, validating the code changes for quality and consistency, we can confidently proceed with the code review process. The automated pre-merge testing using GitHub Actions has ensured that potential regressions and critical errors are identified and addressed beforehand, providing a solid foundation for the code review.
In conclusion
In this article, we reviewed the creation of a Pre-Merge Testing pipeline using GitHub actions python, and pytest.
Further reading on GitHub action features can be found in this link.
Happy testing!