This is a very simple Python calculator that can add two numbers together. It also includes:
- A test to make sure the calculator works correctly
- A Jenkins setup that automatically runs and tests your code
Perfect for: Complete beginners who want to learn about automation and testing.
simple-calculator/
βββ README.md # This guide you're reading now
βββ app.py # The calculator program (4 math operations)
βββ test.py # Tests to check if the calculator works
βββ requirements.txt # List of Python packages we need
βββ Jenkinsfile # Instructions for Jenkins multibranch pipeline
- 4 functions: add, subtract, multiply, divide
- Asks you to choose which operation you want
- Handles errors (like dividing by zero)
- User-friendly with clear prompts
- Tests all 4 calculator functions
- Uses pytest (a professional testing tool)
- Checks edge cases (like dividing by zero)
- Makes sure your calculator is reliable
- Lists the Python packages we need (like pytest)
- Jenkins will automatically install these
- Keeps track of exact versions for consistency
- Automatically runs when you push code to any branch
- Different behavior for different branches:
devβ Development environmentstagingβ Staging environmentmain/masterβ Production environment
Jenkins is like a helpful robot that:
- Watches your code for changes
- Automatically runs your program
- Automatically runs your tests
- Tells you if something is broken
- Go to jenkins.io
- Download Jenkins for your computer (Windows/Mac/Linux)
- Follow the installation guide on their website
- When done, Jenkins will open in your web browser (usually at http://localhost:8080)
- Click "New Item" in Jenkins
- Enter a name like "My Calculator"
- Select "Pipeline" and click OK
- Scroll down to "Pipeline" section
- Select "Pipeline script from SCM" if you're using Git, or "Pipeline script" for simple testing
If you're just testing locally:
- Select "Pipeline script"
- Copy and paste the contents of your Jenkinsfile into the script box
- Click "Save"
- Click "Build Now"
- Watch Jenkins run your code automatically!
- If everything works, you'll see a green checkmark β
- If something fails, you'll see a red X β
- β Green checkmark: Everything worked!
- Your calculator ran without errors
- All tests passed
- You're ready to make changes and run again
- β Red X: Something failed
- Click on the build number to see what went wrong
- Common issues:
- Python not installed
- File not found
- Syntax error in your code
Problem: Your computer can't find Python Solution:
- Make sure Python is installed
- Try
python3instead ofpython - On Windows, try
pyinstead ofpython
Problem: The test file can't find your main program Solution:
- Make sure both
app.pyandtest.pyare in the same folder - Check that you didn't rename the files
By working with this simple project, you'll understand:
- How to write simple functions
- How to test your code
- How to run Python programs
- Why testing is important
- How to write basic tests
- What it means when tests pass or fail
- How to automatically run your code
- How to catch problems early
- How continuous integration works
Once you're comfortable with this simple calculator:
- Add More Functions: Try adding subtract, multiply, or divide
- Add More Tests: Test edge cases like dividing by zero
- Learn Git: Version control to track your changes
- Try Different Branches: Learn about development vs production
- Add Error Handling: Make your calculator handle invalid inputs
- Read the error message carefully - it usually tells you what's wrong
- Check that all files are in the same folder
- Make sure Python is installed and working
- Try running each step manually first
If you've made it this far and everything is working, you've just:
- β Created your first Python application
- β Written your first test
- β Set up your first automation pipeline
- β Learned the basics of CI/CD
- First do the Jenkins setup if you haven't already.
-
In Jenkins, click "New Item"
-
Enter name: "Calculator Multibranch Pipeline"
-
Select: "Multibranch Pipeline"
-
Click "OK"
-
Configure Branch Sources:
- Click "Add Source" β "Github"
- Project Repository:
(https://github.com/hkm7/calculator-application) - Credentials: None
-
Configure Build Configuration:
- Mode: by Jenkinsfile
- Script Path: Jenkinsfile
-
Save and Scan:
- Click "Save"
- Jenkins will automatically scan and find your branches
- You should see:
dev,staging,mainbranches detected
| Branch | Environment | What Happens |
|---|---|---|
dev |
Development | Tests run, deploys to dev environment |
staging |
Staging | Tests run, deploys to staging for final testing |
main |
Production | Tests run, deploys to production |
- Setup - Installs Python packages from requirements.txt
- Test - Runs all tests with pytest
- Run App - Confirms the app is ready
- Deploy - Branch-specific deployment messages
git checkout dev
echo "# Development update" >> README.md
git add .
git commit -m "Test dev branch"git checkout staging
git merge dev # Get latest changes
git commit -m "Test staging branch"git checkout main
git merge staging # Get tested changes
git commit -m "Test production branch"After each commit, check Jenkins - it should automatically:
- Detect the branch changes
- Run the pipeline for that branch
- Show different deployment messages
# If "python" command doesn't work, use "python3"
# You can create an alias:
echo "alias python=python3" >> ~/.bashrc
echo "alias pip=pip3" >> ~/.bashrc
source ~/.bashrc# If you get permission errors with pip:
pip3 install --user -r requirements.txt
# Or use virtual environment (recommended):
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txtStarted by branch indexing
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] stage (Setup)
[Pipeline] { (Setup)
+ python3 --version
Python 3.8.10
+ pip3 install --upgrade pip
+ pip3 install -r requirements.txt
[Pipeline] }
[Pipeline] stage (Test)
[Pipeline] { (Test)
+ python3 -m pytest test.py -v
===== test session starts =====
test.py::test_add PASSED
test.py::test_subtract PASSED
test.py::test_multiply PASSED
test.py::test_divide PASSED
test.py::test_divide_by_zero PASSED
===== 5 passed in 0.02s =====
[Pipeline] }
[Pipeline] stage (Deploy)
π Deploying to PRODUCTION environment
All calculator functions are available in production!
[Pipeline] End of Pipeline
Finished: SUCCESS