No description
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2025-12-12 14:41:25 +00:00
__pycache__ chore: renaming 2025-12-12 14:41:25 +00:00
calculator.py chore: renaming 2025-12-12 14:41:25 +00:00
main.py chore: renaming 2025-12-12 14:41:25 +00:00
README.md chore: change md to readme.md 2025-12-12 14:38:29 +00:00
test_calculator.py chore: renaming 2025-12-12 14:41:25 +00:00

Unit Testing in Python - Training Project

Overview

This project demonstrates fundamental unit testing concepts for junior software engineers. It contains a simple calculator function with comprehensive unit tests to illustrate best practices in test-driven development.

Project Structure

unit-testing-project/
├── README.md
├── main.py              # Entry point - controls execution
├── calculator.py        # Contains the add function with error handling
└── test_calculator.py   # Comprehensive unit tests

What You'll Learn

  • Writing testable functions with proper error handling
  • Creating comprehensive unit tests using Python's unittest framework
  • Testing both happy paths and edge cases
  • Validating error handling in your code
  • Running and interpreting test results

Prerequisites

  • Python 3.6 or higher
  • No external dependencies required (uses Python's built-in unittest module)

Setup Instructions

1. Create Project Directory

mkdir unit-testing-project
cd unit-testing-project

2. Create the Files

Create three Python files and copy the code from each section below:

  • calculator.py
  • test_calculator.py
  • main.py

3. Verify Setup

Ensure all three files are in the same directory.

Running the Project

Run the Main Application

python main.py

This executes the calculator function with sample inputs.

Run the Unit Tests

python -m unittest test_calculator.py

Run Tests with Verbose Output

python -m unittest test_calculator.py -v

Run a Specific Test

python -m unittest test_calculator.TestCalculator.test_add_positive_numbers

Understanding the Code

calculator.py

Contains the add() function that:

  • Accepts two numeric parameters
  • Validates input types
  • Returns the sum
  • Raises appropriate exceptions for invalid inputs

test_calculator.py

Contains the test suite that validates:

  • Happy paths: Normal, expected use cases
  • Edge cases: Boundary conditions (zero, negatives, floats)
  • Error conditions: Invalid inputs and type errors
  • Special values: Very large numbers, infinity, NaN

main.py

Demonstrates practical usage of the calculator function with various test cases.

Test Coverage Breakdown

The test suite includes:

  1. Basic functionality tests - positive integers, negative numbers, zero
  2. Type validation tests - strings, None, lists, mixed types
  3. Float handling tests - decimal numbers, precision
  4. Edge case tests - very large numbers, infinity, NaN
  5. Error message validation - ensures clear error messages

Expected Test Results

When you run the tests, you should see output like:

..........
----------------------------------------------------------------------
Ran 10 tests in 0.001s

OK

Each dot represents a passed test. If a test fails, you'll see an 'F' instead.

Common Testing Patterns Demonstrated

  1. Arrange-Act-Assert: Each test sets up data, executes the function, and verifies results
  2. Testing exceptions: Using assertRaises() to verify error handling
  3. Descriptive test names: Clear names that describe what's being tested
  4. Test isolation: Each test is independent and can run in any order

Extending the Project

Try these exercises:

  1. Add a subtract() function and write tests for it
  2. Add a multiply() function that handles zero properly
  3. Write a test that deliberately fails to see what failure looks like
  4. Add test coverage for division with zero-division error handling

Key Takeaways

  • Unit tests validate individual functions in isolation
  • Good error handling makes functions more robust
  • Tests should cover happy paths, edge cases, and error conditions
  • Clear test names serve as documentation
  • Running tests frequently catches bugs early

Additional Resources

Questions to Consider

  1. Why is it important to test error conditions, not just successful operations?
  2. How do unit tests help when refactoring code?
  3. What happens if you modify the add() function but forget to update tests?
  4. How would you test a function that depends on external services?