This is a hands-on practice session. You already know the theory — let's build it.
| File | Description |
|---|---|
main.py |
A simple Calculator class with four operations |
test_main.py |
Unit tests for the Calculator class |
- Click Fork at the top-right of this page.
- Clone your fork locally:
git clone https://github.com/<your-username>/<repo-name>.git
cd <repo-name>python -m unittest test_main.py -vExpected output:
test_add (test_main.TestCalculator) ... ok
test_divide (test_main.TestCalculator) ... ok
test_divide_by_zero (test_main.TestCalculator) ... ok
test_multiply (test_main.TestCalculator) ... ok
test_subtract (test_main.TestCalculator) ... ok
----------------------------------------------------------------------
Ran 5 tests in 0.001s
OK
In main.py, change add to subtract:
def add(self, a, b):
return a - b # bugRun the tests again and observe the failure. Then revert:
def add(self, a, b):
return a + b # fixedConfirm tests pass before moving on.
mkdir -p .github/workflowsCreate .github/workflows/python-tests.yml and write it together with your instructor.
git add .github/workflows/python-tests.yml
git commit -m "Add GitHub Actions workflow to run tests"
git push origin mainOpen the Actions tab on your GitHub repo and watch the workflow run.
Push the bug from Part 3, watch the workflow go red, then fix and push again.
# introduce bug
git add main.py
git commit -m "Introduce bug"
git push origin main
# fix bug
git add main.py
git commit -m "Fix add method"
git push origin main