Verify addition function returns correct sum
Preconditions (2)
✅ Expected Result: Pytest runs the test and shows that test_add passed successfully
def add(a, b): return a + b def test_add(): result = add(2, 3) assert result == 5
The add function simply returns the sum of two numbers.
The test function test_add calls add(2, 3) and stores the result.
Then it uses assert to check if the result equals 5.
Pytest automatically finds functions starting with test_ and runs them.
This simple test confirms the addition works as expected.
Now add data-driven testing with 3 different pairs of numbers and their expected sums
test_.test_ are recognized as tests by default.test_, so test_add is correct.1 + 1 == 2 is true, so the test will pass.def test_example():
x = 5
y = 2
assert x - y == 3x - y which is 5 - 2 = 3.3 == 3, which is true, so no error is raised.def test_sum():
total = 2 + 2
assert total = 4= which is assignment, not comparison. It should be ==.test_ and indentation is correct, so no error there.multiply(a, b) that returns the product of two numbers. Which PyTest test function correctly tests that multiply(3, 4) returns 12?test_, so test_multiply is correct.== and the expected result is 12, so assert multiply(3, 4) == 12 is correct.Pass the test and this pattern moves unknown