From ebddac517b5414f70ed3b7b636cdcb84a620dc21 Mon Sep 17 00:00:00 2001 From: Maksim Stepanov <17935127+delatrie@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:10:52 +0400 Subject: [PATCH 1/2] feat: add preconditions and expected results api --- allure-behave/src/listener.py | 24 + allure-pytest-bdd/src/allure_api_listener.py | 44 ++ allure-pytest-bdd/src/plugin.py | 12 +- allure-pytest-bdd/src/pytest_bdd_listener.py | 8 + allure-pytest-bdd/src/utils.py | 20 + allure-pytest/src/helper.py | 27 +- allure-pytest/src/listener.py | 32 ++ allure-pytest/src/plugin.py | 14 +- allure-pytest/src/utils.py | 20 + allure-python-commons-test/src/result.py | 16 + allure-python-commons/src/allure/__init__.py | 8 + .../src/allure_commons/_allure.py | 34 ++ .../src/allure_commons/_hooks.py | 32 ++ .../src/allure_commons/model2.py | 4 + .../src/listener/allure_listener.py | 24 + .../allure_api/documentation_test.py | 380 ++++++++++++++++ .../acceptance/documentation_test.py | 391 ++++++++++++++++ .../acceptance/documentation_test.py | 422 ++++++++++++++++++ .../allure_api/documentation_test.py | 122 +++++ 19 files changed, 1629 insertions(+), 5 deletions(-) create mode 100644 tests/allure_behave/acceptance/allure_api/documentation_test.py create mode 100644 tests/allure_pytest/acceptance/documentation_test.py create mode 100644 tests/allure_pytest_bdd/acceptance/documentation_test.py create mode 100644 tests/allure_robotframework/acceptance/allure_api/documentation_test.py diff --git a/allure-behave/src/listener.py b/allure-behave/src/listener.py index a39d0e91..322a23c6 100644 --- a/allure-behave/src/listener.py +++ b/allure-behave/src/listener.py @@ -202,6 +202,30 @@ def add_description_html(self, test_description_html): if test_result: test_result.descriptionHtml = test_description_html + @allure_commons.hookimpl + def add_preconditions(self, test_preconditions): + test_result = self.logger.get_test(None) + if test_result: + test_result.precondition = test_preconditions + + @allure_commons.hookimpl + def add_preconditions_html(self, test_preconditions_html): + test_result = self.logger.get_test(None) + if test_result: + test_result.preconditionHtml = test_preconditions_html + + @allure_commons.hookimpl + def add_expected_results(self, test_expected_results): + test_result = self.logger.get_test(None) + if test_result: + test_result.expectedResult = test_expected_results + + @allure_commons.hookimpl + def add_expected_results_html(self, test_expected_results_html): + test_result = self.logger.get_test(None) + if test_result: + test_result.expectedResultHtml = test_expected_results_html + @allure_commons.hookimpl def add_link(self, url, link_type, name): test_result = self.logger.get_test(None) diff --git a/allure-pytest-bdd/src/allure_api_listener.py b/allure-pytest-bdd/src/allure_api_listener.py index 0043a5c2..a38d28c8 100644 --- a/allure-pytest-bdd/src/allure_api_listener.py +++ b/allure-pytest-bdd/src/allure_api_listener.py @@ -9,6 +9,10 @@ from .utils import ALLURE_DESCRIPTION_HTML_MARK from .utils import ALLURE_DESCRIPTION_MARK +from .utils import ALLURE_PRECONDITIONS_MARK +from .utils import ALLURE_PRECONDITIONS_HTML_MARK +from .utils import ALLURE_EXPECTED_RESULTS_MARK +from .utils import ALLURE_EXPECTED_RESULTS_HTML_MARK from .utils import ALLURE_LABEL_MARK from .utils import ALLURE_LINK_MARK from .utils import ALLURE_TITLE_ATTR @@ -63,6 +67,26 @@ def add_description_html(self, test_description_html): with self.lifecycle.update_test_case() as test_result: test_result.descriptionHtml = test_description_html + @allure_commons.hookimpl + def decorate_as_preconditions(self, test_preconditions): + allure_preconditions = getattr(pytest.mark, ALLURE_PRECONDITIONS_MARK) + return allure_preconditions(test_preconditions) + + @allure_commons.hookimpl + def decorate_as_preconditions_html(self, test_preconditions_html): + allure_preconditions_html = getattr(pytest.mark, ALLURE_PRECONDITIONS_HTML_MARK) + return allure_preconditions_html(test_preconditions_html) + + @allure_commons.hookimpl + def decorate_as_expected_results(self, test_expected_results): + allure_expected_results = getattr(pytest.mark, ALLURE_EXPECTED_RESULTS_MARK) + return allure_expected_results(test_expected_results) + + @allure_commons.hookimpl + def decorate_as_expected_results_html(self, test_expected_results_html): + allure_expected_results_html = getattr(pytest.mark, ALLURE_EXPECTED_RESULTS_HTML_MARK) + return allure_expected_results_html(test_expected_results_html) + @allure_commons.hookimpl def decorate_as_label(self, label_type, labels): allure_label_mark = getattr(pytest.mark, ALLURE_LABEL_MARK) @@ -81,6 +105,26 @@ def decorate_as_link(self, url, link_type, name): allure_link_mark = getattr(pytest.mark, ALLURE_LINK_MARK) return allure_link_mark(url, name=name, link_type=link_type) + @allure_commons.hookimpl + def add_preconditions(self, test_preconditions): + with self.lifecycle.update_test_case() as test_result: + test_result.precondition = test_preconditions + + @allure_commons.hookimpl + def add_preconditions_html(self, test_preconditions_html): + with self.lifecycle.update_test_case() as test_result: + test_result.preconditionHtml = test_preconditions_html + + @allure_commons.hookimpl + def add_expected_results(self, test_expected_results): + with self.lifecycle.update_test_case() as test_result: + test_result.expectedResult = test_expected_results + + @allure_commons.hookimpl + def add_expected_results_html(self, test_expected_results_html): + with self.lifecycle.update_test_case() as test_result: + test_result.expectedResultHtml = test_expected_results_html + @allure_commons.hookimpl def add_link(self, url, link_type, name): url = apply_link_pattern(self.__link_patterns, link_type, url) diff --git a/allure-pytest-bdd/src/plugin.py b/allure-pytest-bdd/src/plugin.py index 0579c66e..378a9177 100644 --- a/allure-pytest-bdd/src/plugin.py +++ b/allure-pytest-bdd/src/plugin.py @@ -10,6 +10,10 @@ from .utils import ALLURE_DESCRIPTION_MARK from .utils import ALLURE_DESCRIPTION_HTML_MARK +from .utils import ALLURE_PRECONDITIONS_MARK +from .utils import ALLURE_PRECONDITIONS_HTML_MARK +from .utils import ALLURE_EXPECTED_RESULTS_MARK +from .utils import ALLURE_EXPECTED_RESULTS_HTML_MARK from .utils import ALLURE_LABEL_MARK from .utils import ALLURE_LINK_MARK @@ -57,10 +61,14 @@ def clean_up(): def register_marks(config): - config.addinivalue_line("markers", f"{ALLURE_DESCRIPTION_MARK}: allure description") - config.addinivalue_line("markers", f"{ALLURE_DESCRIPTION_HTML_MARK}: allure description in HTML") config.addinivalue_line("markers", f"{ALLURE_LABEL_MARK}: allure label marker") config.addinivalue_line("markers", f"{ALLURE_LINK_MARK}: allure link marker") + config.addinivalue_line("markers", f"{ALLURE_DESCRIPTION_MARK}: allure description") + config.addinivalue_line("markers", f"{ALLURE_DESCRIPTION_HTML_MARK}: allure description in HTML") + config.addinivalue_line("markers", f"{ALLURE_PRECONDITIONS_MARK}: allure preconditions") + config.addinivalue_line("markers", f"{ALLURE_PRECONDITIONS_HTML_MARK}: allure preconditions html") + config.addinivalue_line("markers", f"{ALLURE_EXPECTED_RESULTS_MARK}: allure expected results") + config.addinivalue_line("markers", f"{ALLURE_EXPECTED_RESULTS_HTML_MARK}: allure expected results html") def pytest_configure(config): diff --git a/allure-pytest-bdd/src/pytest_bdd_listener.py b/allure-pytest-bdd/src/pytest_bdd_listener.py index 5a96731d..6d406129 100644 --- a/allure-pytest-bdd/src/pytest_bdd_listener.py +++ b/allure-pytest-bdd/src/pytest_bdd_listener.py @@ -20,6 +20,10 @@ from .storage import save_excinfo from .storage import save_test_data from .utils import attach_data +from .utils import get_allure_preconditions +from .utils import get_allure_preconditions_html +from .utils import get_allure_expected_results +from .utils import get_allure_expected_results_html from .utils import get_allure_description from .utils import get_allure_description_html from .utils import get_allure_labels @@ -67,6 +71,10 @@ def pytest_bdd_before_scenario(self, request, feature, scenario): test_result.name = get_test_name(item, scenario, params) test_result.description = get_allure_description(item, feature, scenario) test_result.descriptionHtml = get_allure_description_html(item) + test_result.precondition = get_allure_preconditions(item) + test_result.preconditionHtml = get_allure_preconditions_html(item) + test_result.expectedResult = get_allure_expected_results(item) + test_result.expectedResultHtml = get_allure_expected_results_html(item) test_result.start = now() test_result.testCaseId = md5(full_name) test_result.labels.append(Label(name=LabelType.HOST, value=self.host)) diff --git a/allure-pytest-bdd/src/utils.py b/allure-pytest-bdd/src/utils.py index 02ed0b86..a44630fe 100644 --- a/allure-pytest-bdd/src/utils.py +++ b/allure-pytest-bdd/src/utils.py @@ -27,6 +27,10 @@ ALLURE_TITLE_ATTR = "__allure_display_name__" ALLURE_DESCRIPTION_MARK = "allure_description" ALLURE_DESCRIPTION_HTML_MARK = "allure_description_html" +ALLURE_PRECONDITIONS_MARK = "allure_preconditions" +ALLURE_PRECONDITIONS_HTML_MARK = "allure_preconditions_html" +ALLURE_EXPECTED_RESULTS_MARK = "allure_expected_results" +ALLURE_EXPECTED_RESULTS_HTML_MARK = "allure_expected_results_html" ALLURE_LABEL_MARK = "allure_label" ALLURE_LINK_MARK = "allure_link" @@ -71,6 +75,22 @@ def get_allure_description_html(item): return get_marker_value(item, ALLURE_DESCRIPTION_HTML_MARK) +def get_allure_preconditions(item): + return get_marker_value(item, ALLURE_PRECONDITIONS_MARK) + + +def get_allure_preconditions_html(item): + return get_marker_value(item, ALLURE_PRECONDITIONS_HTML_MARK) + + +def get_allure_expected_results(item): + return get_marker_value(item, ALLURE_EXPECTED_RESULTS_MARK) + + +def get_allure_expected_results_html(item): + return get_marker_value(item, ALLURE_EXPECTED_RESULTS_HTML_MARK) + + def iter_all_labels(item): for mark in item.iter_markers(name=ALLURE_LABEL_MARK): name = mark.kwargs.get("label_type") diff --git a/allure-pytest/src/helper.py b/allure-pytest/src/helper.py index a9df4b7e..5c8116fb 100644 --- a/allure-pytest/src/helper.py +++ b/allure-pytest/src/helper.py @@ -1,6 +1,11 @@ import pytest import allure_commons -from allure_pytest.utils import ALLURE_DESCRIPTION_MARK, ALLURE_DESCRIPTION_HTML_MARK +from allure_pytest.utils import ALLURE_DESCRIPTION_MARK +from allure_pytest.utils import ALLURE_DESCRIPTION_HTML_MARK +from allure_pytest.utils import ALLURE_PRECONDITIONS_MARK +from allure_pytest.utils import ALLURE_PRECONDITIONS_HTML_MARK +from allure_pytest.utils import ALLURE_EXPECTED_RESULTS_MARK +from allure_pytest.utils import ALLURE_EXPECTED_RESULTS_HTML_MARK from allure_pytest.utils import ALLURE_LABEL_MARK, ALLURE_LINK_MARK from allure_pytest.utils import format_allure_link @@ -36,6 +41,26 @@ def decorate_as_description_html(self, test_description_html): allure_description_html = getattr(pytest.mark, ALLURE_DESCRIPTION_HTML_MARK) return allure_description_html(test_description_html) + @allure_commons.hookimpl + def decorate_as_preconditions(self, test_preconditions): + allure_preconditions = getattr(pytest.mark, ALLURE_PRECONDITIONS_MARK) + return allure_preconditions(test_preconditions) + + @allure_commons.hookimpl + def decorate_as_preconditions_html(self, test_preconditions_html): + allure_preconditions_html = getattr(pytest.mark, ALLURE_PRECONDITIONS_HTML_MARK) + return allure_preconditions_html(test_preconditions_html) + + @allure_commons.hookimpl + def decorate_as_expected_results(self, test_expected_results): + allure_expected_results = getattr(pytest.mark, ALLURE_EXPECTED_RESULTS_MARK) + return allure_expected_results(test_expected_results) + + @allure_commons.hookimpl + def decorate_as_expected_results_html(self, test_expected_results_html): + allure_expected_results_html = getattr(pytest.mark, ALLURE_EXPECTED_RESULTS_HTML_MARK) + return allure_expected_results_html(test_expected_results_html) + @allure_commons.hookimpl def decorate_as_label(self, label_type, labels): allure_label = getattr(pytest.mark, ALLURE_LABEL_MARK) diff --git a/allure-pytest/src/listener.py b/allure-pytest/src/listener.py index 0685b61a..6ab235db 100644 --- a/allure-pytest/src/listener.py +++ b/allure-pytest/src/listener.py @@ -16,6 +16,10 @@ from allure_commons.model2 import Label, Link from allure_commons.model2 import Status from allure_commons.types import LabelType, AttachmentType, ParameterMode +from allure_pytest.utils import allure_preconditions +from allure_pytest.utils import allure_preconditions_html +from allure_pytest.utils import allure_expected_results +from allure_pytest.utils import allure_expected_results_html from allure_pytest.utils import allure_description, allure_description_html from allure_pytest.utils import allure_labels, allure_links, pytest_markers from allure_pytest.utils import allure_full_name, allure_package, allure_name @@ -114,6 +118,10 @@ def pytest_runtest_setup(self, item): test_result.testCaseId = md5(full_name) test_result.description = allure_description(item) test_result.descriptionHtml = allure_description_html(item) + test_result.precondition = allure_preconditions(item) + test_result.preconditionHtml = allure_preconditions_html(item) + test_result.expectedResult = allure_expected_results(item) + test_result.expectedResultHtml = allure_expected_results_html(item) current_param_names = [param.name for param in test_result.parameters] test_result.parameters.extend([ Parameter(name=name, value=represent(value)) @@ -299,6 +307,30 @@ def add_description_html(self, test_description_html): if test_result: test_result.descriptionHtml = test_description_html + @allure_commons.hookimpl + def add_preconditions(self, test_preconditions): + test_result = self.allure_logger.get_test(None) + if test_result: + test_result.precondition = test_preconditions + + @allure_commons.hookimpl + def add_preconditions_html(self, test_preconditions_html): + test_result = self.allure_logger.get_test(None) + if test_result: + test_result.preconditionHtml = test_preconditions_html + + @allure_commons.hookimpl + def add_expected_results(self, test_expected_results): + test_result = self.allure_logger.get_test(None) + if test_result: + test_result.expectedResult = test_expected_results + + @allure_commons.hookimpl + def add_expected_results_html(self, test_expected_results_html): + test_result = self.allure_logger.get_test(None) + if test_result: + test_result.expectedResultHtml = test_expected_results_html + @allure_commons.hookimpl def add_link(self, url, link_type, name): test_result = self.allure_logger.get_test(None) diff --git a/allure-pytest/src/plugin.py b/allure-pytest/src/plugin.py index 9efc358b..1eedfada 100644 --- a/allure-pytest/src/plugin.py +++ b/allure-pytest/src/plugin.py @@ -12,8 +12,14 @@ from allure_pytest.helper import AllureTestHelper, AllureTitleHelper from allure_pytest.listener import AllureListener -from allure_pytest.utils import ALLURE_DESCRIPTION_MARK, ALLURE_DESCRIPTION_HTML_MARK -from allure_pytest.utils import ALLURE_LABEL_MARK, ALLURE_LINK_MARK +from allure_pytest.utils import ALLURE_DESCRIPTION_MARK +from allure_pytest.utils import ALLURE_DESCRIPTION_HTML_MARK +from allure_pytest.utils import ALLURE_PRECONDITIONS_MARK +from allure_pytest.utils import ALLURE_PRECONDITIONS_HTML_MARK +from allure_pytest.utils import ALLURE_EXPECTED_RESULTS_MARK +from allure_pytest.utils import ALLURE_EXPECTED_RESULTS_HTML_MARK +from allure_pytest.utils import ALLURE_LABEL_MARK +from allure_pytest.utils import ALLURE_LINK_MARK def pytest_addoption(parser): @@ -172,6 +178,10 @@ def pytest_configure(config): config.addinivalue_line("markers", f"{ALLURE_LINK_MARK}: allure link marker") config.addinivalue_line("markers", f"{ALLURE_DESCRIPTION_MARK}: allure description") config.addinivalue_line("markers", f"{ALLURE_DESCRIPTION_HTML_MARK}: allure description html") + config.addinivalue_line("markers", f"{ALLURE_PRECONDITIONS_MARK}: allure preconditions") + config.addinivalue_line("markers", f"{ALLURE_PRECONDITIONS_HTML_MARK}: allure preconditions html") + config.addinivalue_line("markers", f"{ALLURE_EXPECTED_RESULTS_MARK}: allure expected results") + config.addinivalue_line("markers", f"{ALLURE_EXPECTED_RESULTS_HTML_MARK}: allure expected results html") def select_by_labels(items, config): diff --git a/allure-pytest/src/utils.py b/allure-pytest/src/utils.py index 31ffb63f..18b6b7cb 100644 --- a/allure-pytest/src/utils.py +++ b/allure-pytest/src/utils.py @@ -9,6 +9,10 @@ ALLURE_DESCRIPTION_MARK = "allure_description" ALLURE_DESCRIPTION_HTML_MARK = "allure_description_html" +ALLURE_PRECONDITIONS_MARK = "allure_preconditions" +ALLURE_PRECONDITIONS_HTML_MARK = "allure_preconditions_html" +ALLURE_EXPECTED_RESULTS_MARK = "allure_expected_results" +ALLURE_EXPECTED_RESULTS_HTML_MARK = "allure_expected_results_html" ALLURE_LABEL_MARK = "allure_label" ALLURE_LINK_MARK = "allure_link" ALLURE_UNIQUE_LABELS = [ @@ -73,6 +77,22 @@ def allure_description_html(item): return get_marker_value(item, ALLURE_DESCRIPTION_HTML_MARK) +def allure_preconditions(item): + return get_marker_value(item, ALLURE_PRECONDITIONS_MARK) + + +def allure_preconditions_html(item): + return get_marker_value(item, ALLURE_PRECONDITIONS_HTML_MARK) + + +def allure_expected_results(item): + return get_marker_value(item, ALLURE_EXPECTED_RESULTS_MARK) + + +def allure_expected_results_html(item): + return get_marker_value(item, ALLURE_EXPECTED_RESULTS_HTML_MARK) + + def allure_label(item, label): labels = [] for mark in item.iter_markers(name=ALLURE_LABEL_MARK): diff --git a/allure-python-commons-test/src/result.py b/allure-python-commons-test/src/result.py index 7012234f..b55a338f 100644 --- a/allure-python-commons-test/src/result.py +++ b/allure-python-commons-test/src/result.py @@ -89,6 +89,22 @@ def has_description_html(*matchers): return has_entry("descriptionHtml", all_of(*matchers)) +def has_preconditions(*matchers): + return has_entry("precondition", all_of(*matchers)) + + +def has_preconditions_html(*matchers): + return has_entry("preconditionHtml", all_of(*matchers)) + + +def has_expected_results(*matchers): + return has_entry("expectedResult", all_of(*matchers)) + + +def has_expected_results_html(*matchers): + return has_entry("expectedResultHtml", all_of(*matchers)) + + def has_step(name, *matchers): return has_entry( "steps", diff --git a/allure-python-commons/src/allure/__init__.py b/allure-python-commons/src/allure/__init__.py index 86feb62f..4dc151b5 100644 --- a/allure-python-commons/src/allure/__init__.py +++ b/allure-python-commons/src/allure/__init__.py @@ -1,5 +1,9 @@ from allure_commons._allure import title from allure_commons._allure import description, description_html +from allure_commons._allure import preconditions +from allure_commons._allure import preconditions_html +from allure_commons._allure import expected_results +from allure_commons._allure import expected_results_html from allure_commons._allure import label from allure_commons._allure import severity from allure_commons._allure import tag @@ -23,6 +27,10 @@ "title", "description", "description_html", + "preconditions", + "preconditions_html", + "expected_results", + "expected_results_html", "label", "severity", "suite", diff --git a/allure-python-commons/src/allure_commons/_allure.py b/allure-python-commons/src/allure_commons/_allure.py index 731786dc..11ec8e53 100644 --- a/allure-python-commons/src/allure_commons/_allure.py +++ b/allure-python-commons/src/allure_commons/_allure.py @@ -31,6 +31,24 @@ def description_html(test_description_html): return safely(plugin_manager.hook.decorate_as_description_html(test_description_html=test_description_html)) +def preconditions(test_preconditions): + return safely(plugin_manager.hook.decorate_as_preconditions(test_preconditions=test_preconditions)) + + +def preconditions_html(test_preconditions_html): + return safely(plugin_manager.hook.decorate_as_preconditions_html(test_preconditions_html=test_preconditions_html)) + + +def expected_results(test_expected_results): + return safely(plugin_manager.hook.decorate_as_expected_results(test_expected_results=test_expected_results)) + + +def expected_results_html(test_expected_results_html): + return safely(plugin_manager.hook.decorate_as_expected_results_html( + test_expected_results_html=test_expected_results_html, + )) + + def label(label_type, *labels): return safely(plugin_manager.hook.decorate_as_label(label_type=label_type, labels=labels)) @@ -101,6 +119,22 @@ def description(test_description): def description_html(test_description_html): plugin_manager.hook.add_description_html(test_description_html=test_description_html) + @staticmethod + def preconditions(test_preconditions): + plugin_manager.hook.add_preconditions(test_preconditions=test_preconditions) + + @staticmethod + def preconditions_html(test_preconditions_html): + plugin_manager.hook.add_preconditions_html(test_preconditions_html=test_preconditions_html) + + @staticmethod + def expected_results(test_expected_results): + plugin_manager.hook.add_expected_results(test_expected_results=test_expected_results) + + @staticmethod + def expected_results_html(test_expected_results_html): + plugin_manager.hook.add_expected_results_html(test_expected_results_html=test_expected_results_html) + @staticmethod def label(label_type, *labels): plugin_manager.hook.add_label(label_type=label_type, labels=labels) diff --git a/allure-python-commons/src/allure_commons/_hooks.py b/allure-python-commons/src/allure_commons/_hooks.py index 0ef73b8e..6aae0bb4 100644 --- a/allure-python-commons/src/allure_commons/_hooks.py +++ b/allure-python-commons/src/allure_commons/_hooks.py @@ -30,6 +30,38 @@ def decorate_as_description_html(self, test_description_html): def add_description_html(self, test_description_html): """ description html""" + @hookspec + def decorate_as_preconditions(self, test_preconditions): + """ preconditions """ + + @hookspec + def add_preconditions(self, test_preconditions): + """ preconditions """ + + @hookspec + def decorate_as_preconditions_html(self, test_preconditions_html): + """ preconditions html """ + + @hookspec + def add_preconditions_html(self, test_preconditions_html): + """ preconditions html """ + + @hookspec + def decorate_as_expected_results(self, test_expected_results): + """ expected results """ + + @hookspec + def add_expected_results(self, test_expected_results): + """ expected results """ + + @hookspec + def decorate_as_expected_results_html(self, test_expected_results_html): + """ expected results html """ + + @hookspec + def add_expected_results_html(self, test_expected_results_html): + """ expected results html """ + @hookspec def decorate_as_label(self, label_type, labels): """ label """ diff --git a/allure-python-commons/src/allure_commons/model2.py b/allure-python-commons/src/allure_commons/model2.py index d2a307cc..9a7fa72b 100644 --- a/allure-python-commons/src/allure_commons/model2.py +++ b/allure-python-commons/src/allure_commons/model2.py @@ -52,6 +52,10 @@ class TestResult(ExecutableItem): labels = attrib(default=Factory(list)) links = attrib(default=Factory(list)) titlePath = attrib(default=Factory(list)) + precondition = attrib(default=None) + preconditionHtml = attrib(default=None) + expectedResult = attrib(default=None) + expectedResultHtml = attrib(default=None) @attrs diff --git a/allure-robotframework/src/listener/allure_listener.py b/allure-robotframework/src/listener/allure_listener.py index eb55e5f7..7219baca 100644 --- a/allure-robotframework/src/listener/allure_listener.py +++ b/allure-robotframework/src/listener/allure_listener.py @@ -224,6 +224,30 @@ def add_label(self, label_type, labels): for label in labels if case else (): case.labels.append(Label(label_type, label)) + @allure_commons.hookimpl + def add_preconditions(self, test_preconditions): + with self.lifecycle.update_test_case() as test_result: + if test_result: + test_result.precondition = test_preconditions + + @allure_commons.hookimpl + def add_preconditions_html(self, test_preconditions_html): + with self.lifecycle.update_test_case() as test_result: + if test_result: + test_result.preconditionHtml = test_preconditions_html + + @allure_commons.hookimpl + def add_expected_results(self, test_expected_results): + with self.lifecycle.update_test_case() as test_result: + if test_result: + test_result.expectedResult = test_expected_results + + @allure_commons.hookimpl + def add_expected_results_html(self, test_expected_results_html): + with self.lifecycle.update_test_case() as test_result: + if test_result: + test_result.expectedResultHtml = test_expected_results_html + @allure_commons.hookimpl def add_link(self, url, link_type, name): with self.lifecycle.update_test_case() as case: diff --git a/tests/allure_behave/acceptance/allure_api/documentation_test.py b/tests/allure_behave/acceptance/allure_api/documentation_test.py new file mode 100644 index 00000000..dbd50ca1 --- /dev/null +++ b/tests/allure_behave/acceptance/allure_api/documentation_test.py @@ -0,0 +1,380 @@ +from textwrap import dedent + +from hamcrest import assert_that, equal_to + +from allure_commons_test.report import has_test_case +from allure_commons_test.result import ( + has_preconditions, has_preconditions_html, has_expected_results, has_expected_results_html, with_status, +) + + +def test_dynamic_preconditions_from_step(behave_runner): + results = behave_runner.run_behave( + feature_literals=[""" + Feature: Documentation + Scenario: Documentation + Given documentation + """], + step_literals=[dedent(""" + import allure + from behave import given + + @given("documentation") + def given_documentation(context): + allure.dynamic.preconditions("Preconditions") + """)], + ) + + assert_that( + results, + has_test_case( + "Documentation", + with_status("passed"), + has_preconditions(equal_to("Preconditions")), + ), + ) + + +def test_dynamic_preconditions_from_before_scenario(behave_runner): + results = behave_runner.run_behave( + feature_literals=[""" + Feature: Documentation + Scenario: Documentation + Given documentation + """], + step_literals=[dedent(""" + import allure + from behave import given + + @given("documentation") + def given_documentation(context): + pass + """)], + environment_literal=dedent(""" + import allure + + def before_scenario(context, scenario): + allure.dynamic.preconditions("Preconditions") + """), + ) + + assert_that( + results, + has_test_case( + "Documentation", + with_status("passed"), + has_preconditions(equal_to("Preconditions")), + ), + ) + + +def test_dynamic_preconditions_from_after_scenario(behave_runner): + results = behave_runner.run_behave( + feature_literals=[""" + Feature: Documentation + Scenario: Documentation + Given documentation + """], + step_literals=[dedent(""" + import allure + from behave import given + + @given("documentation") + def given_documentation(context): + pass + """)], + environment_literal=dedent(""" + import allure + + def after_scenario(context, scenario): + allure.dynamic.preconditions("Preconditions") + """), + ) + + assert_that( + results, + has_test_case( + "Documentation", + with_status("passed"), + has_preconditions(equal_to("Preconditions")), + ), + ) + + +def test_dynamic_preconditions_html_from_step(behave_runner): + results = behave_runner.run_behave( + feature_literals=[""" + Feature: Documentation + Scenario: Documentation + Given documentation + """], + step_literals=[dedent(""" + import allure + from behave import given + + @given("documentation") + def given_documentation(context): + allure.dynamic.preconditions_html("
Preconditions
") + """)], + ) + + assert_that( + results, + has_test_case( + "Documentation", + with_status("passed"), + has_preconditions_html(equal_to("Preconditions
")), + ), + ) + + +def test_dynamic_preconditions_html_from_before_scenario(behave_runner): + results = behave_runner.run_behave( + feature_literals=[""" + Feature: Documentation + Scenario: Documentation + Given documentation + """], + step_literals=[dedent(""" + import allure + from behave import given + + @given("documentation") + def given_documentation(context): + pass + """)], + environment_literal=dedent(""" + import allure + + def before_scenario(context, scenario): + allure.dynamic.preconditions_html("Preconditions
") + """), + ) + + assert_that( + results, + has_test_case( + "Documentation", + with_status("passed"), + has_preconditions_html(equal_to("Preconditions
")), + ), + ) + + +def test_dynamic_preconditions_html_from_after_scenario(behave_runner): + results = behave_runner.run_behave( + feature_literals=[""" + Feature: Documentation + Scenario: Documentation + Given documentation + """], + step_literals=[dedent(""" + import allure + from behave import given + + @given("documentation") + def given_documentation(context): + pass + """)], + environment_literal=dedent(""" + import allure + + def after_scenario(context, scenario): + allure.dynamic.preconditions_html("Preconditions
") + """), + ) + + assert_that( + results, + has_test_case( + "Documentation", + with_status("passed"), + has_preconditions_html(equal_to("Preconditions
")), + ), + ) + + +def test_dynamic_expected_results_from_step(behave_runner): + results = behave_runner.run_behave( + feature_literals=[""" + Feature: Documentation + Scenario: Documentation + Given documentation + """], + step_literals=[dedent(""" + import allure + from behave import given + + @given("documentation") + def given_documentation(context): + allure.dynamic.expected_results("Expected results") + """)], + ) + + assert_that( + results, + has_test_case( + "Documentation", + with_status("passed"), + has_expected_results(equal_to("Expected results")), + ), + ) + + +def test_dynamic_expected_results_from_before_scenario(behave_runner): + results = behave_runner.run_behave( + feature_literals=[""" + Feature: Documentation + Scenario: Documentation + Given documentation + """], + step_literals=[dedent(""" + import allure + from behave import given + + @given("documentation") + def given_documentation(context): + pass + """)], + environment_literal=dedent(""" + import allure + + def before_scenario(context, scenario): + allure.dynamic.expected_results("Expected results") + """), + ) + + assert_that( + results, + has_test_case( + "Documentation", + with_status("passed"), + has_expected_results(equal_to("Expected results")), + ), + ) + + +def test_dynamic_expected_results_from_after_scenario(behave_runner): + results = behave_runner.run_behave( + feature_literals=[""" + Feature: Documentation + Scenario: Documentation + Given documentation + """], + step_literals=[dedent(""" + import allure + from behave import given + + @given("documentation") + def given_documentation(context): + pass + """)], + environment_literal=dedent(""" + import allure + + def after_scenario(context, scenario): + allure.dynamic.expected_results("Expected results") + """), + ) + + assert_that( + results, + has_test_case( + "Documentation", + with_status("passed"), + has_expected_results(equal_to("Expected results")), + ), + ) + + +def test_dynamic_expected_results_html_from_step(behave_runner): + results = behave_runner.run_behave( + feature_literals=[""" + Feature: Documentation + Scenario: Documentation + Given documentation + """], + step_literals=[dedent(""" + import allure + from behave import given + + @given("documentation") + def given_documentation(context): + allure.dynamic.expected_results_html("Expected results
") + """)], + ) + + assert_that( + results, + has_test_case( + "Documentation", + with_status("passed"), + has_expected_results_html(equal_to("Expected results
")), + ), + ) + + +def test_dynamic_expected_results_html_from_before_scenario(behave_runner): + results = behave_runner.run_behave( + feature_literals=[""" + Feature: Documentation + Scenario: Documentation + Given documentation + """], + step_literals=[dedent(""" + import allure + from behave import given + + @given("documentation") + def given_documentation(context): + pass + """)], + environment_literal=dedent(""" + import allure + + def before_scenario(context, scenario): + allure.dynamic.expected_results_html("Expected results
") + """), + ) + + assert_that( + results, + has_test_case( + "Documentation", + with_status("passed"), + has_expected_results_html(equal_to("Expected results
")), + ), + ) + + +def test_dynamic_expected_results_html_from_after_scenario(behave_runner): + results = behave_runner.run_behave( + feature_literals=[""" + Feature: Documentation + Scenario: Documentation + Given documentation + """], + step_literals=[dedent(""" + import allure + from behave import given + + @given("documentation") + def given_documentation(context): + pass + """)], + environment_literal=dedent(""" + import allure + + def after_scenario(context, scenario): + allure.dynamic.expected_results_html("Expected results
") + """), + ) + + assert_that( + results, + has_test_case( + "Documentation", + with_status("passed"), + has_expected_results_html(equal_to("Expected results
")), + ), + ) diff --git a/tests/allure_pytest/acceptance/documentation_test.py b/tests/allure_pytest/acceptance/documentation_test.py new file mode 100644 index 00000000..fb139ee9 --- /dev/null +++ b/tests/allure_pytest/acceptance/documentation_test.py @@ -0,0 +1,391 @@ +from hamcrest import assert_that, equal_to + +from allure_commons_test.report import has_test_case +from allure_commons_test.result import ( + has_preconditions, has_preconditions_html, has_expected_results, has_expected_results_html, with_status, +) + + +def test_preconditions_decorator_at_module_level(allure_pytest_runner): + results = allure_pytest_runner.run_pytest(""" + import allure + + pytestmark = [allure.preconditions("Preconditions")] + + class TestDocumentation: + def test_documentation(self): + pass + """, cli_args=["--strict-markers"]) + + assert_that( + results, + has_test_case( + "test_documentation", + with_status("passed"), + has_preconditions(equal_to("Preconditions")), + ), + ) + + +def test_preconditions_decorator_at_class_level(allure_pytest_runner): + results = allure_pytest_runner.run_pytest(""" + import allure + + pytestmark = [allure.preconditions("Module Preconditions")] + + @allure.preconditions("Preconditions") + class TestDocumentation: + def test_documentation(self): + pass + """, cli_args=["--strict-markers"]) + + assert_that( + results, + has_test_case( + "test_documentation", + with_status("passed"), + has_preconditions(equal_to("Preconditions")), + ), + ) + + +def test_preconditions_decorator(allure_pytest_runner): + results = allure_pytest_runner.run_pytest(""" + import allure + + pytestmark = [allure.preconditions("Module Preconditions")] + + @allure.preconditions("Class Preconditions") + class TestDocumentation: + @allure.preconditions("Preconditions") + def test_documentation(self): + pass + """, cli_args=["--strict-markers"]) + + assert_that( + results, + has_test_case( + "test_documentation", + with_status("passed"), + has_preconditions(equal_to("Preconditions")), + ), + ) + + +def test_dynamic_preconditions_taking_precedence(allure_pytest_runner): + results = allure_pytest_runner.run_pytest(""" + import allure + + pytestmark = [allure.preconditions("Module Preconditions")] + + @allure.preconditions("Class Preconditions") + class TestDocumentation: + @allure.preconditions("Function Preconditions") + def test_documentation(self): + allure.dynamic.preconditions("Preconditions") + """, cli_args=["--strict-markers"]) + + assert_that( + results, + has_test_case( + "test_documentation", + with_status("passed"), + has_preconditions(equal_to("Preconditions")), + ), + ) + + +def test_preconditions_html_decorator_at_module_level(allure_pytest_runner): + results = allure_pytest_runner.run_pytest(""" + import allure + + pytestmark = [allure.preconditions_html("Preconditions
")] + + class TestDocumentation: + def test_documentation(self): + pass + """, cli_args=["--strict-markers"]) + + assert_that( + results, + has_test_case( + "test_documentation", + with_status("passed"), + has_preconditions_html(equal_to("Preconditions
")), + ), + ) + + +def test_preconditions_html_decorator_at_class_level(allure_pytest_runner): + results = allure_pytest_runner.run_pytest(""" + import allure + + pytestmark = [allure.preconditions_html("Module Preconditions
")] + + @allure.preconditions_html("Preconditions
") + class TestDocumentation: + def test_documentation(self): + pass + """, cli_args=["--strict-markers"]) + + assert_that( + results, + has_test_case( + "test_documentation", + with_status("passed"), + has_preconditions_html(equal_to("Preconditions
")), + ), + ) + + +def test_preconditions_html_decorator(allure_pytest_runner): + results = allure_pytest_runner.run_pytest(""" + import allure + + pytestmark = [allure.preconditions_html("Module Preconditions
")] + + @allure.preconditions_html("Class Preconditions
") + class TestDocumentation: + @allure.preconditions_html("Preconditions
") + def test_documentation(self): + pass + """, cli_args=["--strict-markers"]) + + assert_that( + results, + has_test_case( + "test_documentation", + with_status("passed"), + has_preconditions_html(equal_to("Preconditions
")), + ), + ) + + +def test_dynamic_preconditions_html_taking_precedence(allure_pytest_runner): + results = allure_pytest_runner.run_pytest(""" + import allure + + pytestmark = [allure.preconditions_html("Module Preconditions
")] + + @allure.preconditions_html("Class Preconditions
") + class TestDocumentation: + @allure.preconditions_html("Function Preconditions
") + def test_documentation(self): + allure.dynamic.preconditions_html("Preconditions
") + """, cli_args=["--strict-markers"]) + + assert_that( + results, + has_test_case( + "test_documentation", + with_status("passed"), + has_preconditions_html(equal_to("Preconditions
")), + ), + ) + + +def test_expected_results_decorator_at_module_level(allure_pytest_runner): + results = allure_pytest_runner.run_pytest(""" + import allure + + pytestmark = [allure.expected_results("Expected results")] + + class TestDocumentation: + def test_documentation(self): + pass + """, cli_args=["--strict-markers"]) + + assert_that( + results, + has_test_case( + "test_documentation", + with_status("passed"), + has_expected_results(equal_to("Expected results")), + ), + ) + + +def test_expected_results_decorator_at_class_level(allure_pytest_runner): + results = allure_pytest_runner.run_pytest(""" + import allure + + pytestmark = [allure.expected_results("Module expected results")] + + @allure.expected_results("Expected results") + class TestDocumentation: + def test_documentation(self): + pass + """, cli_args=["--strict-markers"]) + + assert_that( + results, + has_test_case( + "test_documentation", + with_status("passed"), + has_expected_results(equal_to("Expected results")), + ), + ) + + +def test_expected_results_decorator(allure_pytest_runner): + results = allure_pytest_runner.run_pytest(""" + import allure + + pytestmark = [allure.expected_results("Module expected results")] + + @allure.expected_results("Class expected results") + class TestDocumentation: + @allure.expected_results("Expected results") + def test_documentation(self): + pass + """, cli_args=["--strict-markers"]) + + assert_that( + results, + has_test_case( + "test_documentation", + with_status("passed"), + has_expected_results(equal_to("Expected results")), + ), + ) + + +def test_dynamic_expected_results_taking_precedence(allure_pytest_runner): + results = allure_pytest_runner.run_pytest(""" + import allure + + pytestmark = [allure.expected_results("Module expected results")] + + @allure.expected_results("Class expected results") + class TestDocumentation: + @allure.expected_results("Function expected results") + def test_documentation(self): + allure.dynamic.expected_results("Expected results") + """, cli_args=["--strict-markers"]) + + assert_that( + results, + has_test_case( + "test_documentation", + with_status("passed"), + has_expected_results(equal_to("Expected results")), + ), + ) + + +def test_expected_results_html_decorator_at_module_level(allure_pytest_runner): + results = allure_pytest_runner.run_pytest(""" + import allure + + pytestmark = [allure.expected_results_html("Expected results
")] + + class TestDocumentation: + def test_documentation(self): + pass + """, cli_args=["--strict-markers"]) + + assert_that( + results, + has_test_case( + "test_documentation", + with_status("passed"), + has_expected_results_html(equal_to("Expected results
")), + ), + ) + + +def test_expected_results_html_decorator_at_class_level(allure_pytest_runner): + results = allure_pytest_runner.run_pytest(""" + import allure + + pytestmark = [allure.expected_results_html("Module expected results
")] + + @allure.expected_results_html("Expected results
") + class TestDocumentation: + def test_documentation(self): + pass + """, cli_args=["--strict-markers"]) + + assert_that( + results, + has_test_case( + "test_documentation", + with_status("passed"), + has_expected_results_html(equal_to("Expected results
")), + ), + ) + + +def test_expected_results_html_decorator(allure_pytest_runner): + results = allure_pytest_runner.run_pytest(""" + import allure + + pytestmark = [allure.expected_results_html("Module expected results
")] + + @allure.expected_results_html("Class expected results
") + class TestDocumentation: + @allure.expected_results_html("Expected results
") + def test_documentation(self): + pass + """, cli_args=["--strict-markers"]) + + assert_that( + results, + has_test_case( + "test_documentation", + with_status("passed"), + has_expected_results_html(equal_to("Expected results
")), + ), + ) + + +def test_dynamic_expected_results_html_taking_precedence(allure_pytest_runner): + results = allure_pytest_runner.run_pytest(""" + import allure + + pytestmark = [allure.expected_results_html("Module expected results
")] + + @allure.expected_results_html("Class expected results
") + class TestDocumentation: + @allure.expected_results_html("Function expected results
") + def test_documentation(self): + allure.dynamic.expected_results_html("Expected results
") + """, cli_args=["--strict-markers"]) + + assert_that( + results, + has_test_case( + "test_documentation", + with_status("passed"), + has_expected_results_html(equal_to("Expected results
")), + ), + ) + + +def test_documentation_fields_are_independent(allure_pytest_runner): + results = allure_pytest_runner.run_pytest(""" + import allure + + @allure.description("Description") + @allure.description_html("Description
") + @allure.preconditions("Preconditions") + @allure.preconditions_html("Preconditions
") + @allure.expected_results("Expected results") + @allure.expected_results_html("Expected results
") + def test_documentation(): + allure.dynamic.preconditions("Updated preconditions") + allure.dynamic.preconditions("Final preconditions") + + def test_without_documentation(): + pass + """) + + documented, undocumented = results.test_cases + assert documented["description"] == "Description" + assert documented["descriptionHtml"] == "Description
" + assert documented["precondition"] == "Final preconditions" + assert documented["preconditionHtml"] == "Preconditions
" + assert documented["expectedResult"] == "Expected results" + assert documented["expectedResultHtml"] == "Expected results
" + for field in ("precondition", "preconditionHtml", "expectedResult", "expectedResultHtml"): + assert field not in undocumented diff --git a/tests/allure_pytest_bdd/acceptance/documentation_test.py b/tests/allure_pytest_bdd/acceptance/documentation_test.py new file mode 100644 index 00000000..59906c1b --- /dev/null +++ b/tests/allure_pytest_bdd/acceptance/documentation_test.py @@ -0,0 +1,422 @@ +from hamcrest import assert_that, equal_to + +from allure_commons_test.report import has_test_case +from allure_commons_test.result import ( + has_preconditions, has_preconditions_html, has_expected_results, has_expected_results_html, with_status, +) + + +def test_preconditions_decorator_at_module_level(allure_pytest_bdd_runner): + results = allure_pytest_bdd_runner.run_pytest( + ("sample.feature", """ + Feature: Documentation + Scenario: Documentation + Given noop + """), + """ + import allure + from pytest_bdd import scenario, given + + pytestmark = [allure.preconditions("Preconditions")] + + @scenario("sample.feature", "Documentation") + def test_documentation(): + pass + + @given("noop") + def given_noop(): + pass + """, + cli_args=["--strict-markers"], + ) + + assert_that( + results, + has_test_case( + "sample.feature:Documentation", + with_status("passed"), + has_preconditions(equal_to("Preconditions")), + ), + ) + + +def test_preconditions_decorator(allure_pytest_bdd_runner): + results = allure_pytest_bdd_runner.run_pytest( + ("sample.feature", """ + Feature: Documentation + Scenario: Documentation + Given noop + """), + """ + import allure + from pytest_bdd import scenario, given + + pytestmark = [allure.preconditions("Module Preconditions")] + + @allure.preconditions("Preconditions") + @scenario("sample.feature", "Documentation") + def test_documentation(): + pass + + @given("noop") + def given_noop(): + pass + """, + cli_args=["--strict-markers"], + ) + + assert_that( + results, + has_test_case( + "sample.feature:Documentation", + with_status("passed"), + has_preconditions(equal_to("Preconditions")), + ), + ) + + +def test_dynamic_preconditions(allure_pytest_bdd_runner): + results = allure_pytest_bdd_runner.run_pytest( + ("sample.feature", """ + Feature: Documentation + Scenario: Documentation + Given noop + """), + """ + import allure + from pytest_bdd import scenario, given + + pytestmark = [allure.preconditions("Module Preconditions")] + + @allure.preconditions("Function Preconditions") + @scenario("sample.feature", "Documentation") + def test_documentation(): + allure.dynamic.preconditions("Preconditions") + + @given("noop") + def given_noop(): + pass + """, + cli_args=["--strict-markers"], + ) + + assert_that( + results, + has_test_case( + "sample.feature:Documentation", + with_status("passed"), + has_preconditions(equal_to("Preconditions")), + ), + ) + + +def test_preconditions_html_decorator_at_module_level(allure_pytest_bdd_runner): + results = allure_pytest_bdd_runner.run_pytest( + ("sample.feature", """ + Feature: Documentation + Scenario: Documentation + Given noop + """), + """ + import allure + from pytest_bdd import scenario, given + + pytestmark = [allure.preconditions_html("Preconditions
")] + + @scenario("sample.feature", "Documentation") + def test_documentation(): + pass + + @given("noop") + def given_noop(): + pass + """, + cli_args=["--strict-markers"], + ) + + assert_that( + results, + has_test_case( + "sample.feature:Documentation", + with_status("passed"), + has_preconditions_html(equal_to("Preconditions
")), + ), + ) + + +def test_preconditions_html_decorator(allure_pytest_bdd_runner): + results = allure_pytest_bdd_runner.run_pytest( + ("sample.feature", """ + Feature: Documentation + Scenario: Documentation + Given noop + """), + """ + import allure + from pytest_bdd import scenario, given + + pytestmark = [allure.preconditions_html("Module Preconditions
")] + + @allure.preconditions_html("Preconditions
") + @scenario("sample.feature", "Documentation") + def test_documentation(): + pass + + @given("noop") + def given_noop(): + pass + """, + cli_args=["--strict-markers"], + ) + + assert_that( + results, + has_test_case( + "sample.feature:Documentation", + with_status("passed"), + has_preconditions_html(equal_to("Preconditions
")), + ), + ) + + +def test_dynamic_preconditions_html(allure_pytest_bdd_runner): + results = allure_pytest_bdd_runner.run_pytest( + ("sample.feature", """ + Feature: Documentation + Scenario: Documentation + Given noop + """), + """ + import allure + from pytest_bdd import scenario, given + + pytestmark = [allure.preconditions_html("Module Preconditions
")] + + @allure.preconditions_html("Function Preconditions
") + @scenario("sample.feature", "Documentation") + def test_documentation(): + allure.dynamic.preconditions_html("Preconditions
") + + @given("noop") + def given_noop(): + pass + """, + cli_args=["--strict-markers"], + ) + + assert_that( + results, + has_test_case( + "sample.feature:Documentation", + with_status("passed"), + has_preconditions_html(equal_to("Preconditions
")), + ), + ) + + +def test_expected_results_decorator_at_module_level(allure_pytest_bdd_runner): + results = allure_pytest_bdd_runner.run_pytest( + ("sample.feature", """ + Feature: Documentation + Scenario: Documentation + Given noop + """), + """ + import allure + from pytest_bdd import scenario, given + + pytestmark = [allure.expected_results("Expected results")] + + @scenario("sample.feature", "Documentation") + def test_documentation(): + pass + + @given("noop") + def given_noop(): + pass + """, + cli_args=["--strict-markers"], + ) + + assert_that( + results, + has_test_case( + "sample.feature:Documentation", + with_status("passed"), + has_expected_results(equal_to("Expected results")), + ), + ) + + +def test_expected_results_decorator(allure_pytest_bdd_runner): + results = allure_pytest_bdd_runner.run_pytest( + ("sample.feature", """ + Feature: Documentation + Scenario: Documentation + Given noop + """), + """ + import allure + from pytest_bdd import scenario, given + + pytestmark = [allure.expected_results("Module Expected results")] + + @allure.expected_results("Expected results") + @scenario("sample.feature", "Documentation") + def test_documentation(): + pass + + @given("noop") + def given_noop(): + pass + """, + cli_args=["--strict-markers"], + ) + + assert_that( + results, + has_test_case( + "sample.feature:Documentation", + with_status("passed"), + has_expected_results(equal_to("Expected results")), + ), + ) + + +def test_dynamic_expected_results(allure_pytest_bdd_runner): + results = allure_pytest_bdd_runner.run_pytest( + ("sample.feature", """ + Feature: Documentation + Scenario: Documentation + Given noop + """), + """ + import allure + from pytest_bdd import scenario, given + + pytestmark = [allure.expected_results("Module Expected results")] + + @allure.expected_results("Function Expected results") + @scenario("sample.feature", "Documentation") + def test_documentation(): + allure.dynamic.expected_results("Expected results") + + @given("noop") + def given_noop(): + pass + """, + cli_args=["--strict-markers"], + ) + + assert_that( + results, + has_test_case( + "sample.feature:Documentation", + with_status("passed"), + has_expected_results(equal_to("Expected results")), + ), + ) + + +def test_expected_results_html_decorator_at_module_level(allure_pytest_bdd_runner): + results = allure_pytest_bdd_runner.run_pytest( + ("sample.feature", """ + Feature: Documentation + Scenario: Documentation + Given noop + """), + """ + import allure + from pytest_bdd import scenario, given + + pytestmark = [allure.expected_results_html("Expected results
")] + + @scenario("sample.feature", "Documentation") + def test_documentation(): + pass + + @given("noop") + def given_noop(): + pass + """, + cli_args=["--strict-markers"], + ) + + assert_that( + results, + has_test_case( + "sample.feature:Documentation", + with_status("passed"), + has_expected_results_html(equal_to("Expected results
")), + ), + ) + + +def test_expected_results_html_decorator(allure_pytest_bdd_runner): + results = allure_pytest_bdd_runner.run_pytest( + ("sample.feature", """ + Feature: Documentation + Scenario: Documentation + Given noop + """), + """ + import allure + from pytest_bdd import scenario, given + + pytestmark = [allure.expected_results_html("Module Expected results
")] + + @allure.expected_results_html("Expected results
") + @scenario("sample.feature", "Documentation") + def test_documentation(): + pass + + @given("noop") + def given_noop(): + pass + """, + cli_args=["--strict-markers"], + ) + + assert_that( + results, + has_test_case( + "sample.feature:Documentation", + with_status("passed"), + has_expected_results_html(equal_to("Expected results
")), + ), + ) + + +def test_dynamic_expected_results_html(allure_pytest_bdd_runner): + results = allure_pytest_bdd_runner.run_pytest( + ("sample.feature", """ + Feature: Documentation + Scenario: Documentation + Given noop + """), + """ + import allure + from pytest_bdd import scenario, given + + pytestmark = [allure.expected_results_html("Module Expected results
")] + + @allure.expected_results_html("Function Expected results
") + @scenario("sample.feature", "Documentation") + def test_documentation(): + allure.dynamic.expected_results_html("Expected results
") + + @given("noop") + def given_noop(): + pass + """, + cli_args=["--strict-markers"], + ) + + assert_that( + results, + has_test_case( + "sample.feature:Documentation", + with_status("passed"), + has_expected_results_html(equal_to("Expected results
")), + ), + ) diff --git a/tests/allure_robotframework/acceptance/allure_api/documentation_test.py b/tests/allure_robotframework/acceptance/allure_api/documentation_test.py new file mode 100644 index 00000000..11791672 --- /dev/null +++ b/tests/allure_robotframework/acceptance/allure_api/documentation_test.py @@ -0,0 +1,122 @@ +from hamcrest import assert_that, equal_to + +from allure_commons_test.report import has_test_case +from allure_commons_test.result import ( + has_preconditions, has_preconditions_html, has_expected_results, has_expected_results_html, with_status, +) + + +def test_dynamic_preconditions(robot_runner): + results = robot_runner.run_robotframework( + suite_literals={"documentation.robot": """ + *** Settings *** + Library documentation.py + + *** Test Cases *** + Documentation + Set Documentation + """}, + library_literals={"documentation.py": """ + import allure + + def set_documentation(): + allure.dynamic.preconditions("Previous Preconditions") + allure.dynamic.preconditions("Preconditions") + """}, + ) + + assert_that( + results, + has_test_case( + "Documentation.Documentation", + with_status("passed"), + has_preconditions(equal_to("Preconditions")), + ), + ) + + +def test_dynamic_preconditions_html(robot_runner): + results = robot_runner.run_robotframework( + suite_literals={"documentation.robot": """ + *** Settings *** + Library documentation.py + + *** Test Cases *** + Documentation + Set Documentation + """}, + library_literals={"documentation.py": """ + import allure + + def set_documentation(): + allure.dynamic.preconditions_html("Previous Preconditions
") + allure.dynamic.preconditions_html("Preconditions
") + """}, + ) + + assert_that( + results, + has_test_case( + "Documentation.Documentation", + with_status("passed"), + has_preconditions_html(equal_to("Preconditions
")), + ), + ) + + +def test_dynamic_expected_results(robot_runner): + results = robot_runner.run_robotframework( + suite_literals={"documentation.robot": """ + *** Settings *** + Library documentation.py + + *** Test Cases *** + Documentation + Set Documentation + """}, + library_literals={"documentation.py": """ + import allure + + def set_documentation(): + allure.dynamic.expected_results("Previous Expected results") + allure.dynamic.expected_results("Expected results") + """}, + ) + + assert_that( + results, + has_test_case( + "Documentation.Documentation", + with_status("passed"), + has_expected_results(equal_to("Expected results")), + ), + ) + + +def test_dynamic_expected_results_html(robot_runner): + results = robot_runner.run_robotframework( + suite_literals={"documentation.robot": """ + *** Settings *** + Library documentation.py + + *** Test Cases *** + Documentation + Set Documentation + """}, + library_literals={"documentation.py": """ + import allure + + def set_documentation(): + allure.dynamic.expected_results_html("Previous Expected results
") + allure.dynamic.expected_results_html("Expected results
") + """}, + ) + + assert_that( + results, + has_test_case( + "Documentation.Documentation", + with_status("passed"), + has_expected_results_html(equal_to("Expected results
")), + ), + ) From 3c55227f2fdd74a786bbc34e95a88bbc912104af Mon Sep 17 00:00:00 2001 From: Maksim Stepanov <17935127+delatrie@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:45:29 +0400 Subject: [PATCH 2/2] feat(robot): enable documentation api in robot files --- .../examples/description.rst | 24 ++++- allure-robotframework/src/library/__init__.py | 6 +- .../src/library/allure_library.py | 25 ++++- .../allure_api/documentation_test.py | 92 +++++++++++++++++++ 4 files changed, 144 insertions(+), 3 deletions(-) diff --git a/allure-robotframework/examples/description.rst b/allure-robotframework/examples/description.rst index 6a7270e1..21d26b16 100644 --- a/allure-robotframework/examples/description.rst +++ b/allure-robotframework/examples/description.rst @@ -40,4 +40,26 @@ The documentation can also be set dynamically with the Set Test Documentation This documentation will appear as allure description. -.. _Set Test Documentation keyword: https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set%20Test%20Documentation \ No newline at end of file +.. _Set Test Documentation keyword: https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set%20Test%20Documentation + +---------------------------------- +Preconditions and expected results +---------------------------------- + +Import :code:`AllureLibrary` to set preconditions and expected results directly +from Robot Framework tests. Use :code:`Preconditions` and :code:`Expected Results` +for plain text, or :code:`Preconditions Html` and :code:`Expected Results Html` +for HTML. Each keyword replaces the previous value for its corresponding field. + +.. code:: robotframework + :name: preconditions-expected-results-robot + + *** Settings *** + Library AllureLibrary + + *** Test Cases *** + Preconditions and expected results + Preconditions Preconditions + Preconditions HtmlPreconditions
+ Expected Results Expected results + Expected Results HtmlExpected results
diff --git a/allure-robotframework/src/library/__init__.py b/allure-robotframework/src/library/__init__.py index d21bce6b..c43c8d00 100644 --- a/allure-robotframework/src/library/__init__.py +++ b/allure-robotframework/src/library/__init__.py @@ -1,3 +1,7 @@ from .allure_library import attach, attach_file, global_attach, global_attach_file, global_error +from .allure_library import preconditions, preconditions_html, expected_results, expected_results_html -__all__ = ["attach", "attach_file", "global_attach", "global_attach_file", "global_error"] +__all__ = [ + "attach", "attach_file", "global_attach", "global_attach_file", "global_error", + "preconditions", "preconditions_html", "expected_results", "expected_results_html", +] diff --git a/allure-robotframework/src/library/allure_library.py b/allure-robotframework/src/library/allure_library.py index bb731341..b1fff688 100644 --- a/allure-robotframework/src/library/allure_library.py +++ b/allure-robotframework/src/library/allure_library.py @@ -1,7 +1,10 @@ import allure -__all__ = ["attach", "attach_file", "global_attach", "global_attach_file", "global_error"] +__all__ = [ + "attach", "attach_file", "global_attach", "global_attach_file", "global_error", + "preconditions", "preconditions_html", "expected_results", "expected_results_html", +] def _attachment_type(name): @@ -29,3 +32,23 @@ def global_attach_file(source, name=None, attachment_type=None, extension=None): def global_error(message, trace=None, *, status = allure.error_status.BROKEN): allure.global_error(message, trace=trace, status=status) + + +def preconditions(text): + """Set the test preconditions as plain text.""" + allure.dynamic.preconditions(text) + + +def preconditions_html(text): + """Set the test preconditions as HTML.""" + allure.dynamic.preconditions_html(text) + + +def expected_results(text): + """Set the expected test results as plain text.""" + allure.dynamic.expected_results(text) + + +def expected_results_html(text): + """Set the expected test results as HTML.""" + allure.dynamic.expected_results_html(text) diff --git a/tests/allure_robotframework/acceptance/allure_api/documentation_test.py b/tests/allure_robotframework/acceptance/allure_api/documentation_test.py index 11791672..b328fa2c 100644 --- a/tests/allure_robotframework/acceptance/allure_api/documentation_test.py +++ b/tests/allure_robotframework/acceptance/allure_api/documentation_test.py @@ -120,3 +120,95 @@ def set_documentation(): has_expected_results_html(equal_to("Expected results
")), ), ) + + +def test_preconditions_keyword(robot_runner): + results = robot_runner.run_robotframework( + suite_literals={"documentation.robot": """ + *** Settings *** + Library AllureLibrary + + *** Test Cases *** + Preconditions + Preconditions Previous Preconditions + Preconditions Preconditions + """}, + ) + + assert_that( + results, + has_test_case( + "Documentation.Preconditions", + with_status("passed"), + has_preconditions(equal_to("Preconditions")), + ), + ) + + +def test_preconditions_html_keyword(robot_runner): + results = robot_runner.run_robotframework( + suite_literals={"documentation.robot": """ + *** Settings *** + Library AllureLibrary + + *** Test Cases *** + Preconditions HTML + Preconditions HtmlPrevious Preconditions
+ Preconditions HtmlPreconditions
+ """}, + ) + + assert_that( + results, + has_test_case( + "Documentation.Preconditions HTML", + with_status("passed"), + has_preconditions_html(equal_to("Preconditions
")), + ), + ) + + +def test_expected_results_keyword(robot_runner): + results = robot_runner.run_robotframework( + suite_literals={"documentation.robot": """ + *** Settings *** + Library AllureLibrary + + *** Test Cases *** + Expected results + Expected Results Previous Expected results + Expected Results Expected results + """}, + ) + + assert_that( + results, + has_test_case( + "Documentation.Expected results", + with_status("passed"), + has_expected_results(equal_to("Expected results")), + ), + ) + + +def test_expected_results_html_keyword(robot_runner): + results = robot_runner.run_robotframework( + suite_literals={"documentation.robot": """ + *** Settings *** + Library AllureLibrary + + *** Test Cases *** + Expected results HTML + Expected Results HtmlPrevious Expected results
+ Expected Results HtmlExpected results
+ """}, + ) + + assert_that( + results, + has_test_case( + "Documentation.Expected results HTML", + with_status("passed"), + has_expected_results_html(equal_to("Expected results
")), + ), + )