Infrastructure modules depend on one another: a server may require a network name and IP range, while the network module must supply those exact values. Contract testing statically compares module inputs and outputs with an expected value or format, protecting integrations as modules evolve. section 6.3
Apply contract testing to parameterized infrastructure modules:
- Create the dependency with representative inputs, such as a network factory module receiving a name and CIDR range. section 6.3
- Verify that the output has the required type or facade, then check each required attribute and value. section 6.3
- Run contract tests after unit tests and before integration tests, catching dependency mistakes before temporary infrastructure is created. section 6.7.1
- Rewrite unit and contract tests when an infrastructure tool changes state or metadata formats; preserve higher-level tests where functionality remains stable. section 13.3.2
Network facade contract
A pytest fixture creates a network with the name "hello-world" and CIDR range "10.0.0.0/16". Assertions verify that the outputs are a NetworkFacade and contain the expected network name and IP address range. A failure signals that a dependent server may not be able to create itself on the network. section 6.3
Listing 6.3 Contract test to compare the module outputs with inputs
from network import NetworkFactoryModule, NetworkFacade
import pytest
network_name = 'hello-world' #1
network_cidr_range = '10.0.0.0/16' #2
@pytest.fixture
def network_outputs(): #3
network = NetworkFactoryModule( #4
name=network_name, #4
ip_range=network_cidr_range) #4
return network.outputs() #5
def test_network_output_is_facade(network_outputs): #6
assert isinstance(network_outputs, NetworkFacade) #6
def test_network_output_has_network_name(network_outputs): #7
assert network_outputs._network == f"{network_name}-subnet" #7
def test_network_output_has_ip_cidr_range(network_outputs): #8
assert network_outputs._ip_cidr_range == network_cidr_range #8
Shows a pytest contract test comparing factory-module outputs with the dependent module’s expected inputs. section 6.3



