Skip to main content

Pytest plugin that reports testinfra results to MariaDB or PostgreSQL backends and visualizes on Grafana.

Project description

pytest-testinfra-exporter

Backend-pluggable pytest reporting plugin with preserved pytest-testinfra host parsing behavior.

Installation

From the pytest-testinfra-exporter directory, install the plugin in editable mode:

pip install -e .

If you are standing one level above this directory, use:

pip install -e ./pytest-testinfra-exporter

Pytest will auto-discover the plugin through the package's pytest11 entry point, so no conftest.py changes are required.

Install a database driver if you plan to use a backend:

pip install -e ".[mariadb]"

or:

pip install -e ".[postgres]"

Usage

Once installed, enable reporting directly from the pytest command line:

pytest --storage-report --report-backend=mariadb

The plugin is loaded automatically during pytest startup.

Overview

This project now follows a Strategy + Adapter architecture:

  • Core pytest hooks are implemented in plugin.py.
  • Storage is delegated through AbstractStorageBackend in backend.py.
  • MariaDB support is implemented as MariaDBBackend in backends/mariadb.py.
  • PostgreSQL support is implemented as PostgresBackend in backends/postgres.py.
  • Normalized payloads are represented by dataclasses in models.py.

The testinfra-specific logic is intentionally unchanged (including parsing of salt://, ssh://, fixture host resolution, and nodeid parsing).


Package structure

  • models.py — dataclasses used by the core reporter and backends.
  • backend.py — backend interface contract.
  • backends/mariadb.py — MariaDB adapter implementation.
  • backends/postgres.py — PostgreSQL adapter implementation.
  • plugin.py — pytest hooks, helper utilities, and failure tagging.
  • schema/db.sql — full MariaDB schema reset/apply script.
  • failure_mapper/failure_map.yaml — failure tag mapping rules.
  • docs/index.rst — Sphinx documentation entry point.
  • docs/usage.rst — Sphinx usage guide.
  • docs/api.rst — Sphinx API reference.

Runtime architecture

pytest lifecycle hooks (plugin.py)
  -> build backend strategy (--report-backend)
  -> accumulate TestResultRecord objects in memory
  -> backend.save_results(run_id, results)
  -> backend.session_finish(run_id, counters)

Hook flow

  1. pytest_sessionstart
    • creates run metadata (TestRunSummary)
    • initializes selected backend
    • persists session start
  2. pytest_runtest_makereport
    • captures metadata per nodeid (test name, suite, class, host, markers)
  3. pytest_runtest_logreport
    • merges phase reports (setup/call/teardown)
    • computes final status and captured artifacts
    • appends TestResultRecord
  4. pytest_sessionfinish
    • computes final counters
    • delegates persistence to backend
  5. pytest_terminal_summary
    • prints reporter and tagger status

CLI options

Option Default Description
--storage-report False Enable reporting pipeline.
--report-backend mariadb Storage backend strategy selector (mariadb, postgres).
--datastore-config datastores/default.yaml Path to a YAML file with datastore connection settings. The datastore.<report-backend> section is used. Explicit CLI options override its values.
--run-name Start datetime Human-readable run name stored with the run. Defaults to YYYY-MM-DD HH:MM:SS.
--mariadb-host localhost MariaDB host.
--mariadb-port 3306 MariaDB port.
--mariadb-user testinfra_user MariaDB username.
--mariadb-password password MariaDB password.
--mariadb-database testinfra_reports MariaDB database name.
--mariadb-suite-version None Suite version string (for example git SHA).
--mariadb-init-schema False Run idempotent schema creation/migrations.
--failure-map failure_mapper/failure_map.yaml Failure tagging rules file.
--postgres-host localhost PostgreSQL host.
--postgres-port 5432 PostgreSQL port.
--postgres-user postgres PostgreSQL username.
--postgres-password password PostgreSQL password.
--postgres-database testinfra_reports PostgreSQL database name.
--postgres-init-schema False Run idempotent PostgreSQL schema creation.

Example usage

MariaDB

pytest tests/ \
  --storage-report \
  --report-backend mariadb \
   --run-name "manual-run" \
  --mariadb-host 127.0.0.1 \
  --mariadb-port 3306 \
  --mariadb-user testinfra_user \
  --mariadb-password password \
  --mariadb-database testinfra_reports \
  --mariadb-suite-version "$(git rev-parse --short HEAD)" \
  --mariadb-init-schema

PostgreSQL

pytest tests/ \
  --storage-report \
  --report-backend postgres \
  --postgres-host 127.0.0.1 \
  --postgres-port 5432 \
  --postgres-user postgres \
  --postgres-password password \
  --postgres-database testinfra_reports \
  --postgres-init-schema

Datastore config file

Instead of passing connection flags on every invocation, define them once in a YAML file and select the backend with --report-backend. When --datastore-config is not supplied, the bundled datastores/default.yaml is used:

datastore:
  mariadb:
    host: 'localhost'
    port: 3306
    user: 'testinfra_user'
    password: 'password'
    database: 'testinfra_reports'
  postgres:
    host: 'localhost'
    port: 5432
    user: 'postgres'
    password: 'password'
    database: 'testinfra_reports'
pytest tests/ \
  --storage-report \
  --report-backend mariadb \
  --datastore-config datastore.yaml

Any explicit CLI option (for example --mariadb-user, --mariadb-port) overrides the corresponding value from the YAML file.


Dependencies

  • MariaDB backend: PyMySQL
  • PostgreSQL backend: psycopg2-binary
  • Failure tagging: PyYAML
pip install PyMySQL psycopg2-binary pyyaml

Failure tagging

FailureTagger loads YAML rules from failure_mapper/failure_map.yaml and applies first-match classification for fail/error results.

Supported matching:

  • match_type: exact (substring)
  • match_type: regex (re.search)

Sphinx documentation

A Sphinx-ready docs tree is now included:

  • docs/index.rst
  • docs/usage.rst
  • docs/api.rst

These files use autodoc directives for:

  • models
  • backend
  • plugin
  • backends.mariadb
  • backends.postgres

Example minimal docs/conf.py:

extensions = [
    "sphinx.ext.autodoc",
    "sphinx.ext.napoleon",
    "sphinx.ext.viewcode",
]

Build docs from project root:

sphinx-build -b html docs docs/_build/html

Notes

  • Timestamp persistence remains naive IST for compatibility with existing dashboards.
  • Existing testinfra host extraction behavior is preserved.
  • Current backend implementations: mariadb, postgres.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_testinfra_exporter-0.3.0.tar.gz (23.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_testinfra_exporter-0.3.0-py3-none-any.whl (29.4 kB view details)

Uploaded Python 3

File details

Details for the file pytest_testinfra_exporter-0.3.0.tar.gz.

File metadata

File hashes

Hashes for pytest_testinfra_exporter-0.3.0.tar.gz
Algorithm Hash digest
SHA256 9bc5204e83f1c42632e88bdfa76b070fefdf30403fcefa295d2777857f162dd2
MD5 89af9c46a700aa7127ee1d5a9ee0e8fc
BLAKE2b-256 b7f89a6df76f2b7feb24d0f103c3c073835e1c95179733e315a509222e6ecf5f

See more details on using hashes here.

File details

Details for the file pytest_testinfra_exporter-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_testinfra_exporter-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 250d853724a4af43c62a2bb80896e99f6188aa76c64ee6ede30a734513239761
MD5 42addb1728c75ebefd37dfe87e039c57
BLAKE2b-256 db4f81c576b0b2f86732c8b524feed1f0ecdb6e98750d84d050ce17079b9db04

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page