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

Add example of Cucumber ndjson report usage #122

Merged
merged 2 commits into from
Oct 7, 2024
Merged
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
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
Loading