Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --allure-tags command line option to filter test cases by custom tags and pytest markers #723

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions allure-pytest/examples/label/tags/select_tests_by_tags.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Selecting tests by TAG label
----------------------------

You can use following commandline options to specify different sets of tests to execute passing a list of
values as compiled expression:

--allure-tags

There are some tests marked with TAG labels:

>>> import allure

>>> @allure.tag("some feature", "smoke")
... def test_feature_smoke():
... pass

>>> @allure.tag("some feature", "load")
... def test_feature_load():
... pass


>>> @pytest.mark.some_feature
>>> @pytest.mark.e2e
... def test_feature_e2e():
... pass


If you run ``pytest`` with following options: ``$ pytest tests.py --allure-tags="some_feature and (smoke or e2e)" --alluredir=./report`` first
and last tests will be runned.
All spaces in tags in the expression should be replaced with "_".
The function only works with statically assigned tags by @allure.tag marker.
55 changes: 53 additions & 2 deletions allure-pytest/src/plugin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import attr
import argparse

import allure
Expand All @@ -15,6 +16,9 @@
from allure_pytest.utils import ALLURE_DESCRIPTION_MARK, ALLURE_DESCRIPTION_HTML_MARK
from allure_pytest.utils import ALLURE_LABEL_MARK, ALLURE_LINK_MARK

from typing import AbstractSet
from _pytest.mark import _parse_expression


def pytest_addoption(parser):
parser.getgroup("reporting").addoption('--alluredir',
Expand Down Expand Up @@ -102,6 +106,13 @@ def a_label_type(string):
help="""Comma-separated list of IDs.
Run tests that have at least one of the specified id labels.""")

parser.getgroup("general").addoption('--allure-tags',
dest="allure_tags",
metavar="TAGS_SET",
default='',
type=str,
help="An alternative for pytest -m key with support allure tags.")

def link_pattern(string):
pattern = string.split(':', 1)
if not pattern[0]:
Expand Down Expand Up @@ -202,11 +213,51 @@ def is_planed(item):
return items, []


@attr.s(slots=True, auto_attribs=True)
class AllureTagsMatcher:
own_tags: AbstractSet[str]

@classmethod
def from_item(cls, item):
mark_names = {
tag.replace(' ', '_')
for mark in item.iter_markers('allure_label')
for tag in mark.args
if mark.kwargs.get('label_type') == 'tag'
}
mark_names.update(
(
mark.name for mark in item.iter_markers()
if not mark.args and not mark.kwargs
)
)
return cls(mark_names)

def __call__(self, name: str) -> bool:
return name in self.own_tags


def select_by_tags(items, config) -> None:
if not config.option.allure_tags:
return items, []
expr = _parse_expression(config.option.allure_tags, "Wrong expression passed to '--allure-tags'")
selected, deselected = [], []
for item in items:
if expr.evaluate(AllureTagsMatcher.from_item(item)):
selected.append(item)
else:
deselected.append(item)
return selected, deselected


def pytest_collection_modifyitems(items, config):
selected, deselected_by_testcase = select_by_testcase(items, config)
selected, deselected_by_labels = select_by_labels(selected, config)
selected, deselected_by_tags = select_by_tags(selected, config)

items[:] = selected

if deselected_by_testcase or deselected_by_labels:
config.hook.pytest_deselected(items=[*deselected_by_testcase, *deselected_by_labels])
if deselected_by_testcase or deselected_by_labels or deselected_by_tags:
config.hook.pytest_deselected(
items=[*deselected_by_testcase, *deselected_by_labels, *deselected_by_tags]
)