Skip to main content

pytest-print adds the printer fixture you can use to print messages to the user (directly to the pytest runner, not stdout)

Project description

pytest-print

PyPI PyPI - Implementation PyPI - Python Version Downloads PyPI - License check

Allows to print extra content onto the PyTest reporting. This can be used for example to report sub-steps for long running tests, or to print debug information in your tests when you cannot debug the code (so that the end user does not need to wonder if the test froze/dead locked or not).

Install

pip install pytest-print

CLI flags

The following flags are registered for the pytest command:

  • --print by default the module activates print when pytest verbosity is greater than zero, this allows to bypass this and force print irrespective of the verbosity
  • --print-relative-time will print the relative time since the start of the test (display how long it takes to reach prints)

API

This library provides the following fixtures that help you print messages within a pytest run (bypasses the pytest output capture, so it will show up in the standard output, even if the test passes):

  • printer: Printter - function level fixture, when called prints a message line (with very simple formatting),

  • printer_session: Printter - session scoped fixture same as above but using (this exists as a backwards compatibility layer, as we didn't want to switch the originally function scope variant to session one),

  • pretty_printer: PrettyPrintter - session scoped fixture, when called prints a message line (with fancy formatting of space for indentation, icon for every message, and elapsed time format in form of [{elapsed:.20f}]) and also allows creating a printer that will be indented one level deeper (and optionally use a different icon).

  • create_pretty_printer: PrettyPrinterFactory - allows the caller to customize the fancy formatter as they wish. Takes one formatter argument, whose arguments should be interpreted as:

    ┌──────┐   ┌──────────┐┌─────────┐┌────────┐
    │ pre   ==   head   ││  icon   ││ space  │
    └──────┘   └──────────┘└─────────┘└────────┘
    
    ┌─────────────┐┌───────┐┌──────┐┌────────────┐
    │ indentation ││ timer ││ pre  ││ msg        │
    └─────────────┘└───────┘└──────┘└────────────┘
                   ┌───────┐┌────────────────────┐┌──────┐┌────────────┐
                    timer ││ spacer             ││ pre  ││ msg                       └───────┘└────────────────────┘└──────┘└────────────┘
                   ┌───────┐┌────────────────────┐┌────────────────────┐┌──────┐┌────────────┐
                    timer ││ spacer             ││ spacer             ││ pre  ││ msg                       └───────┘└────────────────────┘└────────────────────┘└──────┘└────────────┘
    

Example: printer_session

from __future__ import annotations

from typing import TYPE_CHECKING, Iterator

import pytest

if TYPE_CHECKING:
    from pytest_print import Printer


@pytest.fixture(scope="session")
def _expensive_setup(printer_session: Printer) -> Iterator[None]:
    printer_session("setup")
    yield
    printer_session("teardown")


@pytest.mark.usefixtures("_expensive_setup")
def test_run(printer_session: Printer) -> None:
    printer_session("running")
pytest magic.py -vvvv
...

magic.py::test_run
        setup expensive operation
        running test

magic.py::test_run PASSED
        teardown expensive operation

Example: pretty_printer

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

from pytest_print import Formatter

if TYPE_CHECKING:
    from pytest_print import PrettyPrinter, PrettyPrinterFactory


@pytest.fixture(scope="session")
def pretty(create_pretty_printer: PrettyPrinterFactory) -> PrettyPrinter:
    formatter = Formatter(indentation="  ", head=" ", space=" ", icon="⏩", timer_fmt="[{elapsed:.20f}]")
    return create_pretty_printer(formatter=formatter)


def test_long_running(pretty: PrettyPrinter) -> None:
    pretty("Starting test")

    pretty_printer_1 = pretty.indent(icon="🚀")
    pretty_printer_1("Drill down to 1st level details")

    pretty_printer_2 = pretty_printer_1.indent(icon="🚀")
    pretty_printer_2("Drill down to 2nd level details")

    pretty("Finished test")
magic.py::test_long_running
    Starting test
      🚀 Drill down to 1st level details
         🚀 Drill down to 2nd level details
    Finished test

magic.py::test_long_running PASSED

Example: create_pretty_printer

If you need nested messages you can use the printer_factory fixture or the pprinter.

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

from pytest_print import Formatter

if TYPE_CHECKING:
    from pytest_print import PrettyPrinter, PrettyPrinterFactory


@pytest.fixture(scope="session")
def pretty(create_pretty_printer: PrettyPrinterFactory) -> PrettyPrinter:
    formatter = Formatter(
      indentation=" I ",
      head=" H ",
      space=" S ",
      icon="🧹",
      timer_fmt="[{elapsed:.5f}]",
     )
    return create_pretty_printer(formatter=formatter)


def test_long_running(pretty: PrettyPrinter) -> None:
    pretty("Starting test")

    pretty_printer_1 = pretty.indent(icon="🚀")
    pretty_printer_1("Drill down to 1st level details")

    pretty_printer_2 = pretty_printer_1.indent(icon="🚀")
    pretty_printer_2("Drill down to 2nd level details")

    pretty("Finished test")
pytest magic.py --print --print-relative-time
...

magic.py
 I [0.00022] H 🧹 S Starting test
   [0.00029]        H 🚀 S Drill down to 1st level details
   [0.00034]               H 🚀 S Drill down to 2nd level details
 I [0.00038] H 🧹 S Finished test

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_print-1.2.0.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

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

pytest_print-1.2.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file pytest_print-1.2.0.tar.gz.

File metadata

  • Download URL: pytest_print-1.2.0.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_print-1.2.0.tar.gz
Algorithm Hash digest
SHA256 62514752d788206789d04dde2e69218d04138068377e5355d389fcc186a8e51e
MD5 75c12d01fd49532b24db375648b0e645
BLAKE2b-256 0c0d7167bc3a83d6862241b367bce92119c338929a46408b4ca9555c15cb5b40

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_print-1.2.0.tar.gz:

Publisher: release.yaml on pytest-dev/pytest-print

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_print-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_print-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_print-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 12969d579e72d549b71a68cef8a2d10ec47afdbaf92e32c8777d5d93d5a1cd42
MD5 5623480d246b56078fc5f7af954b5d6e
BLAKE2b-256 4c8002c87f95140e3621dde9a599c0b5684464f16acc0786ab8c8c5e929330c2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_print-1.2.0-py3-none-any.whl:

Publisher: release.yaml on pytest-dev/pytest-print

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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