-
Notifications
You must be signed in to change notification settings - Fork 255
Add support allure decorators (not all) for allure-pytest-bdd (fixes #726) #818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
fantom0005
wants to merge
18
commits into
allure-framework:master
from
fantom0005:add-base-allure-tags-to-report
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
8c8fc24
Added support of base allure tags to allure-pytest-bdd.
fantom0005 994938c
Add an id label to pytest bdd tests
fantom0005 8af1be3
Commit the missed file
fantom0005 2386ff8
Added the test for tags in allure-pytest-bdd
fantom0005 88b79da
Fix import
fantom0005 22eb421
Added support description and allure tags
fantom0005 6365baf
Fix import
fantom0005 8eea1d0
Fix import
fantom0005 f95c772
remove allure.title tags because it does not work
fantom0005 298b685
cleaning
fantom0005 8f01c93
Added links support
fantom0005 4f993be
fix test
fantom0005 1d67636
Added tests
fantom0005 0d1783a
Unify the test
fantom0005 031b3da
Unify the test
fantom0005 c54c38d
Unify the test
fantom0005 4bc7f25
Unify the test
fantom0005 cb5ed75
Fixed review issues
fantom0005 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import pytest | ||
| import allure_commons | ||
| from .utils import ALLURE_DESCRIPTION_MARK, ALLURE_DESCRIPTION_HTML_MARK | ||
| from .utils import ALLURE_LABEL_MARK, ALLURE_LINK_MARK | ||
| from .utils import format_allure_link | ||
|
|
||
|
|
||
| class AllureTestHelper: | ||
| def __init__(self, config): | ||
| self.config = config | ||
|
|
||
| @allure_commons.hookimpl | ||
| def decorate_as_description(self, test_description): | ||
| allure_description = getattr(pytest.mark, ALLURE_DESCRIPTION_MARK) | ||
| return allure_description(test_description) | ||
|
|
||
| @allure_commons.hookimpl | ||
| 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_label(self, label_type, labels): | ||
| allure_label = getattr(pytest.mark, ALLURE_LABEL_MARK) | ||
| return allure_label(*labels, label_type=label_type) | ||
|
|
||
| @allure_commons.hookimpl | ||
| def decorate_as_link(self, url, link_type, name): | ||
| url = format_allure_link(self.config, url, link_type) | ||
| allure_link = getattr(pytest.mark, ALLURE_LINK_MARK) | ||
| name = url if name is None else name | ||
| return allure_link(url, name=name, link_type=link_type) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,10 @@ | ||
| import allure_commons | ||
| import argparse | ||
| import os | ||
|
|
||
| import allure_commons | ||
| from allure_commons.logger import AllureFileLogger | ||
|
|
||
| from .helper import AllureTestHelper | ||
| from .pytest_bdd_listener import PytestBDDListener | ||
|
|
||
|
|
||
|
|
@@ -17,11 +21,32 @@ def pytest_addoption(parser): | |
| dest="clean_alluredir", | ||
| help="Clean alluredir folder if it exists") | ||
|
|
||
| parser.getgroup("general").addoption('--allure-link-pattern', | ||
| action="append", | ||
| dest="allure_link_pattern", | ||
| metavar="LINK_TYPE:LINK_PATTERN", | ||
| default=[], | ||
| type=link_pattern, | ||
| help="""Url pattern for link type. Allows short links in test, | ||
| like 'issue-1'. Text will be formatted to full url with python | ||
| str.format().""") | ||
|
|
||
|
|
||
| def link_pattern(string): | ||
| pattern = string.split(':', 1) | ||
| if not pattern[0]: | ||
| raise argparse.ArgumentTypeError('Link type is mandatory.') | ||
|
|
||
| if len(pattern) != 2: | ||
| raise argparse.ArgumentTypeError('Link pattern is mandatory') | ||
| return pattern | ||
|
|
||
|
|
||
| def cleanup_factory(plugin): | ||
| def clean_up(): | ||
| name = allure_commons.plugin_manager.get_name(plugin) | ||
| allure_commons.plugin_manager.unregister(name=name) | ||
|
|
||
| return clean_up | ||
|
|
||
|
|
||
|
|
@@ -32,6 +57,10 @@ def pytest_configure(config): | |
| if report_dir: | ||
| report_dir = os.path.abspath(report_dir) | ||
|
|
||
| test_helper = AllureTestHelper(config) | ||
| allure_commons.plugin_manager.register(test_helper) | ||
| config.add_cleanup(cleanup_factory(test_helper)) | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, register custom marks like allure-pytest does it here |
||
| pytest_bdd_listener = PytestBDDListener() | ||
| config.pluginmanager.register(pytest_bdd_listener) | ||
| allure_commons.plugin_manager.register(pytest_bdd_listener) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,26 @@ | ||
| import os | ||
| from uuid import UUID | ||
| from allure_commons.utils import md5 | ||
| from allure_commons.model2 import StatusDetails | ||
| from allure_commons.model2 import Status | ||
|
|
||
| from allure_commons.model2 import Parameter | ||
| from allure_commons.model2 import Status | ||
| from allure_commons.model2 import StatusDetails | ||
| from allure_commons.types import LabelType | ||
| from allure_commons.utils import format_exception | ||
| from allure_commons.utils import md5 | ||
| from allure_commons.utils import represent | ||
|
|
||
| ALLURE_DESCRIPTION_MARK = 'allure_description' | ||
delatrie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ALLURE_DESCRIPTION_HTML_MARK = 'allure_description_html' | ||
| ALLURE_LABEL_MARK = 'allure_label' | ||
| ALLURE_LINK_MARK = 'allure_link' | ||
| ALLURE_UNIQUE_LABELS = [ | ||
| LabelType.SEVERITY, | ||
| LabelType.FRAMEWORK, | ||
| LabelType.HOST, | ||
| LabelType.SUITE, | ||
| LabelType.PARENT_SUITE, | ||
| LabelType.SUB_SUITE | ||
| ] | ||
|
|
||
|
|
||
| def get_step_name(step): | ||
|
|
@@ -48,3 +64,66 @@ def get_params(node): | |
| outline_params = params.pop('_pytest_bdd_example', {}) | ||
| params.update(outline_params) | ||
| return [Parameter(name=name, value=value) for name, value in params.items()] | ||
|
|
||
|
|
||
| def format_allure_link(config, url, link_type): | ||
| pattern = dict(config.option.allure_link_pattern).get(link_type, '{}') | ||
| return pattern.format(url) | ||
|
|
||
|
|
||
| def allure_labels(item): | ||
| unique_labels = dict() | ||
| labels = set() | ||
| for mark in item.iter_markers(name=ALLURE_LABEL_MARK): | ||
| label_type = mark.kwargs["label_type"] | ||
| if label_type in ALLURE_UNIQUE_LABELS: | ||
| if label_type not in unique_labels.keys(): | ||
| unique_labels[label_type] = mark.args[0] | ||
| else: | ||
| for arg in mark.args: | ||
| labels.add((label_type, arg)) | ||
| for k, v in unique_labels.items(): | ||
| labels.add((k, v)) | ||
| return labels | ||
|
|
||
|
|
||
| def get_marker_value(item, keyword): | ||
| marker = item.get_closest_marker(keyword) | ||
| return marker.args[0] if marker and marker.args else None | ||
|
|
||
|
|
||
| def allure_description(item): | ||
| description = get_marker_value(item, ALLURE_DESCRIPTION_MARK) | ||
| if description: | ||
| return description | ||
| elif hasattr(item, 'function'): | ||
| return item.function.__doc__ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please add a test on converting the function's doc into the Allure description? |
||
|
|
||
|
|
||
| def mark_to_str(marker): | ||
| args = [represent(arg) for arg in marker.args] | ||
| kwargs = ['{name}={value}'.format(name=key, value=represent(marker.kwargs[key])) for key in marker.kwargs] | ||
| if marker.name in ('filterwarnings', 'skip', 'skipif', 'xfail', 'usefixtures', 'tryfirst', 'trylast'): | ||
| markstr = '@pytest.mark.{name}'.format(name=marker.name) | ||
| else: | ||
| markstr = '{name}'.format(name=marker.name) | ||
| if args or kwargs: | ||
| parameters = ', '.join(args + kwargs) | ||
| markstr = '{}({})'.format(markstr, parameters) | ||
| return markstr | ||
|
|
||
|
|
||
| def pytest_markers(item): | ||
| for keyword in item.keywords.keys(): | ||
| if any([keyword.startswith('allure_'), keyword == 'parametrize']): | ||
| continue | ||
| marker = item.get_closest_marker(keyword) | ||
| if marker is None: | ||
| continue | ||
|
|
||
| yield mark_to_str(marker) | ||
|
|
||
|
|
||
| def allure_links(item): | ||
| for mark in item.iter_markers(name=ALLURE_LINK_MARK): | ||
| yield mark.kwargs["link_type"], mark.args[0], mark.kwargs["name"] | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.