☆ 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.pyYou 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/workflowsStep 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 mainStep 5: Verify the workflow runs
- Go to your GitHub repository
- Click the Actions tab
- You should see your workflow running (yellow circle) or completed (green checkmark)
- 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-medianStep 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 elementStep 3: Commit and push (without testing locally!)
git add stats_lib.py
git commit -m "Update median calculation"
git push origin feature/buggy-medianStep 4: Create Pull Request
- Create the pull request on GitHub
- Watch the checks run
- Notice the red X ❌
- Click “Details” to see which test failed
- 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-medianStep 6: Create Pull Request
- Go to GitHub and create a pull request
- Notice the yellow circle appears next to your PR - checks are running!
- Wait for it to turn into a green checkmark ✅
- The PR shows “All checks have passed”
Repository Owner can merge this PR with confidence!