{"id":5035,"library":"pytest-testinfra","title":"pytest-testinfra","description":"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.","status":"maintenance","version":"10.2.2","language":"en","source_language":"en","source_url":"https://github.com/pytest-dev/pytest-testinfra","tags":["testing","pytest","infrastructure","devops","server-testing","system-administration","ansible","docker"],"install":[{"cmd":"pip install pytest-testinfra","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core testing framework that pytest-testinfra extends.","package":"pytest","optional":false},{"reason":"Optional backend for testing remote SSH hosts.","package":"paramiko","optional":true},{"reason":"Optional backend for testing hosts managed by Ansible, using Ansible inventories and connections.","package":"ansible","optional":true},{"reason":"Optional backend for testing Docker containers.","package":"docker","optional":true},{"reason":"Optional backend for testing Windows hosts via WinRM.","package":"pywinrm","optional":true},{"reason":"Optional backend for testing containers in Kubernetes.","package":"kubectl","optional":true},{"reason":"Optional backend for testing Podman containers.","package":"podman","optional":true},{"reason":"Optional backend for testing LXC/LXD containers.","package":"lxd","optional":true}],"imports":[{"note":"pytest-testinfra works as a plugin to pytest. Its primary interface is the `host` fixture, which is automatically made available to test functions.","symbol":"pytest","correct":"import pytest\n# The 'host' fixture is provided automatically by pytest-testinfra"}],"quickstart":{"code":"import pytest\n\ndef test_passwd_file(host):\n    passwd = host.file(\"/etc/passwd\")\n    assert passwd.contains(\"root\")\n    assert passwd.user == \"root\"\n    assert passwd.group == \"root\"\n    assert passwd.mode == 0o644\n\ndef test_nginx_is_installed(host):\n    # Requires nginx to be present on the target host\n    nginx = host.package(\"nginx\")\n    assert nginx.is_installed\n    assert nginx.version.startswith(\"1.\")\n\ndef test_nginx_running_and_enabled(host):\n    # Requires nginx service to be present on the target host\n    nginx = host.service(\"nginx\")\n    assert nginx.is_running\n    assert nginx.is_enabled\n\n# To run this, save as e.g., test_myinfra.py and execute:\n# pytest test_myinfra.py --connection=local\n# For remote host:\n# pytest test_myinfra.py --host=ssh://user@your-remote-host --sudo --connection=ssh","lang":"python","description":"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`."},"warnings":[{"fix":"Upgrade your Python environment to 3.9 or higher.","message":"Python 3.9 or newer is now required. Previous versions of pytest-testinfra supported older Python releases (e.g., Python 3.6 support was dropped in v7.0.0, and Python 3.9 became mandatory in v8.1.0).","severity":"breaking","affected_versions":"<8.1.0"},{"fix":"Update your `pip install` commands and `requirements.txt` to use `pytest-testinfra`. If you were directly importing `testinfra` modules (e.g., `from testinfra.host import Host`), ensure your code is using the `host` fixture pattern.","message":"The package was renamed from 'testinfra' to 'pytest-testinfra' in version 6.0.0. Users with 'testinfra' in their `requirements.txt` or import statements needed to update. A dummy 'testinfra' package was available for transition but should not be relied upon.","severity":"breaking","affected_versions":"<6.0.0"},{"fix":"Be aware of the maintenance status; consider contributing via pull requests if you encounter issues or need new features, or explore community forks.","message":"The project is currently not actively maintained. Responses to issues or pull requests may be delayed for several months. This means new features, bug fixes, or compatibility updates might be slow or require community contributions.","severity":"gotcha","affected_versions":"10.2.2+"},{"fix":"Favor `host.command()` for executing shell commands and inspecting their output, or use `host.package()`, `host.service()`, `host.file()` for high-level infrastructure checks. If complex Python logic is needed, consider writing a remote Python script and executing it via `host.run()`, or using Ansible's 'script' module if using the Ansible backend.","message":"When using `pytest-testinfra` for remote execution, direct calls to certain Python libraries (e.g., `requests`) via the `host` fixture might not work as expected. The remote environment often provides a more basic wrapper, not a full Python interpreter capable of running arbitrary modules directly.","severity":"gotcha","affected_versions":"All"},{"fix":"Pass necessary inventory data as pytest parameters if you need cross-host variables, or structure your tests to operate on the variables relevant to the current `host` fixture instance. For complex Ansible integration, ensure you understand the limitations of `testinfra`'s Ansible backend.","message":"Accessing Ansible inventory variables for *other* hosts (e.g., `hostvars[other_host]`) or global `group_vars` directly through the `host` fixture can be complicated or not directly supported. `host.ansible.get_variables()` primarily provides variables for the currently tested host.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}