Skip to content

Commit

Permalink
select tests by testcase id (via allure-framework#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
sseliverstov authored Sep 2, 2019
1 parent 36f133a commit 4a66122
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
21 changes: 18 additions & 3 deletions allure-pytest/src/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import allure
import allure_commons
import os
import json

from allure_commons.types import LabelType
from allure_commons.logger import AllureFileLogger

from allure_pytest.utils import allure_labels

from allure_pytest.utils import allure_label, allure_labels
from allure_pytest.helper import AllureTestHelper
from allure_pytest.listener import AllureListener

Expand Down Expand Up @@ -137,10 +139,23 @@ def pytest_configure(config):
config.addinivalue_line("markers", "{mark}: allure description html".format(mark=ALLURE_DESCRIPTION_HTML_MARK))


def pytest_collection_modifyitems(items, config):
def select_by_labels(items, config):
arg_labels = set().union(config.option.allure_epics,
config.option.allure_features,
config.option.allure_stories,
config.option.allure_severities)
return filter(lambda item: arg_labels & set(allure_labels(item)) if arg_labels else True, items)


items[:] = filter(lambda item: arg_labels & set(allure_labels(item)) if arg_labels else True, items)
def select_by_testcase(items):
file_path = os.environ.get("AS_TESTPLAN_PATH")
ids = []
if file_path:
with open(file_path, 'r') as file:
ids = set(json.load(file))
return filter(lambda item: ids & set(allure_label(item, LabelType.ID)) if ids else True, items)


def pytest_collection_modifyitems(items, config):
items[:] = select_by_testcase(items)
items[:] = select_by_labels(items, config)
9 changes: 8 additions & 1 deletion allure-pytest/src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ def allure_description_html(item):
return get_marker_value(item, ALLURE_DESCRIPTION_HTML_MARK)


def allure_label(item, label):
labels = []
for mark in item.iter_markers(name=ALLURE_LABEL_MARK):
if mark.kwargs.get("label_type") == label:
labels.extend(mark.args)
return labels


def allure_labels(item):
unique_labels = dict()
labels = set()
Expand All @@ -60,7 +68,6 @@ def allure_labels(item):
labels.add((label_type, arg))
for k, v in unique_labels.items():
labels.add((k, v))

return labels


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pytest
import json
import os
from hamcrest import assert_that, only_contains, any_of, ends_with


@pytest.mark.parametrize(
["ids", "expected_tests"],
[
(
["1", "2"],
["test_number_one", "test_number_two"]
),
(
["1", "3", "3+"],
["test_number_one", "test_number_three"]
),
(
None,
["test_number_one", "test_number_two", "test_number_three", "test_without_number"]
),
]
)
def test_select_by_testcase_id_test(ids, expected_tests, allured_testdir):
"""
>>> import allure
>>> @allure.id("1")
... def test_number_one():
... pass
>>> @allure.id("2")
... def test_number_two():
... pass
>>> @allure.id("3")
... @allure.id("3+")
... def test_number_three():
... pass
>>> def test_without_number():
... pass
"""
allured_testdir.parse_docstring_source()

if ids:
py_path = allured_testdir.testdir.makefile(".json", json.dumps(ids))
os.environ["AS_TESTPLAN_PATH"] = py_path.strpath
else:
del os.environ["AS_TESTPLAN_PATH"]

allured_testdir.run_with_allure()
test_cases = [test_case["fullName"] for test_case in allured_testdir.allure_report.test_cases]
assert_that(test_cases, only_contains(
any_of(
*[ends_with(name) for name in expected_tests]
)
))
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from allure_commons_test.label import has_label


def test_allure_ee_id_label(executed_docstring_source):
def test_set_testcase_id_label(executed_docstring_source):
"""
>>> import allure
Expand Down

0 comments on commit 4a66122

Please sign in to comment.