Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cucumber junit report #729

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,14 @@ To have an output in json format:

This will output an expanded (meaning scenario outlines will be expanded to several scenarios) Cucumber format.

A similar report is the junit format. It follows this schema from `Jenkins Junit Schema <https://github.com/cucumber/junit-xml-formatter/blob/main/jenkins-junit.xsd>`_

To have an output in junit format:

::

pytest --cucumberjunit=<path to junit report>

To enable gherkin-formatted output on terminal, use `--gherkin-terminal-reporter` in conjunction with the `-v` or `-vv` options:

::
Expand Down
6 changes: 5 additions & 1 deletion src/pytest_bdd/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import pytest
from typing_extensions import ParamSpec

from . import cucumber_json, generation, gherkin_terminal_reporter, given, reporting, then, when
from . import generation, given, then, when
from .reports import cucumber_json, cucumber_junit, gherkin_terminal_reporter, reporting
from .utils import CONFIG_STACK

if TYPE_CHECKING:
Expand Down Expand Up @@ -59,6 +60,7 @@ def pytest_addoption(parser: Parser) -> None:
"""Add pytest-bdd options."""
add_bdd_ini(parser)
cucumber_json.add_options(parser)
cucumber_junit.add_options(parser)
generation.add_options(parser)
gherkin_terminal_reporter.add_options(parser)

Expand All @@ -72,6 +74,7 @@ def pytest_configure(config: Config) -> None:
"""Configure all subplugins."""
CONFIG_STACK.append(config)
cucumber_json.configure(config)
cucumber_junit.configure(config)
gherkin_terminal_reporter.configure(config)


Expand All @@ -80,6 +83,7 @@ def pytest_unconfigure(config: Config) -> None:
if CONFIG_STACK:
CONFIG_STACK.pop()
cucumber_json.unconfigure(config)
cucumber_junit.unconfigure(config)


