☆ Exercise 3

In this exercise we’ll add automated testing to the statistics library using GitHub Actions. This ensures all future pull requests are automatically tested before merging.

Part 1: Understanding the Current Tests

Step 1: Explore the existing test file

Open test_stats_lib.py in your repository. You’ll see pytest tests for the statistics functions.

Step 2: Run tests locally

# Run the tests
pytest test_stats_lib.py

# Run with coverage report
pytest --cov=stats_lib test_stats_lib.py

You should see all tests pass with the merged code from the previous exercises.


Part 2: Create a GitHub Actions Workflow

The Repository Owner leads this part, but everyone should follow along in their own clone.

Step 1: Create the workflow directory structure

mkdir -p .github/workflows

Step 2: Create the workflow file

Create the workflow file .github/workflows/pytest.yml. You can use the workflow in https://github.com/Bristol-Training/demo-calc-lib as a template.

Step 3: Ensure requirements.txt exists

If it doesn’t exist, create it. Since this is a simple library, the file might be minimal or empty, but it should exist for the workflow.

Step 4: Commit and push

git add .github/workflows/pytest.yml
git add requirements.txt  # if you created it
git commit -m "Add GitHub Actions workflow for automated testing"
git push origin main

Step 5: Verify the workflow runs

  1. Go to your GitHub repository
  2. Click the Actions tab
  3. You should see your workflow running (yellow circle) or completed (green checkmark)
  4. Click on the workflow run to see detailed logs

Automated testing is now active!


Part 3: Test the Workflow with a Pull Request

Now let’s see GitHub Actions in action by creating a pull request with failing tests.

Step 1: Create a buggy branch

git checkout main
git pull origin main
git checkout -b feature/buggy-median

Step 2: Introduce a bug in stats_lib.py

Find the median function and change it to:

def median(data):
    """Calculate median (with an intentional bug)."""
    clean_data = [x for x in data if x is not None]
    
    if not clean_data:
        raise ValueError("No valid data points")
    
    sorted_data = sorted(clean_data)
    n = len(sorted_data)
    
    # BUG: Wrong calculation!
    return sorted_data[0]  # Always returns the first element

Step 3: Commit and push (without testing locally!)

git add stats_lib.py
git commit -m "Update median calculation"
git push origin feature/buggy-median

Step 4: Create Pull Request

  1. Create the pull request on GitHub
  2. Watch the checks run
  3. Notice the red X
  4. Click “Details” to see which test failed
  5. The PR shows “Some checks were not successful”

Step 5: Fix the bug

# Fix the median function back to correct implementation
git add stats_lib.py
git commit -m "Fix median calculation"
git push origin feature/buggy-median

Step 6: Create Pull Request

  1. Go to GitHub and create a pull request
  2. Notice the yellow circle appears next to your PR - checks are running!
  3. Wait for it to turn into a green checkmark
  4. The PR shows “All checks have passed”

Repository Owner can merge this PR with confidence!