Automating Testing and CI with GitHub Actions: A Rövarspråket Case Study

If you’re a developer who wants to ensure that your code is always working correctly, continuous integration (CI) is your best friend. In this blog post, I’ll walk you through how I set up GitHub Actions for one of my Python projects that encodes text using the playful Rövarspråket language, and how you can do the same in your projects.

What is GitHub Actions?

GitHub Actions is a CI/CD (Continuous Integration/Continuous Deployment) platform that allows you to automate tasks like building, testing, and deploying your code. In short, GitHub Actions can run automated scripts whenever you push code, submit a pull request, or even on a schedule.

For my project, the goal was to ensure that every time I made changes to the Rövarspråket encoding code or its associated tests, GitHub Actions would automatically run my test suite to ensure everything still worked. Let’s dive into how I set it up!

The Project: Rövarspråket Encoder

Before jumping into the GitHub Actions setup, let me give you a brief overview of the project. The Rövarspråket language is a fun encoding game where every consonant in a word is doubled, with an “o” placed in between. For example, the word “stubborn” becomes “sostotubobboborornon.”

The project includes:

  • A Python script (Rover.py) that implements the encoding rules.
  • A test file (test_rover.py) that uses pytest to check if the encoding works correctly.

Setting Up GitHub Actions for the Project

Step 1: Create the Workflow File

To automate testing, we need to create a special file in the repository that tells GitHub what to do. This file is located in .github/workflows/ and is typically named something like python-app.yml.

Here’s what mine looks like:

name: Python CI

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Check out the code
      uses: actions/checkout@v2

    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.x'

    - name: Install dependencies
      run: |
        pip install pytest pytest-cov

    - name: Run tests with coverage
      run: |
        pytest --cov=Rover --cov-report=html

    - name: Upload HTML coverage report
      uses: actions/upload-artifact@v3
      with:
        name: coverage-html-report
        path: htmlcov/

Breakdown of the Workflow:

  1. Trigger on Push or Pull Request: The workflow is triggered every time you push code or create a pull request, ensuring that tests are run automatically.
  2. Checkout Code: The actions/checkout@v2 step checks out the repository code so that it can be accessed by the workflow.
  3. Set up Python: The actions/setup-python@v2 step sets up a Python environment, and the python-version option ensures you’re using the right version of Python.
  4. Install Dependencies: This step installs the necessary dependencies for the project, including pytest for running tests and pytest-cov for generating coverage reports.
  5. Run Tests and Generate HTML Coverage: The pytest --cov=Rover --cov-report=html command runs the tests and generates an HTML coverage report in the htmlcov/ directory.
  6. Upload HTML Coverage Report: The actions/upload-artifact@v3 step uploads the htmlcov/ directory as an artifact, which can be downloaded from GitHub after the workflow completes.

Step 2: Commit and Push the Workflow

Once the workflow file is created, commit it to your repository and push it to GitHub:

git add .github/workflows/python-app.yml
git commit -m "Set up GitHub Actions for automated testing and HTML coverage report"
git push

Now, every time you push code or create a pull request, GitHub Actions will automatically run your tests and generate a coverage report.

Viewing the HTML Coverage Report

Once your workflow completes, follow these steps to view the HTML coverage report:

  1. Go to the “Actions” Tab: Navigate to the “Actions” tab in your GitHub repository.
  2. Select the Workflow Run: Choose the latest workflow run (green checkmark for success, red X for failure).
  3. Download the Artifact: Scroll to the “Artifacts” section and download the coverage-html-report.
  4. Open the Report: Inside the downloaded folder (htmlcov/), open index.html in your browser to see which lines of code were covered by tests.

You can find the complete project on GitHub here: GitHub Project Link.

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*
You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>