pytest-print


Namepytest-print JSON
Version 1.1.0 PyPI version JSON
download
home_pageNone
Summarypytest-print adds the printer fixture you can use to print messages to the user (directly to the pytest runner, not stdout)
upload_time2025-02-25 01:49:22
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licensePermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords env pytest
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pytest-print

[![PyPI](https://img.shields.io/pypi/v/pytest-print?style=flat-square)](https://pypi.org/project/pytest-print)
[![PyPI - Implementation](https://img.shields.io/pypi/implementation/pytest-print?style=flat-square)](https://pypi.org/project/pytest-print)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pytest-print?style=flat-square)](https://pypi.org/project/pytest-print)
[![Downloads](https://static.pepy.tech/badge/pytest-print/month)](https://pepy.tech/project/pytest-print)
[![PyPI - License](https://img.shields.io/pypi/l/pytest-print?style=flat-square)](https://opensource.org/licenses/MIT)
[![check](https://github.com/pytest-dev/pytest-print/actions/workflows/check.yaml/badge.svg)](https://github.com/pytest-dev/pytest-print/actions/workflows/check.yaml)

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).

<!--ts-->

- [Install](#install)
- [CLI flags](#cli-flags)
- [API](#api)
  - [Example: printer_session](#example-printer_session)
  - [Example: pretty_printer](#example-pretty_printer)
  - [Example: create_pretty_printer](#example-create_pretty_printer)

<!--te-->

## Install

```sh
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`](#example-printer_session) - 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`](#example-pretty_printer) - 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`](#example-create_pretty_printer) - allows the caller to customize the
  fancy formatter as they wish. Takes one `formatter` argument, whose arguments should be interpreted as:

  ```shell
  ┌──────┐   ┌──────────┐┌─────────┐┌────────┐
  │ pre  │ ==│   head   ││  icon   ││ space  │
  └──────┘   └──────────┘└─────────┘└────────┘

  ┌─────────────┐┌───────┐┌──────┐┌────────────┐
  │ indentation ││ timer ││ pre  ││ msg        │
  └─────────────┘└───────┘└──────┘└────────────┘
                 ┌───────┐┌────────────────────┐┌──────┐┌────────────┐
                 │ timer ││ spacer             ││ pre  ││ msg        │
                 └───────┘└────────────────────┘└──────┘└────────────┘
                 ┌───────┐┌────────────────────┐┌────────────────────┐┌──────┐┌────────────┐
                 │ timer ││ spacer             ││ spacer             ││ pre  ││ msg        │
                 └───────┘└────────────────────┘└────────────────────┘└──────┘└────────────┘
  ```

### Example: `printer_session`

```python
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")
```

```shell
pytest magic.py -vvvv
...

magic.py::test_run
        setup expensive operation
        running test

magic.py::test_run PASSED
        teardown expensive operation
```

### Example: `pretty_printer`

```python
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")
```

```shell
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`.

```python
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")
```

```bash
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
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pytest-print",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "Bern\u00e1t G\u00e1bor <gaborjbernat@gmail.com>",
    "keywords": "env, pytest",
    "author": null,
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/79/eb/18c4bdcb4e0ee39ee15d9456469f3da73db033ce37e8e68501426c870bbc/pytest_print-1.1.0.tar.gz",
    "platform": null,
    "description": "# pytest-print\n\n[![PyPI](https://img.shields.io/pypi/v/pytest-print?style=flat-square)](https://pypi.org/project/pytest-print)\n[![PyPI - Implementation](https://img.shields.io/pypi/implementation/pytest-print?style=flat-square)](https://pypi.org/project/pytest-print)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pytest-print?style=flat-square)](https://pypi.org/project/pytest-print)\n[![Downloads](https://static.pepy.tech/badge/pytest-print/month)](https://pepy.tech/project/pytest-print)\n[![PyPI - License](https://img.shields.io/pypi/l/pytest-print?style=flat-square)](https://opensource.org/licenses/MIT)\n[![check](https://github.com/pytest-dev/pytest-print/actions/workflows/check.yaml/badge.svg)](https://github.com/pytest-dev/pytest-print/actions/workflows/check.yaml)\n\nAllows to print extra content onto the PyTest reporting. This can be used for example to report sub-steps for long\nrunning tests, or to print debug information in your tests when you cannot debug the code (so that the end user does not\nneed to wonder if the test froze/dead locked or not).\n\n<!--ts-->\n\n- [Install](#install)\n- [CLI flags](#cli-flags)\n- [API](#api)\n  - [Example: printer_session](#example-printer_session)\n  - [Example: pretty_printer](#example-pretty_printer)\n  - [Example: create_pretty_printer](#example-create_pretty_printer)\n\n<!--te-->\n\n## Install\n\n```sh\npip install pytest-print\n```\n\n## CLI flags\n\nThe following flags are registered for the pytest command:\n\n- `--print` by default the module activates print when pytest verbosity is greater than zero, this allows to bypass this\n  and force print irrespective of the verbosity\n- `--print-relative-time` will print the relative time since the start of the test (display how long it takes to reach\n  prints)\n\n## API\n\nThis library provides the following fixtures that help you print messages within a pytest run (bypasses the pytest\noutput capture, so it will show up in the standard output, even if the test passes):\n\n- `printer: Printter` - function level fixture, when called prints a message line (with very simple formatting),\n- [`printer_session: Printter`](#example-printer_session) - session scoped fixture same as above but using (this exists\n  as a backwards compatibility layer, as we didn't want to switch the originally function scope variant to session one),\n- [`pretty_printer: PrettyPrintter`](#example-pretty_printer) - session scoped fixture, when called prints a message\n  line (with fancy formatting of space for indentation, `\u23e9` icon for every message, and elapsed time format in form of\n  `[{elapsed:.20f}]`) and also allows creating a printer that will be indented one level deeper (and optionally use a\n  different icon).\n- [`create_pretty_printer: PrettyPrinterFactory`](#example-create_pretty_printer) - allows the caller to customize the\n  fancy formatter as they wish. Takes one `formatter` argument, whose arguments should be interpreted as:\n\n  ```shell\n  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2510   \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n  \u2502 pre  \u2502 ==\u2502   head   \u2502\u2502  icon   \u2502\u2502 space  \u2502\n  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2518   \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n\n  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n  \u2502 indentation \u2502\u2502 timer \u2502\u2502 pre  \u2502\u2502 msg        \u2502\n  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2518\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n                 \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n                 \u2502 timer \u2502\u2502 spacer             \u2502\u2502 pre  \u2502\u2502 msg        \u2502\n                 \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2518\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n                 \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n                 \u2502 timer \u2502\u2502 spacer             \u2502\u2502 spacer             \u2502\u2502 pre  \u2502\u2502 msg        \u2502\n                 \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2518\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n  ```\n\n### Example: `printer_session`\n\n```python\nfrom __future__ import annotations\n\nfrom typing import TYPE_CHECKING, Iterator\n\nimport pytest\n\nif TYPE_CHECKING:\n    from pytest_print import Printer\n\n\n@pytest.fixture(scope=\"session\")\ndef _expensive_setup(printer_session: Printer) -> Iterator[None]:\n    printer_session(\"setup\")\n    yield\n    printer_session(\"teardown\")\n\n\n@pytest.mark.usefixtures(\"_expensive_setup\")\ndef test_run(printer_session: Printer) -> None:\n    printer_session(\"running\")\n```\n\n```shell\npytest magic.py -vvvv\n...\n\nmagic.py::test_run\n        setup expensive operation\n        running test\n\nmagic.py::test_run PASSED\n        teardown expensive operation\n```\n\n### Example: `pretty_printer`\n\n```python\nfrom __future__ import annotations\n\nfrom typing import TYPE_CHECKING\n\nimport pytest\n\nfrom pytest_print import Formatter\n\nif TYPE_CHECKING:\n    from pytest_print import PrettyPrinter, PrettyPrinterFactory\n\n\n@pytest.fixture(scope=\"session\")\ndef pretty(create_pretty_printer: PrettyPrinterFactory) -> PrettyPrinter:\n    formatter = Formatter(indentation=\"  \", head=\" \", space=\" \", icon=\"\u23e9\", timer_fmt=\"[{elapsed:.20f}]\")\n    return create_pretty_printer(formatter=formatter)\n\n\ndef test_long_running(pretty: PrettyPrinter) -> None:\n    pretty(\"Starting test\")\n\n    pretty_printer_1 = pretty.indent(icon=\"\ud83d\ude80\")\n    pretty_printer_1(\"Drill down to 1st level details\")\n\n    pretty_printer_2 = pretty_printer_1.indent(icon=\"\ud83d\ude80\")\n    pretty_printer_2(\"Drill down to 2nd level details\")\n\n    pretty(\"Finished test\")\n```\n\n```shell\nmagic.py::test_long_running\n   \u23e9 Starting test\n      \ud83d\ude80 Drill down to 1st level details\n         \ud83d\ude80 Drill down to 2nd level details\n   \u23e9 Finished test\n\nmagic.py::test_long_running PASSED\n```\n\n### Example: `create_pretty_printer`\n\nIf you need nested messages you can use the `printer_factory` fixture or the `pprinter`.\n\n```python\nfrom __future__ import annotations\n\nfrom typing import TYPE_CHECKING\n\nimport pytest\n\nfrom pytest_print import Formatter\n\nif TYPE_CHECKING:\n    from pytest_print import PrettyPrinter, PrettyPrinterFactory\n\n\n@pytest.fixture(scope=\"session\")\ndef pretty(create_pretty_printer: PrettyPrinterFactory) -> PrettyPrinter:\n    formatter = Formatter(\n      indentation=\" I \",\n      head=\" H \",\n      space=\" S \",\n      icon=\"\ud83e\uddf9\",\n      timer_fmt=\"[{elapsed:.5f}]\",\n     )\n    return create_pretty_printer(formatter=formatter)\n\n\ndef test_long_running(pretty: PrettyPrinter) -> None:\n    pretty(\"Starting test\")\n\n    pretty_printer_1 = pretty.indent(icon=\"\ud83d\ude80\")\n    pretty_printer_1(\"Drill down to 1st level details\")\n\n    pretty_printer_2 = pretty_printer_1.indent(icon=\"\ud83d\ude80\")\n    pretty_printer_2(\"Drill down to 2nd level details\")\n\n    pretty(\"Finished test\")\n```\n\n```bash\npytest magic.py --print --print-relative-time\n...\n\nmagic.py\n I [0.00022] H \ud83e\uddf9 S Starting test\n   [0.00029]        H \ud83d\ude80 S Drill down to 1st level details\n   [0.00034]               H \ud83d\ude80 S Drill down to 2nd level details\n I [0.00038] H \ud83e\uddf9 S Finished test\n```\n",
    "bugtrack_url": null,
    "license": "Permission is hereby granted, free of charge, to any person obtaining a\n        copy of this software and associated documentation files (the\n        \"Software\"), to deal in the Software without restriction, including\n        without limitation the rights to use, copy, modify, merge, publish,\n        distribute, sublicense, and/or sell copies of the Software, and to\n        permit persons to whom the Software is furnished to do so, subject to\n        the following conditions:\n        \n        The above copyright notice and this permission notice shall be included\n        in all copies or substantial portions of the Software.\n        \n        THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS\n        OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n        MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\n        IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\n        CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\n        TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\n        SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "pytest-print adds the printer fixture you can use to print messages to the user (directly to the pytest runner, not stdout)",
    "version": "1.1.0",
    "project_urls": {
        "Homepage": "https://github.com/pytest-dev/pytest-print",
        "Source": "https://github.com/pytest-dev/pytest-print",
        "Tracker": "https://github.com/pytest-dev/pytest-print/issues"
    },
    "split_keywords": [
        "env",
        " pytest"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "333eaf3a9fa14a014241dc8c0637fdddc02440a7ffa678247c374dd96416a447",
                "md5": "5a4e30628c16540100e61b943d32ae7c",
                "sha256": "ec82a8bbe5e1d4f6b3a287c2c119de7e5cdf57a31981e9dba2a4809c5829bb5e"
            },
            "downloads": -1,
            "filename": "pytest_print-1.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5a4e30628c16540100e61b943d32ae7c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 7167,
            "upload_time": "2025-02-25T01:49:20",
            "upload_time_iso_8601": "2025-02-25T01:49:20.425443Z",
            "url": "https://files.pythonhosted.org/packages/33/3e/af3a9fa14a014241dc8c0637fdddc02440a7ffa678247c374dd96416a447/pytest_print-1.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "79eb18c4bdcb4e0ee39ee15d9456469f3da73db033ce37e8e68501426c870bbc",
                "md5": "324bd4ec0e093e787c9471616bed132f",
                "sha256": "15cf4236c735a8f1865403f7a9fbbb95f6b6b98f3759046f9f30a24c8a32018d"
            },
            "downloads": -1,
            "filename": "pytest_print-1.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "324bd4ec0e093e787c9471616bed132f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 12581,
            "upload_time": "2025-02-25T01:49:22",
            "upload_time_iso_8601": "2025-02-25T01:49:22.579809Z",
            "url": "https://files.pythonhosted.org/packages/79/eb/18c4bdcb4e0ee39ee15d9456469f3da73db033ce37e8e68501426c870bbc/pytest_print-1.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-25 01:49:22",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "pytest-dev",
    "github_project": "pytest-print",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "pytest-print"
}
        
Elapsed time: 1.30034s