Step-by-Step Guide to Automating Unit Tests for a Sample Python Calculator application with GitHub Actions
Introduction
In this guide, i will walk through writing unit tests for a simple Python calculator with four functions: add, subtract, multiply, and delete. Then set up GitHub Actions to automate running these tests. This ensures that code is reliable and that any changes do not introduce new bugs.
Step1:
Log into the github account and create a repository.
Step 2: Create the Python Calculator Program
First, we need to create the calculator program. Save the following code in a file named calculator.py
under the folder name “src“.
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def delete(a, b):
return a / b # Assuming 'delete' means division
Step 2: Write Unit Tests for the Calculator
Write unit tests for each of the calculator functions. Create a new file named test_calculator.py
and add the following code:
from src.calculator import add,sub,mul,div
def test_add():
assert add(1,2)==3
assert add(-1,2)==1
def test_sub():
assert sub(5,4)==1
def test_mul():
assert mul(5,3)==15
def test_div():
assert div(6,2)==3
Step 3: Push the code to Github
Add all the required module details in requirement.txt
pytest
Step 4: Push the code to Github
echo "# githubaction-test" >> README.md
git init
git add *
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/vipinputhanveetil/githubaction-test.git
git push -u origin main
Step 5: Understanding GitHub Actions
GitHub Actions is a powerful tool for automating software workflows directly from your GitHub repository. It allows you to define workflows triggered by specific events such as pushes, pull requests, or scheduled times. These workflows can run a series of commands to build, test, and deploy your code.
Key Components:
Workflow: Defined in a YAML file and contains a set of jobs.
Job: A set of steps that execute on the same runner.
Step: An individual task performed by a job, such as running a script or installing a dependency.
Runner: A virtual machine or container where a job runs.
Step 6: Set Up GitHub Actions
To automate the process of running these tests, we will use GitHub Actions. Create a directory named .github/workflows
in the root of your project and add a file named ci.yml
with the following content:
yaml
name: Python CI - Unit test
# Trigger the workflow on any push to the main branch or pull request
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
# Step 1: Check out the code from the repository
- name: Check out code
uses: actions/checkout@v2
# Step 2: Set up Python environment
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.8'
# Step 3: Install dependencies
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# Step 4: Run tests
- name: Run tests
run: pytest
Step 7: Commit and Push Your Code to GitHub
Now, commit your changes and push them to your GitHub repository:
bash
git add .
git commit -m "start unit test"
git push origin main
GitHub Actions will automatically run your tests whenever you push new code or create a pull request.
Step 6: Verify the GitHub Actions Workflow
After pushing your code, go to your GitHub repository and navigate to the Actions tab. You should see your workflow running. Once the tests complete, you will see the results of the tests, indicating whether they passed or failed.
Github: https://github.com/vipinputhanveetil/githubaction-test
Conclusion
By following these steps, you have set up a Python calculator program with unit tests and automated the testing process using GitHub Actions. This setup provides a continuous integration process that helps maintain the quality of your codebase, ensuring that any changes do not introduce new bugs. Automating your tests not only saves time but also increases the reliability of your software. Happy coding!