pytest-testinfra
repository·main·Indexed 25 days ago
https://github.com/pytest-dev/pytest-testinfraA pytest plugin used to write Python tests to verify the actual state of infrastructure, such as files, packages, and services. It serves as a Python-based equivalent to Serverspec and supports multiple connection backends including SSH, Docker, Podman, Ansible, Salt, kubectl, OpenShift, and WinRM. The library can be used as a pytest plugin or independently via the Connection API using testinfra.get_host().
What's inside pytest-testinfra
- For users automating the testing of Ansible roles, Molecule provides an automated testing framework that includes native support for testinfra.
Check code style with ruff
mainAll code must pass linting checks using
ruff. You can install and run it using the following commands:pip install ruff ruff checkRun the test suite with tox
mainTo ensure all tests pass before submitting a pull request, you should set up a full test environment. This requires havingDockerandtoxinstalled on your system. You can run the complete test suite using thetoxcommand.toxParametrize Testinfra tests with pytest
mainTo avoid a single failure stopping the entire test suite, use
pytest.mark.parametrizeinstead of iterating over a list inside a single test function. This ensures each package or configuration is treated as a distinct test case, providing clear pass/fail status for each item in the parameter list.import pytest @pytest.mark.parametrize("name,version", [ ("nginx", "1.6"), ("python", "2.7"), ]) def test_packages(host, name, version): pkg = host.package(name) assert pkg.is_installed assert pkg.version.startswith(version)Get help with testinfra
mainIf you encounter issues or have questions regarding testinfra, you can seek assistance through the following channels:
- Issue Tracker: Search for existing problems or report new ones on the GitHub issue tracker.
- IRC: Join the
#pytestchannel on the libera.chat network to ask questions in real-time. - pytest Documentation: Since testinfra is implemented as a pytest plugin, it is highly recommended to consult the official pytest documentation to understand the underlying testing framework and maximize your usage of testinfra.
Install pytest-testinfra
mainYou can install the stable version of
pytest-testinfravia pip, or install the latest development version directly from GitHub.To install the stable version:
pip install pytest-testinfraTo install the development version:
pip install 'git+https://github.com/pytest-dev/pytest-testinfra@main#egg=pytest-testinfra'pip install pytest-testinfraRun Testinfra tests in parallel with pytest-xdist
mainTo speed up test execution for large test suites, you can use thepytest-xdistplugin to run tests across multiple processes using the-nflag.Quick start with pytest-testinfra
mainTestinfra allows you to write Python unit tests to verify the actual state of your servers. It works as a plugin for
pytest. In your test functions, you use thehostfixture to interact with the system under test.Common modules used via the
hostfixture include:host.file(path): To inspect files and their attributes (e.g.,contains,user,group,mode).host.package(name): To check package status (e.g.,is_installed,version).host.service(name): To check service status (e.g.,is_running,is_enabled).
To run your tests, use the
pytestcommand pointing to your test file.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): nginx = host.package("nginx") assert nginx.is_installed assert nginx.version.startswith("1.2") def test_nginx_running_and_enabled(host): nginx = host.service("nginx") assert nginx.is_running assert nginx.is_enabled # Run with: # $ pytest -v test_myinfra.pyAccess Testinfra modules via the host fixture
mainTestinfra modules are accessed through thehostfixture. To use them in your tests, declarehostas an argument in your test function. Thehostobject provides access to various infrastructure modules (e.g.,file,service,user,package) to inspect and verify the state of your system.Install Testinfra with specific connection backends
mainTestinfra uses Python
extrasto manage dependencies for different connection backends. To ensure you have the necessary dependencies for your target environments, install the specific backend you need. For example, to install support for Ansible and Salt, use:$ pip install pytest-testinfra[ansible,salt]Note that some backends may still require system-packaged tools to be installed on your machine.
Integrate Testinfra with Jenkins
mainIn a Jenkins environment where Vagrant is available, you can automate the testing lifecycle by installing dependencies, bringing up the Vagrant machine, and running
pytestwith the--junit-xmlflag to generate reports for Jenkins to consume.pip install pytest-testinfra paramiko vagrant up vagrant ssh-config > .vagrant/ssh-config pytest --hosts=default --ssh-config=.vagrant/ssh-config --junit-xml junit.xml tests.pyIntegrate Testinfra with Vagrant
mainTo run Testinfra tests against a running Vagrant machine, export the Vagrant SSH configuration to a file and pass it to
pytestusing the--ssh-configflag.vagrant ssh-config > .vagrant/ssh-config pytest --hosts=default --ssh-config=.vagrant/ssh-config tests.py