pytest-testinfra

10.2.2 · maintenance · verified Sun Apr 12

pytest-testinfra is a Python library and a pytest plugin that enables you to write unit tests in Python to verify the actual state of your servers or other infrastructure components. It acts as a Serverspec equivalent in Python, allowing you to test configurations managed by tools like Ansible, Puppet, or Chef. The current version is 10.2.2. The project recently announced it is not actively maintained, which may lead to slower responses to issues and pull requests.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic tests for a local system using the `host` fixture. Create a Python file (e.g., `test_myinfra.py`) with test functions that accept the `host` fixture. The `host` object provides modules to inspect files, packages, and services. You can run these tests against a local machine, SSH connection, Docker container, and more by specifying the `--connection` and `--host` parameters with `pytest`.

import pytest

def test_passwd_file(host):
    passwd = host.file("/etc/passwd")
    assert passwd.contains("root")
    assert passwd.user == "root"
    assert passwd.group == "root"
    assert passwd.mode == 0o644

def test_nginx_is_installed(host):
    # Requires nginx to be present on the target host
    nginx = host.package("nginx")
    assert nginx.is_installed
    assert nginx.version.startswith("1.")

def test_nginx_running_and_enabled(host):
    # Requires nginx service to be present on the target host
    nginx = host.service("nginx")
    assert nginx.is_running
    assert nginx.is_enabled

# To run this, save as e.g., test_myinfra.py and execute:
# pytest test_myinfra.py --connection=local
# For remote host:
# pytest test_myinfra.py --host=ssh://user@your-remote-host --sudo --connection=ssh

view raw JSON →