pytest-bdd-report 1.2.2
A pytest-bdd plugin for generating useful and informative BDD test reports
Requires Python: >=3.5

=================
pytest-bdd-report
=================

|python| |pypi| |formatting|

|tests| |bdd_tests| |ui_tests|

|total|

.. |total| image:: https://static.pepy.tech/badge/pytest-bdd-report
    :target: https://pepy.tech/project/pytest-bdd-report
    :alt: PePy total downloads

.. |pypi| image:: https://img.shields.io/pypi/v/pytest-bdd-report.svg
    :target: https://pypi.org/project/pytest-bdd-report
    :alt: PyPI version

.. |python| image:: https://img.shields.io/pypi/pyversions/pytest-bdd-report.svg
    :target: https://pypi.org/project/pytest-bdd-report
    :alt: Python versions

.. |formatting| image:: https://img.shields.io/github/actions/workflow/status/mattiamonti/pytest-bdd-report/black.yml?logo=GitHub%20actions&label=Formatting
    :alt: GitHub Workflow Status (with event)

.. |tests| image:: https://img.shields.io/github/actions/workflow/status/mattiamonti/pytest-bdd-report/unit%20tests.yml?logo=pytest&label=Unit%20Tests
    :alt: GitHub Workflow Status (with event)

.. |bdd_tests| image:: https://img.shields.io/github/actions/workflow/status/mattiamonti/pytest-bdd-report/ui%20BDD%20tests.yml?logo=pytest&label=BDD%20UI%20Tests
    :alt: GitHub Workflow Status (with event)

.. |ui_tests| image:: https://img.shields.io/github/actions/workflow/status/mattiamonti/pytest-bdd-report/ui%20automated%20tests.yml?logo=robotframework&label=UI%20Tests
    :alt: GitHub Workflow Status (with event)

.. |dstats| image:: https://img.shields.io/pypi/dd/pytest-bdd-report
   :alt: PyPI - Downloads



The `pytest-bdd-report` plugin is a useful extension for the `pytest-bdd`_ library that allows you to generate
useful and informative reports for BDD (Behavior-Driven Development) tests developed using the pytest-bdd framework.
This plugin facilitates the generation of clear and effective HTML reports, providing a comprehensible view of BDD test executions within the project.

For more, check out the `Documentation`_

----

This `pytest`_ plugin was generated with `Cookiecutter`_ along with `@hackebrot`_'s `cookiecutter-pytest-plugin`_ template.


Features
--------

* **Detailed BDD Reports**: The pytest-bdd-report plugin enables the generation of detailed reports for BDD tests executed using pytest-bdd. These reports clearly show the executed steps, tested scenarios, and obtained results.
* **HTML Format**: The generated reports are presented in an intuitive and interactive HTML format. This allows developers, testers, and other team members to easily view the status of BDD tests.
* **Easy Installation**: Installing the plugin is simple and fast. You can install it using the command `pip install pytest-bdd-report`.


Requirements
------------

* Jinja2
* pytest
* pytest-bdd


Installation
------------

1. Ensure that you have `pytest`_ and `pytest-bdd`_ installed in your development environment.
2. Open a terminal window.
3. Execute the following command to install the `pytest-bdd-report` plugin via `pip`_ from `PyPI`_

::

    $ pip install pytest-bdd-report


Usage
-----

Once installed, you can generate BDD reports in an HTML file using the following command:

::

    $ pytest --bdd-report="report.html"

Attach a screenshot to the HTML report generated by pytest-bdd-report, by using the provided attach.screenshot() method.

This helper function lets you attach screenshots either from raw bytes (e.g. page.screenshot() in Playwright) or from a file path on disk.
The screenshot will automatically appear inside the corresponding Feature → Scenario section of the final report.

Example with Playwright
=======================

.. code-block:: python

    from pytest_bdd_report import attach

    @pytest.hookimpl(hookwrapper=True)
    def pytest_bdd_step_error(request, feature, scenario, step, step_func, step_func_args, exception):
        yield
        for fixture in step_func_args.values():
            page = getattr(fixture, "page", None)
            if page:
                # Attach screenshot from bytes
                screenshot_bytes = page.screenshot()

                attach.screenshot(screenshot_bytes, feature.name, scenario.name) # pytest-bdd-report code to attach the screenshot

                # Attach screenshot from image path
                screenshot_path = "screenshot_path/test.png"
                page.screenshot(path=screenshot_path)

                attach.screenshot(screenshot_path, feature.name, scenario.name) # pytest-bdd-report code to attach the screenshot

Parameters
==========

.. list-table::
   :header-rows: 1
   :widths: 20 20 60

   * - Parameter
     - Type
     - Description
   * - ``image``
     - ``bytes | str | Path``
     - Screenshot content. Can be raw bytes or a file path to the image to attach.
   * - ``feature_name``
     - ``str``
     - The name of the feature to which the screenshot belongs.
   * - ``scenario_name``
     - ``str``
     - The name of the scenario to which the screenshot belongs.



Run tests
---------

To run tests:

1. Create a virtual environment
2. Install the required packages:

::

    $ pip install -r requirements.txt
    $ playwright install


3. Install the plugin locally:

::

    $ pip install -e .


4. Run the unit tests:

::

    $ python -m pytest --ignore=tests/bdd/

5. Run the BDD UI tests with pytest-bdd:

::

    $ python -m pytest tests/bdd

6. Want a report for the Playwright BDD tests? use the --bdd-report flag!

::

    $ python -m pytest tests/bdd --bdd-report="report.html"


Contributing
------------
Contributions are very welcome. Tests can be run with `pytest`_ as shown.

License
-------

Distributed under the terms of the `MIT`_ license, "pytest-bdd-report" is free and open source software


Issues
------

If you encounter any problems, please `file an issue`_ along with a detailed description.

.. _`Cookiecutter`: https://github.com/audreyr/cookiecutter
.. _`@hackebrot`: https://github.com/hackebrot
.. _`MIT`: http://opensource.org/licenses/MIT
.. _`BSD-3`: http://opensource.org/licenses/BSD-3-Clause
.. _`GNU GPL v3.0`: http://www.gnu.org/licenses/gpl-3.0.txt
.. _`Apache Software License 2.0`: http://www.apache.org/licenses/LICENSE-2.0
.. _`cookiecutter-pytest-plugin`: https://github.com/pytest-dev/cookiecutter-pytest-plugin
.. _`file an issue`: https://github.com/mattiamonti/pytest-bdd-report/issues
.. _`pytest`: https://github.com/pytest-dev/pytest
.. _`tox`: https://tox.readthedocs.io/en/latest/
.. _`pip`: https://pypi.org/project/pip/
.. _`PyPI`: https://pypi.org/project
.. _`pytest-bdd`: https://github.com/pytest-dev/pytest-bdd
.. _`Documentation`: https://mattia-monti.gitbook.io/pytest-bdd-report/