0
0
PyTesttesting~10 mins

Why integration tests verify components together in PyTest - Test Execution Impact

Choose your learning style9 modes available
Test Overview

This test checks that two components, a calculator and a logger, work correctly when used together. It verifies that the calculator adds numbers and the logger records the operation.

Test Code - pytest
PyTest
import pytest

class Calculator:
    def __init__(self, logger):
        self.logger = logger

    def add(self, a, b):
        result = a + b
        self.logger.log(f"Added {a} and {b} to get {result}")
        return result

class Logger:
    def __init__(self):
        self.messages = []

    def log(self, message):
        self.messages.append(message)

@pytest.fixture
def logger():
    return Logger()

@pytest.fixture
def calculator(logger):
    return Calculator(logger)


def test_calculator_add_logs_operation(calculator, logger):
    result = calculator.add(3, 4)
    assert result == 7
    assert logger.messages[-1] == "Added 3 and 4 to get 7"
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test startspytest test runner initialized-PASS
2Fixtures create Logger and Calculator instancesLogger with empty messages list, Calculator linked to Logger-PASS
3Calculator.add(3, 4) is calledCalculator computes sum 7-PASS
4Logger.log records message 'Added 3 and 4 to get 7'Logger messages list contains one entryLogger messages list last entry is 'Added 3 and 4 to get 7'PASS
5Assert result == 7Result is 77 == 7PASS
6Assert logger message is correctLogger messages list last entry is 'Added 3 and 4 to get 7'Message matches expected stringPASS
7Test endsAll assertions passed-PASS
Failure Scenario
Failing Condition: Logger does not record the addition message or Calculator returns wrong sum
Execution Trace Quiz - 3 Questions
Test your understanding
What does the integration test verify?
AThat Calculator and Logger work correctly together
BThat Calculator adds numbers correctly alone
CThat Logger stores messages correctly alone
DThat Calculator subtracts numbers correctly
Key Result
Integration tests check that separate parts work together correctly, catching problems that unit tests alone might miss.