What if your code could check itself every time you change it?
Why Unit testing in Testing Fundamentals? - Purpose & Use Cases
Imagine you just wrote a small piece of code to calculate discounts for a shopping app. Every time you change something, you open the app and try different prices manually to see if the discount works correctly.
This manual checking is slow and tiring. You might forget some price cases or make mistakes while testing. If the code changes often, you waste a lot of time repeating the same checks, and bugs can sneak in unnoticed.
Unit testing lets you write small automatic checks for each part of your code. These tests run quickly and tell you immediately if something breaks, so you don't have to test everything by hand every time.
Run app, enter price 100, check discount manually Run app, enter price 200, check discount manually
assert calculate_discount(100) == 10 assert calculate_discount(200) == 20
Unit testing makes your code safer and saves time by catching errors early with automatic checks.
A developer changes a function to add a new feature. Thanks to unit tests, they quickly see if the old discount calculations still work without opening the app and clicking around.
Manual testing is slow and error-prone.
Unit tests automate checks for small code parts.
They help catch bugs early and speed up development.
Practice
unit testing in software development?Solution
Step 1: Understand unit testing scope
Unit testing focuses on testing small, individual parts of code, like functions or methods.Step 2: Compare with other testing types
Testing the entire system is integration or system testing, not unit testing.Final Answer:
To check small parts of code work correctly -> Option CQuick Check:
Unit testing = small code parts check [OK]
- Confusing unit testing with system testing
- Thinking unit tests check UI or deployment
- Believing unit tests run the full application
assert?Solution
Step 1: Recall Python assert syntax
Python usesassert expressionwhere expression must be True for test to pass.Step 2: Check each option
assert add(2, 3) == 5 uses correct syntax:assert add(2, 3) == 5. assert add(2, 3) = 5 uses single = which is assignment, invalid here. assert(add(2, 3) = 5) uses single = which is assignment, invalid syntax. assert add(2, 3) equals 5 uses invalid keyword 'equals'.Final Answer:
assert add(2, 3) == 5 -> Option AQuick Check:
assert with == for comparison [OK]
- Using single '=' instead of '==' in assert
- Writing assert with invalid keywords
- Adding unnecessary parentheses in assert
def multiply(a, b):
return a * b
assert multiply(3, 4) == 12
assert multiply(0, 5) == 0
assert multiply(-1, 3) == -3
assert multiply(2, 2) == 5Solution
Step 1: Evaluate each assertion
multiply(3,4) = 12 matches expected 12 (pass). multiply(0,5) = 0 matches 0 (pass). multiply(-1,3) = -3 matches -3 (pass). multiply(2,2) = 4 but expected 5 (fail).Step 2: Identify failing assertion
The last assertion expects 5 but function returns 4, so it fails.Final Answer:
The last assertion fails -> Option AQuick Check:
4 != 5 causes failure [OK]
- Assuming all assertions pass without checking values
- Confusing multiplication results
- Ignoring the last assertion
def divide(a, b):
return a / b
assert divide(10, 2) == 5
assert divide(5, 0) == 0Solution
Step 1: Analyze function behavior
Function divides a by b. Dividing by zero is not allowed and causes an error.Step 2: Check assertions
First assertion divides 10 by 2, which is valid. Second assertion divides 5 by 0, which causes a runtime ZeroDivisionError.Final Answer:
Division by zero causes runtime error -> Option BQuick Check:
Divide by zero error in second assert [OK]
- Ignoring division by zero error
- Thinking assertions need parentheses in Python
- Assuming function returns only integers
Solution
Step 1: Understand the edge case
An empty list has no maximum value, so the function should handle this gracefully, often by returning None or raising an error.Step 2: Evaluate test cases
assert max_number([]) == None tests the empty list case expecting None, which is appropriate. Other options test normal lists, not empty.Final Answer:
assert max_number([]) == None -> Option DQuick Check:
Empty list test expects None [OK]
- Not testing empty list edge case
- Using normal list tests for empty case
- Assuming empty list returns zero
Ready to prove it?
Pass the test and this pattern moves unknown
