Automate testing for a simple calculator add function using black-box and white-box approaches
Preconditions (2)
✅ Expected Result: All tests pass confirming correct addition behavior and code logic
import pytest # Simple calculator add function def add(a: int, b: int) -> int: return a + b # Black-box tests: test only inputs and outputs def test_add_black_box_positive(): # Test adding positive numbers assert add(2, 3) == 5 def test_add_black_box_negative_and_positive(): # Test adding negative and positive number assert add(-1, 1) == 0 def test_add_black_box_zeros(): # Test adding zeros assert add(0, 0) == 0 # White-box test: test internal logic explicitly def test_add_white_box_logic(): # Since add is a simple return of a + b, test with multiple values for a, b in [(10, 5), (-3, -7), (0, 4)]: expected = a + b result = add(a, b) assert result == expected, f"Expected {expected} but got {result}" if __name__ == "__main__": pytest.main()
The add function is a simple addition returning a + b.
Black-box tests test_add_black_box_positive, test_add_black_box_negative_and_positive, and test_add_black_box_zeros check the function output for given inputs without looking at the code.
The white-box test test_add_white_box_logic runs multiple inputs and compares the result to the expected sum, explicitly verifying the logic inside the function.
Using pytest allows simple assert statements and easy test running.
Each test is independent and clearly named to show if it is black-box or white-box testing.
Now add data-driven testing with 3 different input pairs for the add function
Pass the test and this pattern moves unknown