@pytest.hookimpl(hookwrapper=True)
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ def _get_result(self, step: dict[str, Any], report: TestReport, error_message: b
:return: `dict` in form {"status": "<passed|failed|skipped>", ["error_message": "<error_message>"]}
"""
result: dict[str, Any] = {}
if report.passed or not step["failed"]: # ignore setup/teardown
if report.skipped:
reason = report.longrepr[2][report.longrepr[2].find(":") + 2 :]
result = {"status": "skipped", "skipped_message": reason}
elif report.passed or not step["failed"]: # ignore setup/teardown
result = {"status": "passed"}
elif report.failed:
result = {"status": "failed", "error_message": str(report.longrepr) if error_message else ""}
elif report.skipped:
result = {"status": "skipped"}
result["duration"] = int(math.floor((10**9) * step["duration"])) # nanosec
return result

Expand Down
131 changes: 131 additions & 0 deletions src/pytest_bdd/reports/cucumber_junit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
"""Cucumber junit output formatter."""

from __future__ import annotations

import typing
import xml.dom.minidom

from .cucumber_json import LogBDDCucumberJSON

if typing.TYPE_CHECKING:
from _pytest.config import Config
from _pytest.config.argparsing import Parser
from _pytest.terminal import TerminalReporter

SYSTEM_OUT_DEFAULT_MESSAGE_LENGTH = 61
SYSTEM_OUT_MINIMUM_DOTS = 5
SYSTEM_OUT_INITIAL_INTEND = " "


def add_options(parser: Parser) -> None:
"""Add pytest-bdd options."""
group = parser.getgroup("bdd", "Cucumber JSON")
group.addoption(
"--cucumberjunit",
"--cucumber-junit",
action="store",
dest="cucumber_junit_path",
metavar="path",
default=None,
help="create cucumber junit style report file at given path.",
)


def configure(config: Config) -> None:
cucumber_junit_path = config.option.cucumber_junit_path
# prevent opening junit log on worker nodes (xdist)
if cucumber_junit_path and not hasattr(config, "workerinput"):
config._bddcucumberjunit = LogBDDCucumberJUNIT(cucumber_junit_path) # type: ignore[attr-defined]
config.pluginmanager.register(config._bddcucumberjunit) # type: ignore[attr-defined]


def unconfigure(config: Config) -> None:
xml = getattr(config, "_bddcucumberjunit", None) # type: ignore[attr-defined]
if xml is not None:
del config._bddcucumberjunit # type: ignore[attr-defined]
config.pluginmanager.unregister(xml)


class LogBDDCucumberJUNIT(LogBDDCucumberJSON):
"""Logging plugin for cucumber like junit output."""

def _join_and_pad(self, str1, str2, total_length=SYSTEM_OUT_DEFAULT_MESSAGE_LENGTH):
remaining = total_length - len(str1) - len(str2) - SYSTEM_OUT_MINIMUM_DOTS

if remaining >= 0:
return SYSTEM_OUT_INITIAL_INTEND + str1 + "." * (remaining + SYSTEM_OUT_MINIMUM_DOTS) + str2
else:
return self._join_and_pad(str1[:remaining], str2, total_length)

def _generate_xml_report(self) -> xml.dom.minidom.Document:
document = xml.dom.minidom.Document()

root = document.createElement("testsuite")
root.setAttribute("name", "pytest-bdd.cucumber.junit")
no_of_tests = 0
no_of_skipped = 0
no_of_failures = 0
no_of_errors = 0
scenario_time = 0

for feature in self.features.values():
for test_case in feature["elements"]:
no_of_tests += 1
test_case_doc = document.createElement("testcase")
test_case_doc.setAttribute("classname", feature["name"])
if test_case["keyword"] == "Scenario Outline":
params = test_case["id"][test_case["id"].find("[") + 1 : -1]
name = f'{test_case["name"]} - ({params})'
else:
name = test_case["name"]

test_case_doc.setAttribute("name", name)

failure = False
skipped = False
failure_doc = document.createElement("failure")
skipped_doc = document.createElement("skipped")
case_time = 0

text = "\n"
for step in test_case["steps"]:
text += self._join_and_pad(f'{step["keyword"]} {step["name"]}', step["result"]["status"]) + "\n"
case_time += step["result"]["duration"]
if step["result"]["status"] == "failed":
failure = True
failure_doc.appendChild(document.createTextNode(step["result"]["error_message"]))
elif step["result"]["status"] == "skipped":
skipped = True
skipped_doc.appendChild(document.createTextNode(step["result"]["skipped_message"]))
test_case_doc.setAttribute("time", str(case_time))
if failure:
test_case_doc.appendChild(failure_doc)
no_of_failures += 1
no_of_tests -= 1
elif skipped:
test_case_doc.appendChild(skipped_doc)
no_of_skipped += 1
no_of_tests -= 1

system_out = document.createElement("system-out")
system_out.appendChild(document.createTextNode(text + "\n"))
test_case_doc.appendChild(system_out)
root.appendChild(test_case_doc)
scenario_time += case_time

root.setAttribute("tests", str(no_of_tests))
root.setAttribute("skipped", str(no_of_skipped))
root.setAttribute("failures", str(no_of_failures))
root.setAttribute("errors", str(no_of_errors))
root.setAttribute("time", str(scenario_time))

document.appendChild(root)
return document

def pytest_sessionfinish(self) -> None:
document = self._generate_xml_report()
with open(self.logfile, "w", encoding="utf-8") as logfile:
document.writexml(logfile, indent=" ", addindent=" ", newl="\n", encoding="utf-8")

def pytest_terminal_summary(self, terminalreporter: TerminalReporter) -> None:
terminalreporter.write_sep("-", f"generated junit file: {self.logfile}")
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from _pytest.reports import TestReport
from _pytest.runner import CallInfo

from .parser import Feature, Scenario, Step
from ..parser import Feature, Scenario, Step


class StepReport:
Expand Down
Empty file.
103 changes: 103 additions & 0 deletions tests/feature/reports/cucumber_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import textwrap


class OfType:
"""Helper object to help compare object type to initialization type"""

def __init__(self, type: type | None = None) -> None:
self.type = type

def __eq__(self, other: object) -> bool:
return isinstance(other, self.type) if self.type else True


def create_test(pytester):
pytester.makefile(
".ini",
pytest=textwrap.dedent(
"""
[pytest]
markers =
scenario-passing-tag
scenario-failing-tag
scenario-outline-passing-tag
feature-tag
"""
),
)
pytester.makefile(
".feature",
test=textwrap.dedent(
"""
@feature-tag
Feature: One passing scenario, one failing scenario

@scenario-passing-tag
Scenario: Passing
Given a passing step
And some other passing step

@scenario-failing-tag
Scenario: Failing
Given a passing step
And a failing step

@scenario-outline-passing-tag
Scenario Outline: Passing outline
Given type <type> and value <value>

Examples: example1
| type | value |
| str | hello |
| int | 42 |
| float | 1.0 |

Scenario: Skipping test
Given a skipping step
"""
),
)
pytester.makepyfile(
textwrap.dedent(
"""
import pytest
from pytest_bdd import given, when, scenario, parsers

@given('a passing step')
def _():
return 'pass'

@given('some other passing step')
def _():
return 'pass'

@given('a failing step')
def _():
raise Exception('Error')

@given('a skipping step')
def _():
pytest.skip('skipping')

@given(parsers.parse('type {type} and value {value}'))
def _():
return 'pass'

@scenario('test.feature', 'Passing')
def test_passing():
pass

@scenario('test.feature', 'Failing')
def test_failing():
pass

@scenario('test.feature', 'Passing outline')
def test_passing_outline():
pass

@scenario('test.feature', 'Skipping test')
def test_skipping():
pass
"""
)
)
Loading