Skip to content

Commit

Permalink
Merge pull request #122 from elchupanebrej/feature/self_descripting_g…
Browse files Browse the repository at this point in the history
…herkin

Add example of Cucumber ndjson report usage
  • Loading branch information
elchupanebrej authored Oct 7, 2024
2 parents 3716f68 + a2c784e commit f71b89b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
9 changes: 9 additions & 0 deletions docs/features.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,12 @@ Scenario search from base directory

.. include:: ../features/Feature/Load/Scenario search from base directory.feature
:code: gherkin

Report
------

New line delimited JSON
#######################

.. include:: ../features/Report/New line delimited JSON.feature
:code: gherkin
22 changes: 22 additions & 0 deletions features/Report/New line delimited JSON.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Feature: NDJson could be produced on the feature run
Scenario:
Given File "Passing.feature" with content:
"""gherkin
Feature: Passing feature
Scenario: Passing scenario
Given Passing step
"""
And File "conftest.py" with content:
"""python
from pytest_bdd import step
collect_ignore_glob = ["*.lock"] # Workaround to allow launch this test under Gherkin itself
@step('Passing step')
def _():
...
"""
When run pytest
|cli_args |--messagesndjson|out.ndjson|
Then File "out.ndjson" has "15" lines
Then Report "out.ndjson" parsable into messages
2 changes: 1 addition & 1 deletion src/pytest_bdd/message_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def process_messages(queue: Queue, stop_event: Event, messages_file_path: Union[
last_enter = True
lock = FileLock(f"{messages_file_path}.lock")
with lock:
with Path(messages_file_path).open(mode="at+", buffering=1) as f:
with Path(messages_file_path).open(mode="at+", buffering=1, encoding="utf-8") as f:
lines = []
while not queue.empty():
try:
Expand Down
25 changes: 24 additions & 1 deletion tests/e2e/conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import json
import re
import shutil
import string
from functools import reduce
from operator import attrgetter, itemgetter
from pathlib import Path
from typing import TYPE_CHECKING

from pytest import fixture
from pytest_httpserver import HTTPServer

from pytest_bdd import given, step
from messages import Envelope # type:ignore[attr-defined]
from pytest_bdd import given, step, then
from pytest_bdd.compatibility.pytest import assert_outcomes
from pytest_bdd.mimetypes import Mimetype
from pytest_bdd.testing_utils import data_table_to_dicts
Expand Down Expand Up @@ -99,3 +102,23 @@ def copy_path(request, testdir: "Testdir", initial_path, final_path, step):
shutil.copy(full_initial_path, full_final_path)
else:
shutil.copytree(full_initial_path, full_final_path, dirs_exist_ok=True)


@then(
re.compile(r"File \"(?P<file_path>(\w|\\|.)+)\" has \"(?P<line_count>(\w|\\|.)+)\" lines"),
converters=dict(line_count=int, file_path=Path),
)
def _(file_path: Path, line_count: int):
with file_path.open("r") as fp:
real_line_count = reduce(lambda _, last: last, map(itemgetter(0), enumerate(fp, start=1)))
assert line_count == real_line_count


@then(re.compile(r"Report \"(?P<file_path>(\w|\\|.)+)\" parsable into messages"), converters=dict(file_path=Path))
def _(file_path: Path):
with file_path.open(mode="r") as ast_file:
try:
for raw_datum in ast_file:
Envelope.model_validate(json.loads(raw_datum))
except Exception as e:
raise AssertionError from e

0 comments on commit f71b89b

Please sign in to comment.