Skip to content
Go back

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

Published:

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 are 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:

name: Python application

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Starting Selenoid Server (aerokube/selenoid) within actions
        uses: ajinx/selenoid@2.0
      - name: Set up Python 3.9
        uses: actions/setup-python@v2
        with:
          python-version: 3.9
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
      - name: Install plugin
        run: pip install pytest-github-actions-annotate-failures
      - name: Test with pytest
        run: |
          pytest --alluredir=allure-results --browser remote
      - name: Allure results
        uses: simple-elf/allure-report-action@master
        if: always()
        id: allure-report
        with:
          allure_results: allure-results
          allure_report: allure-report
          gh_pages: gh-pages
          allure_history: allure-history
      - name: Deploy report to Github Pages
        if: always()
        uses: peaceiris/actions-gh-pages@v2
        env:
          PERSONAL_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PUBLISH_BRANCH: gh-pages
          PUBLISH_DIR: allure-history

Walkthrough

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

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

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.

runs-on: ubuntu-latest

now a series of workflow steps are executed in order:

- uses: actions/checkout@v2
- name: Starting Selenoid Server (aerokube/selenoid) within actions
  uses: ajinx/selenoid@2.0
  - name: Set up Python 3.9
    uses: actions/setup-python@v2
    with:
      python-version: 3.9
  - name: Install dependencies
    run: |
      python -m pip install --upgrade pip
      pip install -r requirements.txt

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

- name: Install plugin
  run: pip install pytest-github-actions-annotate-failures
  - name: Test with pytest
    run: |
      pytest --alluredir=allure-results --browser remote
  - name: Allure results
    uses: simple-elf/allure-report-action@master
    if: always()
    id: allure-report
    with:
      allure_results: allure-results
      allure_report: allure-report
      gh_pages: gh-pages
      allure_history: allure-history
  - name: Deploy report to Github Pages
    if: always()
    uses: peaceiris/actions-gh-pages@v2
    env:
      PERSONAL_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      PUBLISH_BRANCH: gh-pages
      PUBLISH_DIR: allure-history

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!


Suggest Changes

Have a challenge? Let's Talk


Previous Post
Test Automation - How To Show Current Test Name Before Each Test
Next Post
Test Automation - How To Attach Public IP Adress to Allure report using Pytest and Requests