Why do integration tests verify components together instead of testing them separately?
Think about what happens when parts of a system connect and communicate.
Integration tests ensure that different parts of the system work together properly, catching issues that unit tests might miss when components are tested alone.
What will be the output of this pytest integration test when both components work correctly?
def component_a(): return 5 def component_b(x): return x * 2 def test_integration(): result_a = component_a() result_b = component_b(result_a) assert result_b == 10
Check the values returned and how they are used in the assertion.
The test calls component_a which returns 5, then component_b doubles it to 10. The assertion checks if result_b equals 10, which is true, so the test passes.
Which assertion correctly verifies that two components integrate properly by checking the combined output?
def component_x(): return [1, 2] def component_y(data): return [x * 3 for x in data] result = component_y(component_x())
Think about what component_y does to the list from component_x.
component_x returns [1, 2]. component_y multiplies each element by 3, so the result is [3, 6]. The assertion must check for this exact list.
This integration test fails. What is the cause?
def service_one(): return {'status': 'ok', 'data': 10} def service_two(response): return response['data'] / 0 def test_services(): res = service_one() output = service_two(res) assert output == 10
Look at the operation inside service_two carefully.
service_two divides by zero, which raises a ZeroDivisionError and causes the test to fail before the assertion.
Which pytest fixture setup is best for integration tests that require initializing multiple components together?
Consider setup and cleanup for multiple components in integration tests.
Option A uses a fixture with yield to provide components and then clean them up after tests, which is best practice for integration tests involving multiple components.