From 4d889205f80a65b33b10e3fe52252bc39fe41f4b Mon Sep 17 00:00:00 2001 From: Denis Bautin Date: Fri, 8 Jan 2021 19:25:17 +0500 Subject: [PATCH 01/18] Add to coftest.py reference on pytester plugin --- allure-pytest-bdd/test/conftest.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/allure-pytest-bdd/test/conftest.py b/allure-pytest-bdd/test/conftest.py index b925ac4d..7a9f79a8 100644 --- a/allure-pytest-bdd/test/conftest.py +++ b/allure-pytest-bdd/test/conftest.py @@ -8,6 +8,9 @@ from pytest_bdd import given, when, parsers +pytest_plugins = "pytester" + + @contextmanager def fake_logger(path, logger): blocked_plugins = [] From 0719d641a1ed47a789ed2bcb2ce94e73029a93cc Mon Sep 17 00:00:00 2001 From: Denis Bautin Date: Fri, 8 Jan 2021 19:28:21 +0500 Subject: [PATCH 02/18] Move feature directory to allure-pytest-bdd\test\ --- allure-pytest-bdd/{ => test}/features/background.feature | 0 allure-pytest-bdd/{ => test}/features/outline.feature | 0 allure-pytest-bdd/{ => test}/features/scenario.feature | 0 allure-pytest-bdd/test/outline_test.py | 2 +- allure-pytest-bdd/test/scenario_test.py | 2 +- 5 files changed, 2 insertions(+), 2 deletions(-) rename allure-pytest-bdd/{ => test}/features/background.feature (100%) rename allure-pytest-bdd/{ => test}/features/outline.feature (100%) rename allure-pytest-bdd/{ => test}/features/scenario.feature (100%) diff --git a/allure-pytest-bdd/features/background.feature b/allure-pytest-bdd/test/features/background.feature similarity index 100% rename from allure-pytest-bdd/features/background.feature rename to allure-pytest-bdd/test/features/background.feature diff --git a/allure-pytest-bdd/features/outline.feature b/allure-pytest-bdd/test/features/outline.feature similarity index 100% rename from allure-pytest-bdd/features/outline.feature rename to allure-pytest-bdd/test/features/outline.feature diff --git a/allure-pytest-bdd/features/scenario.feature b/allure-pytest-bdd/test/features/scenario.feature similarity index 100% rename from allure-pytest-bdd/features/scenario.feature rename to allure-pytest-bdd/test/features/scenario.feature diff --git a/allure-pytest-bdd/test/outline_test.py b/allure-pytest-bdd/test/outline_test.py index d176148a..43361d3b 100644 --- a/allure-pytest-bdd/test/outline_test.py +++ b/allure-pytest-bdd/test/outline_test.py @@ -1,6 +1,6 @@ from pytest_bdd import scenario -@scenario("../features/outline.feature", "Scenario outline") +@scenario("features/outline.feature", "Scenario outline") def test_scenario_outline(): pass diff --git a/allure-pytest-bdd/test/scenario_test.py b/allure-pytest-bdd/test/scenario_test.py index 18362409..b8a19582 100644 --- a/allure-pytest-bdd/test/scenario_test.py +++ b/allure-pytest-bdd/test/scenario_test.py @@ -1,6 +1,6 @@ from pytest_bdd import scenario -@scenario("../features/scenario.feature", "Simple passed scenario") +@scenario("features/scenario.feature", "Simple passed scenario") def test_simple_passed_scenario(): pass From 0600bca64986bf09d4d0c28f83145dd170d236e8 Mon Sep 17 00:00:00 2001 From: Denis Bautin Date: Sat, 9 Jan 2021 21:03:56 +0500 Subject: [PATCH 03/18] v1.0 Bug fix #474 Add file attachment_worker.py with AttachmentWorker class Now PytestBDDListener use AttachmentWorker for deleting duplicate of attachments --- allure-pytest-bdd/src/attachment_worker.py | 51 ++++++++++++++++++++ allure-pytest-bdd/src/pytest_bdd_listener.py | 5 ++ 2 files changed, 56 insertions(+) create mode 100644 allure-pytest-bdd/src/attachment_worker.py diff --git a/allure-pytest-bdd/src/attachment_worker.py b/allure-pytest-bdd/src/attachment_worker.py new file mode 100644 index 00000000..c05c7763 --- /dev/null +++ b/allure-pytest-bdd/src/attachment_worker.py @@ -0,0 +1,51 @@ +import os.path +import os + + +class AttachmentWorker: + + def __init__(self, test_result, item): + self.test_result = test_result + self.attachments_dir = AttachmentWorker.get_path_to_attachments(item) + + @staticmethod + def get_path_to_attachments(item): + allure_dir = os.path.normpath( + item.config.invocation_params.args[0].split('=')[1]) + if os.path.isabs(allure_dir): + return allure_dir + else: + project_dir = str(item.config.invocation_params.dir) + return os.path.join(project_dir, allure_dir.lstrip("\\")) + + def delete_duplicates(self): + if len(self.test_result.attachments) == 0: + return + + for step in self.test_result.steps: + for attach in step.attachments: + to_delete = self._find_duplicate(attach) + if to_delete is not None: + self.test_result.attachments.remove(to_delete) + os.remove(os.path.join(self.attachments_dir, to_delete.source)) + + def _find_duplicate(self, attachment_from_step): + for attachment in self.test_result.attachments: + if self._are_attachments_equal(attachment, attachment_from_step): + return attachment + + return None + + def _are_attachments_equal(self, first, second): + first_file = open(os.path.join(self.attachments_dir, first.source), 'br') + first_content = first_file.read() + first_file.close() + + second_file = open(os.path.join(self.attachments_dir, second.source), 'br') + second_content = second_file.read() + second_file.close() + + return \ + first.name == second.name and \ + first.type == second.type and \ + first_content == second_content diff --git a/allure-pytest-bdd/src/pytest_bdd_listener.py b/allure-pytest-bdd/src/pytest_bdd_listener.py index 0ed04f13..668d618a 100644 --- a/allure-pytest-bdd/src/pytest_bdd_listener.py +++ b/allure-pytest-bdd/src/pytest_bdd_listener.py @@ -17,6 +17,8 @@ from allure_commons.lifecycle import AllureLifecycle from .utils import get_full_name, get_name, get_params +from .attachment_worker import AttachmentWorker + class PytestBDDListener(object): def __init__(self): @@ -113,6 +115,9 @@ def pytest_runtest_makereport(self, item, call): test_result.status = status test_result.statusDetails = status_details + if test_result is not None and test_result.status is not None: + AttachmentWorker(test_result, item).delete_duplicates() + if report.when == 'teardown': self.lifecycle.write_test_case(uuid=uuid) From 3dc630dd90f3d81efe8f9e782f9d6f74a19a144f Mon Sep 17 00:00:00 2001 From: Denis Bautin Date: Sat, 9 Jan 2021 21:24:06 +0500 Subject: [PATCH 04/18] Add files for test --- .../test/Bug474Tests/bug474.feature | 5 +++ .../test/Bug474Tests/test_bug474.py | 31 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 allure-pytest-bdd/test/Bug474Tests/bug474.feature create mode 100644 allure-pytest-bdd/test/Bug474Tests/test_bug474.py diff --git a/allure-pytest-bdd/test/Bug474Tests/bug474.feature b/allure-pytest-bdd/test/Bug474Tests/bug474.feature new file mode 100644 index 00000000..a033eb2a --- /dev/null +++ b/allure-pytest-bdd/test/Bug474Tests/bug474.feature @@ -0,0 +1,5 @@ +Feature: Feature Test + Scenario: My Scenario Test + Given two numbers: 2, 3 + When addition it + Then must be sum of it diff --git a/allure-pytest-bdd/test/Bug474Tests/test_bug474.py b/allure-pytest-bdd/test/Bug474Tests/test_bug474.py new file mode 100644 index 00000000..87a51468 --- /dev/null +++ b/allure-pytest-bdd/test/Bug474Tests/test_bug474.py @@ -0,0 +1,31 @@ +from pytest_bdd import scenario, given, when, then, parsers +import pytest +import allure + +a = 0 +b = 0 + + +@given(parsers.parse("two numbers: {first}, {second}")) +def step_impl(first, second): + global a, b + a = first + b = second + + +@pytest.fixture() +@when("addition it") +def addition(): + allure.attach('A text attachment in module scope fixture', 'blah blah blah', allure.attachment_type.TEXT) + sum_ = a+b + return sum_ + + +@then("must be sum of it") +def check_sum(addition): + assert a+b == addition + + +@scenario("bug474.feature", "My Scenario Test") +def test_my_scenario(): + pass From c6f1048437bd6c0a58fb3963d9c03df63c4467dc Mon Sep 17 00:00:00 2001 From: Denis Bautin Date: Sat, 9 Jan 2021 23:37:46 +0500 Subject: [PATCH 05/18] Small refactor --- allure-pytest-bdd/src/pytest_bdd_listener.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/allure-pytest-bdd/src/pytest_bdd_listener.py b/allure-pytest-bdd/src/pytest_bdd_listener.py index 668d618a..76e5267f 100644 --- a/allure-pytest-bdd/src/pytest_bdd_listener.py +++ b/allure-pytest-bdd/src/pytest_bdd_listener.py @@ -115,7 +115,7 @@ def pytest_runtest_makereport(self, item, call): test_result.status = status test_result.statusDetails = status_details - if test_result is not None and test_result.status is not None: + if test_result and test_result.status: AttachmentWorker(test_result, item).delete_duplicates() if report.when == 'teardown': From fb27f2da80e63ea8ecff13c49b3506b5e246eb93 Mon Sep 17 00:00:00 2001 From: Denis Bautin Date: Sun, 10 Jan 2021 03:40:17 +0500 Subject: [PATCH 06/18] Fix AttachmentWorker --- allure-pytest-bdd/src/attachment_worker.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/allure-pytest-bdd/src/attachment_worker.py b/allure-pytest-bdd/src/attachment_worker.py index c05c7763..ae03d259 100644 --- a/allure-pytest-bdd/src/attachment_worker.py +++ b/allure-pytest-bdd/src/attachment_worker.py @@ -10,14 +10,25 @@ def __init__(self, test_result, item): @staticmethod def get_path_to_attachments(item): + splitted_param = AttachmentWorker._get_allurdir_param(item).split('=') + + project_dir = str(item.config.invocation_params.dir) + if len(splitted_param) == 1: + return project_dir + allure_dir = os.path.normpath( - item.config.invocation_params.args[0].split('=')[1]) + splitted_param[1].lstrip()) if os.path.isabs(allure_dir): return allure_dir else: - project_dir = str(item.config.invocation_params.dir) return os.path.join(project_dir, allure_dir.lstrip("\\")) + @staticmethod + def _get_allurdir_param(item): + for param in item.config.invocation_params.args: + if param.startswith("--alluredir"): + return param + def delete_duplicates(self): if len(self.test_result.attachments) == 0: return From dce0f92f2ac4526812e69e8ccde7e4eaf59847dd Mon Sep 17 00:00:00 2001 From: Denis Bautin Date: Sun, 10 Jan 2021 03:51:31 +0500 Subject: [PATCH 07/18] Small refactoring of AttachmentWorker --- allure-pytest-bdd/src/attachment_worker.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/allure-pytest-bdd/src/attachment_worker.py b/allure-pytest-bdd/src/attachment_worker.py index ae03d259..cf571eb3 100644 --- a/allure-pytest-bdd/src/attachment_worker.py +++ b/allure-pytest-bdd/src/attachment_worker.py @@ -16,8 +16,7 @@ def get_path_to_attachments(item): if len(splitted_param) == 1: return project_dir - allure_dir = os.path.normpath( - splitted_param[1].lstrip()) + allure_dir = os.path.normpath(splitted_param[1]) if os.path.isabs(allure_dir): return allure_dir else: From 5979b6004ed238ca287541229bbf00cec1b74c3b Mon Sep 17 00:00:00 2001 From: Denis Bautin Date: Sun, 10 Jan 2021 03:54:34 +0500 Subject: [PATCH 08/18] Add test for bug fix #474 --- .../test/Bug474Tests/bug474.feature | 51 +++++++++++++++++-- .../test/Bug474Tests/test_bug474.py | 38 +++++--------- 2 files changed, 58 insertions(+), 31 deletions(-) diff --git a/allure-pytest-bdd/test/Bug474Tests/bug474.feature b/allure-pytest-bdd/test/Bug474Tests/bug474.feature index a033eb2a..6be1a5a3 100644 --- a/allure-pytest-bdd/test/Bug474Tests/bug474.feature +++ b/allure-pytest-bdd/test/Bug474Tests/bug474.feature @@ -1,5 +1,46 @@ -Feature: Feature Test - Scenario: My Scenario Test - Given two numbers: 2, 3 - When addition it - Then must be sum of it +Feature: Bug #474 + Scenario: allure.attach calling in method decorated with When and Pytest.fixture + Given example.feature with content: + """ + Feature: Feature Test + Scenario: My Scenario Test + Given two numbers: 2, 3 + When addition it + Then must be sum of it + """ + And example_test.py with content: + """ + from pytest_bdd import scenario, given, when, then, parsers + import pytest + import allure + + a = 0 + b = 0 + + + @given(parsers.parse("two numbers: {first}, {second}")) + def step_impl(first, second): + global a, b + a = first + b = second + + + @pytest.fixture() + @when("addition it") + def addition(): + allure.attach('A text attachment in module scope fixture', 'blah blah blah', allure.attachment_type.TEXT) + sum_ = a+b + return sum_ + + + @then("must be sum of it") + def check_sum(addition): + assert a+b == addition + + + @scenario("example.feature", "My Scenario Test") + def test_my_scenario(): + pass + """ + When run pytest-bdd with allure + Then attachment must be only in when-step attachments \ No newline at end of file diff --git a/allure-pytest-bdd/test/Bug474Tests/test_bug474.py b/allure-pytest-bdd/test/Bug474Tests/test_bug474.py index 87a51468..b3b70dac 100644 --- a/allure-pytest-bdd/test/Bug474Tests/test_bug474.py +++ b/allure-pytest-bdd/test/Bug474Tests/test_bug474.py @@ -1,31 +1,17 @@ -from pytest_bdd import scenario, given, when, then, parsers -import pytest -import allure +from pytest_bdd import scenario, then -a = 0 -b = 0 - - -@given(parsers.parse("two numbers: {first}, {second}")) -def step_impl(first, second): - global a, b - a = first - b = second - - -@pytest.fixture() -@when("addition it") -def addition(): - allure.attach('A text attachment in module scope fixture', 'blah blah blah', allure.attachment_type.TEXT) - sum_ = a+b - return sum_ +@scenario("bug474.feature", "allure.attach calling in method decorated with When and Pytest.fixture") +def test_my_scenario(): + pass -@then("must be sum of it") -def check_sum(addition): - assert a+b == addition +@then("attachment must be only in when-step attachments") +def attachment_only_in_when(allure_report): + test_case_report = allure_report.test_cases[0] + when_step_report = next(step for step in test_case_report["steps"] + if step["name"].startswith("When")) -@scenario("bug474.feature", "My Scenario Test") -def test_my_scenario(): - pass + assert "attachments" not in test_case_report.keys() + assert len(when_step_report["attachments"]) == 1 + assert when_step_report["attachments"][0]["name"] == "blah blah blah" From e694c509d2e7005fe45fcc5cd067e142f633ada5 Mon Sep 17 00:00:00 2001 From: Denis Bautin Date: Mon, 11 Jan 2021 20:51:59 +0500 Subject: [PATCH 09/18] Fix and refactor AttachmentWorker --- allure-pytest-bdd/src/attachment_worker.py | 34 +++++++++++----------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/allure-pytest-bdd/src/attachment_worker.py b/allure-pytest-bdd/src/attachment_worker.py index cf571eb3..6f2fdb54 100644 --- a/allure-pytest-bdd/src/attachment_worker.py +++ b/allure-pytest-bdd/src/attachment_worker.py @@ -8,6 +8,17 @@ def __init__(self, test_result, item): self.test_result = test_result self.attachments_dir = AttachmentWorker.get_path_to_attachments(item) + def delete_duplicates(self): + if len(self.test_result.attachments) == 0: + return + + for step in self.test_result.steps: + for attach in step.attachments: + to_delete = self._find_duplicate(attach) + if to_delete is not None: + self.test_result.attachments.remove(to_delete) + os.remove(os.path.join(self.attachments_dir, to_delete.source)) + @staticmethod def get_path_to_attachments(item): splitted_param = AttachmentWorker._get_allurdir_param(item).split('=') @@ -22,23 +33,6 @@ def get_path_to_attachments(item): else: return os.path.join(project_dir, allure_dir.lstrip("\\")) - @staticmethod - def _get_allurdir_param(item): - for param in item.config.invocation_params.args: - if param.startswith("--alluredir"): - return param - - def delete_duplicates(self): - if len(self.test_result.attachments) == 0: - return - - for step in self.test_result.steps: - for attach in step.attachments: - to_delete = self._find_duplicate(attach) - if to_delete is not None: - self.test_result.attachments.remove(to_delete) - os.remove(os.path.join(self.attachments_dir, to_delete.source)) - def _find_duplicate(self, attachment_from_step): for attachment in self.test_result.attachments: if self._are_attachments_equal(attachment, attachment_from_step): @@ -59,3 +53,9 @@ def _are_attachments_equal(self, first, second): first.name == second.name and \ first.type == second.type and \ first_content == second_content + + @staticmethod + def _get_allurdir_param(item): + for param in item.config.invocation_params.args: + if param.startswith("--alluredir"): + return param From a1900e454e8f485476a5d6adf34db0c6cc0688e8 Mon Sep 17 00:00:00 2001 From: Ruslan Date: Mon, 11 Jan 2021 23:57:40 +0500 Subject: [PATCH 10/18] AttachmentWorker refactor --- allure-pytest-bdd/src/attachment_worker.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/allure-pytest-bdd/src/attachment_worker.py b/allure-pytest-bdd/src/attachment_worker.py index 6f2fdb54..d2be475d 100644 --- a/allure-pytest-bdd/src/attachment_worker.py +++ b/allure-pytest-bdd/src/attachment_worker.py @@ -28,10 +28,10 @@ def get_path_to_attachments(item): return project_dir allure_dir = os.path.normpath(splitted_param[1]) - if os.path.isabs(allure_dir): - return allure_dir - else: - return os.path.join(project_dir, allure_dir.lstrip("\\")) + if not os.path.isabs(allure_dir): + allure_dir = os.path.join(project_dir, allure_dir.lstrip("\\")) + + return allure_dir def _find_duplicate(self, attachment_from_step): for attachment in self.test_result.attachments: From 76c668a47734fbf0138d3b3fdc50210e14434eb9 Mon Sep 17 00:00:00 2001 From: Denis Bautin Date: Tue, 12 Jan 2021 00:16:23 +0500 Subject: [PATCH 11/18] Create directory attachments_tests and move bug474 tests to there --- .../test/{ => attachments_tests}/Bug474Tests/bug474.feature | 0 .../test/{ => attachments_tests}/Bug474Tests/test_bug474.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename allure-pytest-bdd/test/{ => attachments_tests}/Bug474Tests/bug474.feature (100%) rename allure-pytest-bdd/test/{ => attachments_tests}/Bug474Tests/test_bug474.py (100%) diff --git a/allure-pytest-bdd/test/Bug474Tests/bug474.feature b/allure-pytest-bdd/test/attachments_tests/Bug474Tests/bug474.feature similarity index 100% rename from allure-pytest-bdd/test/Bug474Tests/bug474.feature rename to allure-pytest-bdd/test/attachments_tests/Bug474Tests/bug474.feature diff --git a/allure-pytest-bdd/test/Bug474Tests/test_bug474.py b/allure-pytest-bdd/test/attachments_tests/Bug474Tests/test_bug474.py similarity index 100% rename from allure-pytest-bdd/test/Bug474Tests/test_bug474.py rename to allure-pytest-bdd/test/attachments_tests/Bug474Tests/test_bug474.py From cd590eabd785a0bcbe70dafa81f94676a4bf6f17 Mon Sep 17 00:00:00 2001 From: Denis Bautin Date: Tue, 12 Jan 2021 00:25:17 +0500 Subject: [PATCH 12/18] Create directory tests and move outline_test.py, scenario_test.py to there --- allure-pytest-bdd/test/{ => tests}/outline_test.py | 2 +- allure-pytest-bdd/test/{ => tests}/scenario_test.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename allure-pytest-bdd/test/{ => tests}/outline_test.py (54%) rename allure-pytest-bdd/test/{ => tests}/scenario_test.py (53%) diff --git a/allure-pytest-bdd/test/outline_test.py b/allure-pytest-bdd/test/tests/outline_test.py similarity index 54% rename from allure-pytest-bdd/test/outline_test.py rename to allure-pytest-bdd/test/tests/outline_test.py index 43361d3b..d176148a 100644 --- a/allure-pytest-bdd/test/outline_test.py +++ b/allure-pytest-bdd/test/tests/outline_test.py @@ -1,6 +1,6 @@ from pytest_bdd import scenario -@scenario("features/outline.feature", "Scenario outline") +@scenario("../features/outline.feature", "Scenario outline") def test_scenario_outline(): pass diff --git a/allure-pytest-bdd/test/scenario_test.py b/allure-pytest-bdd/test/tests/scenario_test.py similarity index 53% rename from allure-pytest-bdd/test/scenario_test.py rename to allure-pytest-bdd/test/tests/scenario_test.py index b8a19582..18362409 100644 --- a/allure-pytest-bdd/test/scenario_test.py +++ b/allure-pytest-bdd/test/tests/scenario_test.py @@ -1,6 +1,6 @@ from pytest_bdd import scenario -@scenario("features/scenario.feature", "Simple passed scenario") +@scenario("../features/scenario.feature", "Simple passed scenario") def test_simple_passed_scenario(): pass From aec2b58ae26cdd6d1122f083d5476bad0e8e867e Mon Sep 17 00:00:00 2001 From: Denis Bautin Date: Tue, 12 Jan 2021 00:28:31 +0500 Subject: [PATCH 13/18] Create basis for PyFileBuilder, and acceptance.feature --- allure-pytest-bdd/test/acceptance.feature | 14 ++++++++++++++ allure-pytest-bdd/test/py_file_builder.py | 16 ++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 allure-pytest-bdd/test/acceptance.feature create mode 100644 allure-pytest-bdd/test/py_file_builder.py diff --git a/allure-pytest-bdd/test/acceptance.feature b/allure-pytest-bdd/test/acceptance.feature new file mode 100644 index 00000000..06a9330e --- /dev/null +++ b/allure-pytest-bdd/test/acceptance.feature @@ -0,0 +1,14 @@ +# Created by denis at 11.01.2021 +Feature: test + # Enter feature description here + + Scenario: scenario + Given py file with imports: pytest, pytest_bdd, allure + + And func: + """ + @given("given_step") + def my_func(): + allure.attach("blah", ...) + raise Exception("message") + """ diff --git a/allure-pytest-bdd/test/py_file_builder.py b/allure-pytest-bdd/test/py_file_builder.py new file mode 100644 index 00000000..207f083d --- /dev/null +++ b/allure-pytest-bdd/test/py_file_builder.py @@ -0,0 +1,16 @@ + +class PyFileBuilder: + + def __init__(self): + self._import_line = None + self._file_funcs = [] + + def add_imports(self, *modules_names): + """ Ex: module_names = ("pytest", ...)""" + pass + + def add_func(self, str_func): + pass + + def create_file_content(self): + pass From 7e396b50aa5769daffede0976683e111509c02f2 Mon Sep 17 00:00:00 2001 From: Ruslan Date: Tue, 12 Jan 2021 02:37:17 +0500 Subject: [PATCH 14/18] Make a PyFileBuilder class and add a tests --- allure-pytest-bdd/test/py_file_builder.py | 24 +++-- .../test/py_file_builder_test.py | 94 +++++++++++++++++++ 2 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 allure-pytest-bdd/test/py_file_builder_test.py diff --git a/allure-pytest-bdd/test/py_file_builder.py b/allure-pytest-bdd/test/py_file_builder.py index 207f083d..2f1cd00a 100644 --- a/allure-pytest-bdd/test/py_file_builder.py +++ b/allure-pytest-bdd/test/py_file_builder.py @@ -1,16 +1,26 @@ - class PyFileBuilder: - def __init__(self): + def __init__(self, name): self._import_line = None self._file_funcs = [] + self.name = name def add_imports(self, *modules_names): - """ Ex: module_names = ("pytest", ...)""" - pass + import_list = [] + + for module in modules_names: + import_list.append("import " + module) + + if len(import_list) != 0: + self._import_line = "\n".join(import_list) def add_func(self, str_func): - pass + self._file_funcs.append(str_func) + + def get_content(self): + content = "\n\n\n".join(self._file_funcs) + + if self._import_line is not None: + content = self._import_line + "\n\n\n" + content - def create_file_content(self): - pass + return content diff --git a/allure-pytest-bdd/test/py_file_builder_test.py b/allure-pytest-bdd/test/py_file_builder_test.py new file mode 100644 index 00000000..9be371dd --- /dev/null +++ b/allure-pytest-bdd/test/py_file_builder_test.py @@ -0,0 +1,94 @@ +from .py_file_builder import PyFileBuilder +import pytest + + +def test_common_func(): + imports = ["pytest", "pytest_bdd", "allure"] + funcs = [ + """@given("given_step") +def given_func(): + allure.attach("blah", ...) + raise Exception("message")""", + """@when("when_step") +def when_func(): + allure.attach("blah", ...) + raise Exception("message")""", + """@then("then_step") +def then_func(): + allure.attach("blah", ...) + raise Exception("message")""" + ] + + expected_answer = """import pytest +import pytest_bdd +import allure + + +@given("given_step") +def given_func(): + allure.attach("blah", ...) + raise Exception("message") + + +@when("when_step") +def when_func(): + allure.attach("blah", ...) + raise Exception("message") + + +@then("then_step") +def then_func(): + allure.attach("blah", ...) + raise Exception("message")""" + + file_builder = PyFileBuilder("test") + + file_builder.add_imports(*imports) + + for func in funcs: + file_builder.add_func(func) + + assert file_builder.get_content() == expected_answer + + +def test_without_imports_func(): + funcs = [ + """@given("given_step") +def given_func(): + allure.attach("blah", ...) + raise Exception("message")""", + """@when("when_step") +def when_func(): + allure.attach("blah", ...) + raise Exception("message")""", + """@then("then_step") +def then_func(): + allure.attach("blah", ...) + raise Exception("message")""" + ] + + expected_answer = """@given("given_step") +def given_func(): + allure.attach("blah", ...) + raise Exception("message") + + +@when("when_step") +def when_func(): + allure.attach("blah", ...) + raise Exception("message") + + +@then("then_step") +def then_func(): + allure.attach("blah", ...) + raise Exception("message")""" + + file_builder = PyFileBuilder("test") + + file_builder.add_imports() + + for func in funcs: + file_builder.add_func(func) + + assert file_builder.get_content() == expected_answer From aa1f176bf7497e5d91ee14ab33e559345d876e0f Mon Sep 17 00:00:00 2001 From: Ruslan Date: Tue, 12 Jan 2021 03:02:04 +0500 Subject: [PATCH 15/18] Add a valid reactions on unexpected cases and expand tests --- allure-pytest-bdd/test/py_file_builder.py | 3 ++ .../test/py_file_builder_test.py | 52 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/allure-pytest-bdd/test/py_file_builder.py b/allure-pytest-bdd/test/py_file_builder.py index 2f1cd00a..570bedd8 100644 --- a/allure-pytest-bdd/test/py_file_builder.py +++ b/allure-pytest-bdd/test/py_file_builder.py @@ -18,6 +18,9 @@ def add_func(self, str_func): self._file_funcs.append(str_func) def get_content(self): + if len(self._file_funcs) == 0: + raise Exception("There are no functions in this file") + content = "\n\n\n".join(self._file_funcs) if self._import_line is not None: diff --git a/allure-pytest-bdd/test/py_file_builder_test.py b/allure-pytest-bdd/test/py_file_builder_test.py index 9be371dd..a2154939 100644 --- a/allure-pytest-bdd/test/py_file_builder_test.py +++ b/allure-pytest-bdd/test/py_file_builder_test.py @@ -92,3 +92,55 @@ def then_func(): file_builder.add_func(func) assert file_builder.get_content() == expected_answer + + +def test_empty_func_str(): + funcs = [ + "", + """@when("when_step") +def when_func(): + allure.attach("blah", ...) + raise Exception("message")""", + """@then("then_step") +def then_func(): + allure.attach("blah", ...) + raise Exception("message")""" + ] + + expected_answer = """ + + +@when("when_step") +def when_func(): + allure.attach("blah", ...) + raise Exception("message") + + +@then("then_step") +def then_func(): + allure.attach("blah", ...) + raise Exception("message")""" + + file_builder = PyFileBuilder("test") + + file_builder.add_imports() + + for func in funcs: + file_builder.add_func(func) + + assert file_builder.get_content() == expected_answer + + +def test_have_no_added_funcs(): + imports = ["pytest", "pytest_bdd", "allure"] + funcs = [] + + file_builder = PyFileBuilder("test") + + file_builder.add_imports(*imports) + + for func in funcs: + file_builder.add_func(func) + + with pytest.raises(Exception): + file_builder.get_content() From c802bb90382162da577197d53313e01a43b23815 Mon Sep 17 00:00:00 2001 From: Denis Bautin Date: Tue, 12 Jan 2021 04:39:45 +0500 Subject: [PATCH 16/18] Add common steps for testing Update acceptance.feature --- allure-pytest-bdd/test/acceptance.feature | 10 +++-- allure-pytest-bdd/test/conftest.py | 49 ++++++++++++++++++++++- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/allure-pytest-bdd/test/acceptance.feature b/allure-pytest-bdd/test/acceptance.feature index 06a9330e..382af5e1 100644 --- a/allure-pytest-bdd/test/acceptance.feature +++ b/allure-pytest-bdd/test/acceptance.feature @@ -3,12 +3,14 @@ Feature: test # Enter feature description here Scenario: scenario - Given py file with imports: pytest, pytest_bdd, allure - - And func: + Given py file with name: example + And with imports: pytest, pytest_bdd, allure + And with func: """ - @given("given_step") + @pytest_bdd.given("given_step") def my_func(): allure.attach("blah", ...) raise Exception("message") """ + And test for scenario_name from file.feature + And py file saved diff --git a/allure-pytest-bdd/test/conftest.py b/allure-pytest-bdd/test/conftest.py index 7a9f79a8..319da993 100644 --- a/allure-pytest-bdd/test/conftest.py +++ b/allure-pytest-bdd/test/conftest.py @@ -4,9 +4,10 @@ import allure_commons from allure_commons_test.report import AllureReport from allure_commons.logger import AllureFileLogger -from .steps import * # noqa F401 F403 +from .steps import * # noqa F401 F403 from pytest_bdd import given, when, parsers +from .py_file_builder import PyFileBuilder pytest_plugins = "pytester" @@ -64,3 +65,49 @@ def feature_definition(name, extension, content, testdir): @when("run pytest-bdd with allure") def run(allured_testdir): allured_testdir.run_with_allure() + + +@pytest.fixture() +@given(parsers.parse("py file with name: {name}")) +def current_py_file_builder(name): + return PyFileBuilder(name) + + +@given(parsers.parse("with imports: {modules}")) +def add_imports_in_builder(modules, current_py_file_builder): + modules_names = [module.strip() for module in modules.split(",")] + current_py_file_builder.add_imports(*modules_names) + + +@given(parsers.re("with func:(?:\n)(?P[\\S|\\s]*)")) +def add_func_in_builder(content, current_py_file_builder): + current_py_file_builder.add_func(content) + + +@given("with passed steps") +def add_passed_steps(current_py_file_builder): + + passed_steps = '@pytest_bdd.given("passed step")\n' \ + '@pytest_bdd.when("passed step")\n' \ + '@pytest_bdd.then("passed step")\n' \ + 'def step_impl():\n' \ + ' pass' + + current_py_file_builder.add_func(passed_steps) + + +@given(parsers.parse("test for {scenario_name} from {feature_file}")) +def add_scenario_step(scenario_name, feature_file, current_py_file_builder): + + scenario_func = f'@pytest_bdd.scenario("{feature_file}", "{scenario_name}")\n' \ + 'def test_scenario():\n' \ + ' pass' + + current_py_file_builder.add_func(scenario_func) + + +@given(parsers.parse("py file saved")) +def save_py_file(current_py_file_builder, testdir): + testdir.makefile( + ".py", + **dict([(current_py_file_builder.name, current_py_file_builder.get_content())])) From 9534cf2d2ca67c3531d85dec4cc8599d77e31150 Mon Sep 17 00:00:00 2001 From: Denis Bautin Date: Tue, 12 Jan 2021 16:59:24 +0500 Subject: [PATCH 17/18] Add steps for testing attachments --- .../test/attachments_tests/conftest.py | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 allure-pytest-bdd/test/attachments_tests/conftest.py diff --git a/allure-pytest-bdd/test/attachments_tests/conftest.py b/allure-pytest-bdd/test/attachments_tests/conftest.py new file mode 100644 index 00000000..8ca8a1fa --- /dev/null +++ b/allure-pytest-bdd/test/attachments_tests/conftest.py @@ -0,0 +1,45 @@ +import os.path + +from pytest_bdd import then, parsers +import pytest + + +@then(parsers.re("this attachment with content:(?:\n)(?P[\\S|\\s]*)")) +def check_attachment_content(expected_content, suitable_attachment, testdir): + file_path = os.path.join(testdir.tmpdir.strpath, suitable_attachment["source"]) + with open(file_path, "r") as file: + actual_content = file.read() + + assert actual_content == expected_content + + +@pytest.fixture() +@then(parsers.parse("attachment {attachment_name} must be in {location_name}")) +def suitable_attachment(attachment_name, location_name, allure_report): + test_case_report = allure_report.test_cases[0] + + if location_name == "attachments": + attachments = test_case_report["attachments"] + else: + attachments = _get_step_report(test_case_report, location_name)["attachments"] + + suitable_attachments = [attachment for attachment in attachments + if attachment["name"] == attachment_name] + + assert len(suitable_attachments) == 1 + return suitable_attachments[0] + + +@then(parsers.parse("attachments must not be in {location_name}")) +def attachments_must_no_be_in(location_name, allure_report): + test_case_report = allure_report.test_cases[0] + + if location_name == "attachments": + assert "attachments" not in test_case_report.keys() + else: + assert "attachments" not in _get_step_report(test_case_report, location_name).keys() + + +def _get_step_report(test_case_report, step_name): + return next(step for step in test_case_report["steps"] + if step["name"] == step_name) From b8d37e641c11c331af7f83438f33f8823516338a Mon Sep 17 00:00:00 2001 From: Denis Bautin Date: Tue, 12 Jan 2021 17:02:05 +0500 Subject: [PATCH 18/18] Change test on bug #474 --- .../Bug474Tests/bug474.feature | 61 ++++++++----------- .../Bug474Tests/test_bug474.py | 13 +--- 2 files changed, 27 insertions(+), 47 deletions(-) diff --git a/allure-pytest-bdd/test/attachments_tests/Bug474Tests/bug474.feature b/allure-pytest-bdd/test/attachments_tests/Bug474Tests/bug474.feature index 6be1a5a3..a981cd5a 100644 --- a/allure-pytest-bdd/test/attachments_tests/Bug474Tests/bug474.feature +++ b/allure-pytest-bdd/test/attachments_tests/Bug474Tests/bug474.feature @@ -1,46 +1,37 @@ Feature: Bug #474 - Scenario: allure.attach calling in method decorated with When and Pytest.fixture + Scenario: allure.attach calling in function decorated with When and Pytest.fixture Given example.feature with content: """ Feature: Feature Test Scenario: My Scenario Test - Given two numbers: 2, 3 - When addition it - Then must be sum of it + Given passed step + When when-step is fixture with attachment + Then passed step using fixture """ - And example_test.py with content: + And py file with name: example_test + And with imports: pytest, pytest_bdd, allure + And with passed steps + And with func: """ - from pytest_bdd import scenario, given, when, then, parsers - import pytest - import allure - - a = 0 - b = 0 - - - @given(parsers.parse("two numbers: {first}, {second}")) - def step_impl(first, second): - global a, b - a = first - b = second - - @pytest.fixture() - @when("addition it") - def addition(): - allure.attach('A text attachment in module scope fixture', 'blah blah blah', allure.attachment_type.TEXT) - sum_ = a+b - return sum_ - - - @then("must be sum of it") - def check_sum(addition): - assert a+b == addition - - - @scenario("example.feature", "My Scenario Test") - def test_my_scenario(): + @pytest_bdd.when("when-step is fixture with attachment") + def step_with_attachment(): + allure.attach('Attachment content', 'allure attachment', allure.attachment_type.TEXT) + """ + And with func: + """ + @pytest_bdd.then("passed step using fixture") + def then_step(step_with_attachment): pass """ + And test for My Scenario Test from example.feature + And py file saved + When run pytest-bdd with allure - Then attachment must be only in when-step attachments \ No newline at end of file + + Then attachment allure attachment must be in When when-step is fixture with attachment + And this attachment with content: + """ + Attachment content + """ + And attachments must not be in attachments \ No newline at end of file diff --git a/allure-pytest-bdd/test/attachments_tests/Bug474Tests/test_bug474.py b/allure-pytest-bdd/test/attachments_tests/Bug474Tests/test_bug474.py index b3b70dac..2236962f 100644 --- a/allure-pytest-bdd/test/attachments_tests/Bug474Tests/test_bug474.py +++ b/allure-pytest-bdd/test/attachments_tests/Bug474Tests/test_bug474.py @@ -1,17 +1,6 @@ from pytest_bdd import scenario, then -@scenario("bug474.feature", "allure.attach calling in method decorated with When and Pytest.fixture") +@scenario("bug474.feature", "allure.attach calling in function decorated with When and Pytest.fixture") def test_my_scenario(): pass - - -@then("attachment must be only in when-step attachments") -def attachment_only_in_when(allure_report): - test_case_report = allure_report.test_cases[0] - when_step_report = next(step for step in test_case_report["steps"] - if step["name"].startswith("When")) - - assert "attachments" not in test_case_report.keys() - assert len(when_step_report["attachments"]) == 1 - assert when_step_report["attachments"][0]["name"] == "blah blah blah"