liveBook · Manning

Pytest

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

pytest is a Python testing framework that discovers and runs tests using naming conventions and assertion-based test functions.

pytest is a Python testing framework for discovering and running assertion-based tests. In infrastructure work, it verifies generated configuration without requiring configuration files or a live deployment. section 6.0 section 6.2.1

Use pytest to test infrastructure configuration through small, repeatable checks:

  • Name test files and test functions with the test_ prefix so pytest discovers them. section 6.2.1
  • Test factory functions that generate network and subnet configuration, including names, subnet counts, and IP ranges. section 6.2.1
  • Create fixtures for known configurations and reuse them across tests instead of repeating resource construction or parsing. section 6.2.1
  • Choose tests selectively: automated checks provide durable documentation, but redundant tests increase maintenance effort. section 6.0

Running network configuration tests

A file named test_network.py can collect three tests for a generated network object. Running `pytest test_network.py` reports the session, collects three items, and can finish with `3 passed`.

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

How deep it goes
Defines discovery, fixtures, assertions, and command-line execution, while deferring exhaustive infrastructure coverage and complete policy-tool support.
Examples
Uses network factories, subnet and IP-range assertions, reusable fixtures, and a pytest command reporting three passing tests.
What it assumes
Assumes Python functions and infrastructure configuration concepts, while teaching test discovery, fixtures, and configuration-focused assertions.
What you can do after
Apply it via test files, fixtures, and assertions to verify generated infrastructure objects and selected policy values.
How it connects
Connects unit testing, test fixtures, infrastructure as code, generated configuration, and policy as code.

Also explained in

Compare how 11 books explain pytest →

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.