From dc2b8e8bb6b603c11a8a0648bf3c7ad836d633f0 Mon Sep 17 00:00:00 2001 From: Kostiantyn Goloveshko Date: Mon, 7 Oct 2024 20:12:04 +0300 Subject: [PATCH 1/2] Add example of Cucumber ndjson report usage --- docs/features.rst | 337 +++++++++--------- .../Report/New line delimited JSON.feature | 22 ++ src/pytest_bdd/message_plugin.py | 2 +- tests/e2e/conftest.py | 25 +- 4 files changed, 220 insertions(+), 166 deletions(-) create mode 100644 features/Report/New line delimited JSON.feature diff --git a/docs/features.rst b/docs/features.rst index f98625af..8e9945e2 100644 --- a/docs/features.rst +++ b/docs/features.rst @@ -1,164 +1,173 @@ -Features -======== - -.. NOTE:: Features below are part of end-to-end test suite; You always could find most specific - use cases of **pytest-bdd-ng** by investigation of its regression - test suite https://github.com/elchupanebrej/pytest-bdd-ng/tree/default/tests - - - -Scenario --------- - -Description -########### - -.. include:: ../features/Scenario/Description.feature - :code: gherkin - -Tag -### - -.. include:: ../features/Scenario/Tag.feature - :code: gherkin - -Outline -####### - -Examples Tag -!!!!!!!!!!!! - -.. include:: ../features/Scenario/Outline/Examples Tag.feature - :code: gherkin - -Tutorial --------- - -Launch -###### - -.. include:: ../features/Tutorial/Launch.feature - :code: gherkin - -Step definition ---------------- - -Target fixtures specification -############################# - -.. include:: ../features/Step definition/Target fixtures specification.feature - :code: gherkin - -Pytest fixtures substitution -############################ - -.. include:: ../features/Step definition/Pytest fixtures substitution.feature - :code: gherkin - -Parameters -########## - -Defaults -!!!!!!!! - -.. include:: ../features/Step definition/Parameters/Defaults.feature - :code: gherkin - -Injection as fixtures -!!!!!!!!!!!!!!!!!!!!! - -.. include:: ../features/Step definition/Parameters/Injection as fixtures.feature - :code: gherkin - -Conversion -!!!!!!!!!! - -.. include:: ../features/Step definition/Parameters/Conversion.feature - :code: gherkin - -Parsing by custom parser -!!!!!!!!!!!!!!!!!!!!!!!! - -.. include:: ../features/Step definition/Parameters/Parsing by custom parser.feature - :code: gherkin - -Parsing -!!!!!!! - -.. include:: ../features/Step definition/Parameters/Parsing.feature - :code: gherkin - -Step ----- - -Doc string -########## - -.. include:: ../features/Step/Doc string.feature - :code: gherkin - -Data table -########## - -.. include:: ../features/Step/Data table.feature - :code: gherkin - -Step definition bounding -######################## - -.. include:: ../features/Step/Step definition bounding.feature - :code: gherkin - -Feature -------- - -Localization -############ - -.. include:: ../features/Feature/Localization.feature - :code: gherkin - -Description -########### - -.. include:: ../features/Feature/Description.feature - :code: gherkin - -Tag conversion -############## - -.. include:: ../features/Feature/Tag conversion.feature - :code: gherkin - -Tag -### - -.. include:: ../features/Feature/Tag.feature - :code: gherkin - -Load -#### - -Scenario search from base url -!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - -.. include:: ../features/Feature/Load/Scenario search from base url.feature - :code: gherkin - -Autoload -!!!!!!!! - -.. include:: ../features/Feature/Load/Autoload.feature - :code: gherkin - -Scenario function loader -!!!!!!!!!!!!!!!!!!!!!!!! - -.. include:: ../features/Feature/Load/Scenario function loader.feature - :code: gherkin - -Scenario search from base directory -!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - -.. include:: ../features/Feature/Load/Scenario search from base directory.feature - :code: gherkin +Features +======== + +.. NOTE:: Features below are part of end-to-end test suite; You always could find most specific + use cases of **pytest-bdd-ng** by investigation of its regression + test suite https://github.com/elchupanebrej/pytest-bdd-ng/tree/default/tests + + + +Tutorial +-------- + +Launch +###### + +.. include:: ../features/Tutorial/Launch.feature + :code: gherkin + +Step definition +--------------- + +Pytest fixtures substitution +############################ + +.. include:: ../features/Step definition/Pytest fixtures substitution.feature + :code: gherkin + +Target fixtures specification +############################# + +.. include:: ../features/Step definition/Target fixtures specification.feature + :code: gherkin + +Parameters +########## + +Conversion +!!!!!!!!!! + +.. include:: ../features/Step definition/Parameters/Conversion.feature + :code: gherkin + +Defaults +!!!!!!!! + +.. include:: ../features/Step definition/Parameters/Defaults.feature + :code: gherkin + +Injection as fixtures +!!!!!!!!!!!!!!!!!!!!! + +.. include:: ../features/Step definition/Parameters/Injection as fixtures.feature + :code: gherkin + +Parsing by custom parser +!!!!!!!!!!!!!!!!!!!!!!!! + +.. include:: ../features/Step definition/Parameters/Parsing by custom parser.feature + :code: gherkin + +Parsing +!!!!!!! + +.. include:: ../features/Step definition/Parameters/Parsing.feature + :code: gherkin + +Step +---- + +Data table +########## + +.. include:: ../features/Step/Data table.feature + :code: gherkin + +Doc string +########## + +.. include:: ../features/Step/Doc string.feature + :code: gherkin + +Step definition bounding +######################## + +.. include:: ../features/Step/Step definition bounding.feature + :code: gherkin + +Scenario +-------- + +Description +########### + +.. include:: ../features/Scenario/Description.feature + :code: gherkin + +Tag +### + +.. include:: ../features/Scenario/Tag.feature + :code: gherkin + +Outline +####### + +Examples Tag +!!!!!!!!!!!! + +.. include:: ../features/Scenario/Outline/Examples Tag.feature + :code: gherkin + +Report +------ + +New line delimited JSON +####################### + +.. include:: ../features/Report/New line delimited JSON.feature + :code: gherkin + +Feature +------- + +Description +########### + +.. include:: ../features/Feature/Description.feature + :code: gherkin + +Localization +############ + +.. include:: ../features/Feature/Localization.feature + :code: gherkin + +Tag conversion +############## + +.. include:: ../features/Feature/Tag conversion.feature + :code: gherkin + +Tag +### + +.. include:: ../features/Feature/Tag.feature + :code: gherkin + +Load +#### + +Autoload +!!!!!!!! + +.. include:: ../features/Feature/Load/Autoload.feature + :code: gherkin + +Scenario function loader +!!!!!!!!!!!!!!!!!!!!!!!! + +.. include:: ../features/Feature/Load/Scenario function loader.feature + :code: gherkin + +Scenario search from base directory +!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. include:: ../features/Feature/Load/Scenario search from base directory.feature + :code: gherkin + +Scenario search from base url +!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. include:: ../features/Feature/Load/Scenario search from base url.feature + :code: gherkin diff --git a/features/Report/New line delimited JSON.feature b/features/Report/New line delimited JSON.feature new file mode 100644 index 00000000..36ae7cfc --- /dev/null +++ b/features/Report/New line delimited JSON.feature @@ -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 diff --git a/src/pytest_bdd/message_plugin.py b/src/pytest_bdd/message_plugin.py index dbafe41c..de50c87c 100644 --- a/src/pytest_bdd/message_plugin.py +++ b/src/pytest_bdd/message_plugin.py @@ -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: diff --git a/tests/e2e/conftest.py b/tests/e2e/conftest.py index 852b1113..1eaa3f5e 100644 --- a/tests/e2e/conftest.py +++ b/tests/e2e/conftest.py @@ -1,6 +1,8 @@ +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 @@ -8,7 +10,8 @@ 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 @@ -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(\w|\\|.)+)\" has \"(?P(\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(\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 From a2c784e566a5d214a865771f49383bce4abcd9a6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 7 Oct 2024 17:15:58 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/features.rst | 346 +++++++++++++++++++++++----------------------- 1 file changed, 173 insertions(+), 173 deletions(-) diff --git a/docs/features.rst b/docs/features.rst index 8e9945e2..705fade8 100644 --- a/docs/features.rst +++ b/docs/features.rst @@ -1,173 +1,173 @@ -Features -======== - -.. NOTE:: Features below are part of end-to-end test suite; You always could find most specific - use cases of **pytest-bdd-ng** by investigation of its regression - test suite https://github.com/elchupanebrej/pytest-bdd-ng/tree/default/tests - - - -Tutorial --------- - -Launch -###### - -.. include:: ../features/Tutorial/Launch.feature - :code: gherkin - -Step definition ---------------- - -Pytest fixtures substitution -############################ - -.. include:: ../features/Step definition/Pytest fixtures substitution.feature - :code: gherkin - -Target fixtures specification -############################# - -.. include:: ../features/Step definition/Target fixtures specification.feature - :code: gherkin - -Parameters -########## - -Conversion -!!!!!!!!!! - -.. include:: ../features/Step definition/Parameters/Conversion.feature - :code: gherkin - -Defaults -!!!!!!!! - -.. include:: ../features/Step definition/Parameters/Defaults.feature - :code: gherkin - -Injection as fixtures -!!!!!!!!!!!!!!!!!!!!! - -.. include:: ../features/Step definition/Parameters/Injection as fixtures.feature - :code: gherkin - -Parsing by custom parser -!!!!!!!!!!!!!!!!!!!!!!!! - -.. include:: ../features/Step definition/Parameters/Parsing by custom parser.feature - :code: gherkin - -Parsing -!!!!!!! - -.. include:: ../features/Step definition/Parameters/Parsing.feature - :code: gherkin - -Step ----- - -Data table -########## - -.. include:: ../features/Step/Data table.feature - :code: gherkin - -Doc string -########## - -.. include:: ../features/Step/Doc string.feature - :code: gherkin - -Step definition bounding -######################## - -.. include:: ../features/Step/Step definition bounding.feature - :code: gherkin - -Scenario --------- - -Description -########### - -.. include:: ../features/Scenario/Description.feature - :code: gherkin - -Tag -### - -.. include:: ../features/Scenario/Tag.feature - :code: gherkin - -Outline -####### - -Examples Tag -!!!!!!!!!!!! - -.. include:: ../features/Scenario/Outline/Examples Tag.feature - :code: gherkin - -Report ------- - -New line delimited JSON -####################### - -.. include:: ../features/Report/New line delimited JSON.feature - :code: gherkin - -Feature -------- - -Description -########### - -.. include:: ../features/Feature/Description.feature - :code: gherkin - -Localization -############ - -.. include:: ../features/Feature/Localization.feature - :code: gherkin - -Tag conversion -############## - -.. include:: ../features/Feature/Tag conversion.feature - :code: gherkin - -Tag -### - -.. include:: ../features/Feature/Tag.feature - :code: gherkin - -Load -#### - -Autoload -!!!!!!!! - -.. include:: ../features/Feature/Load/Autoload.feature - :code: gherkin - -Scenario function loader -!!!!!!!!!!!!!!!!!!!!!!!! - -.. include:: ../features/Feature/Load/Scenario function loader.feature - :code: gherkin - -Scenario search from base directory -!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - -.. include:: ../features/Feature/Load/Scenario search from base directory.feature - :code: gherkin - -Scenario search from base url -!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - -.. include:: ../features/Feature/Load/Scenario search from base url.feature - :code: gherkin +Features +======== + +.. NOTE:: Features below are part of end-to-end test suite; You always could find most specific + use cases of **pytest-bdd-ng** by investigation of its regression + test suite https://github.com/elchupanebrej/pytest-bdd-ng/tree/default/tests + + + +Scenario +-------- + +Description +########### + +.. include:: ../features/Scenario/Description.feature + :code: gherkin + +Tag +### + +.. include:: ../features/Scenario/Tag.feature + :code: gherkin + +Outline +####### + +Examples Tag +!!!!!!!!!!!! + +.. include:: ../features/Scenario/Outline/Examples Tag.feature + :code: gherkin + +Tutorial +-------- + +Launch +###### + +.. include:: ../features/Tutorial/Launch.feature + :code: gherkin + +Step definition +--------------- + +Target fixtures specification +############################# + +.. include:: ../features/Step definition/Target fixtures specification.feature + :code: gherkin + +Pytest fixtures substitution +############################ + +.. include:: ../features/Step definition/Pytest fixtures substitution.feature + :code: gherkin + +Parameters +########## + +Defaults +!!!!!!!! + +.. include:: ../features/Step definition/Parameters/Defaults.feature + :code: gherkin + +Injection as fixtures +!!!!!!!!!!!!!!!!!!!!! + +.. include:: ../features/Step definition/Parameters/Injection as fixtures.feature + :code: gherkin + +Conversion +!!!!!!!!!! + +.. include:: ../features/Step definition/Parameters/Conversion.feature + :code: gherkin + +Parsing by custom parser +!!!!!!!!!!!!!!!!!!!!!!!! + +.. include:: ../features/Step definition/Parameters/Parsing by custom parser.feature + :code: gherkin + +Parsing +!!!!!!! + +.. include:: ../features/Step definition/Parameters/Parsing.feature + :code: gherkin + +Step +---- + +Doc string +########## + +.. include:: ../features/Step/Doc string.feature + :code: gherkin + +Data table +########## + +.. include:: ../features/Step/Data table.feature + :code: gherkin + +Step definition bounding +######################## + +.. include:: ../features/Step/Step definition bounding.feature + :code: gherkin + +Feature +------- + +Localization +############ + +.. include:: ../features/Feature/Localization.feature + :code: gherkin + +Description +########### + +.. include:: ../features/Feature/Description.feature + :code: gherkin + +Tag conversion +############## + +.. include:: ../features/Feature/Tag conversion.feature + :code: gherkin + +Tag +### + +.. include:: ../features/Feature/Tag.feature + :code: gherkin + +Load +#### + +Scenario search from base url +!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. include:: ../features/Feature/Load/Scenario search from base url.feature + :code: gherkin + +Autoload +!!!!!!!! + +.. include:: ../features/Feature/Load/Autoload.feature + :code: gherkin + +Scenario function loader +!!!!!!!!!!!!!!!!!!!!!!!! + +.. include:: ../features/Feature/Load/Scenario function loader.feature + :code: gherkin + +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