liveBook · Manning

Contract testing

As explained in Infrastructure as Code, Patterns and Practices: With examples in Python and Terraform

A contract test verifies that an integration between components conforms to an agreed interface or behavioral contract.

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

How Infrastructure as Code, Patterns and Practices: With examples in Python and Terraform covers it

How deep it goes
Defines static input-output comparison, demonstrates assertions, and places tests in a release workflow without detailing runtime behavior.
Examples
Uses a network facade, expected network name and CIDR range, pytest fixtures, and a database module consuming a network ID.
What it assumes
Assumes familiarity with infrastructure modules, dependencies, inputs, outputs, and unit and integration testing.
What you can do after
Diagnose dependency mismatches before deployment and preserve module integrations during changes or tool migrations.
How it connects
Connects unit testing, integration testing, infrastructure modules, and factory, builder, and prototype patterns.

Also explained in

Compare how 4 books explain contract testing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Infrastructure as Code, Patterns and Practices: With examples in Python and Terraform. The section links point to the parts of the book it draws on.