This test checks if adding two numbers and then multiplying the sum by a third number works correctly together.
PyTest
def add(x, y):
return x + y
def multiply(x, y):
return x * y
def combined_operation(a, b, c):
sum_result = add(a, b)
return multiply(sum_result, c)
def test_combined_operation():
result = combined_operation(2, 3, 4)
assert result == 20# Run the testif __name__ == '__main__':
test_combined_operation()
print('Test passed')
OutputSuccess
Important Notes
Integration tests are slower than unit tests because they test more code together.
They help find issues in how parts connect, not just if parts work alone.
Keep integration tests focused on key interactions to avoid long test times.
Summary
Integration tests check combined parts of a program.
They find problems that unit tests might miss.
Use them to ensure components work well together.
Practice
(1/5)
1. Why do integration tests verify components together in pytest?
easy
A. To check if different parts of the program work well together
B. To test a single function in isolation
C. To measure the speed of the program
D. To check the spelling in the code comments
Solution
Step 1: Understand the purpose of integration tests
Integration tests focus on testing how different parts or components of a program work together as a group.
Step 2: Compare with other test types
Unit tests check single functions alone, while integration tests check combined parts to find issues missed by unit tests.
Final Answer:
To check if different parts of the program work well together -> Option A
Quick Check:
Integration tests verify combined components = A [OK]
Hint: Integration tests check combined parts, not single functions [OK]
Common Mistakes:
Confusing integration tests with unit tests
Thinking integration tests check performance
Assuming integration tests check code comments
2. Which pytest code snippet correctly shows an integration test combining two components?
easy
A. def test_multiply(): assert multiply(2, 3) == 6
B. def test_add(): assert add(2, 3) == 5
C. def test_subtract(): assert subtract(5, 3) == 2
D. def test_add_and_multiply(): assert multiply(add(2, 3), 4) == 20
Solution
Step 1: Identify integration test code
Integration tests combine multiple components; here, add and multiply are used together in one test.
Step 2: Check other options
Options A, B, and D test single functions alone, so they are unit tests, not integration tests.