Skip to content

Commit e7a8121

Browse files
authored
[WIP] Pytest marks as tags (fixes #103 fixes #102 fixes #100 and really fixes #60) (#104)
pytest marks as tags (fixes #103 fixes #102 fixes #100 and really fixes #60 via #104)
1 parent a704f11 commit e7a8121

File tree

21 files changed

+330
-70
lines changed

21 files changed

+330
-70
lines changed

allure-pytest/src/helper.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
14
import pytest
25
import allure_commons
36
from allure_pytest.utils import ALLURE_LABEL_PREFIX, ALLURE_LINK_PREFIX
@@ -10,14 +13,14 @@ def __init__(self, config):
1013

1114
@allure_commons.hookimpl
1215
def decorate_as_label(self, label_type, labels):
13-
allure_label_marker = u'{prefix}.{label_type}'.format(prefix=ALLURE_LABEL_PREFIX, label_type=label_type)
16+
allure_label_marker = '{prefix}.{label_type}'.format(prefix=ALLURE_LABEL_PREFIX, label_type=label_type)
1417
allure_label = getattr(pytest.mark, allure_label_marker)
1518
return allure_label(*labels, label_type=label_type)
1619

1720
@allure_commons.hookimpl
1821
def decorate_as_link(self, url, link_type, name):
19-
allure_link_marker = u'{prefix}.{link_type}'.format(prefix=ALLURE_LINK_PREFIX, link_type=link_type)
22+
allure_link_marker = '{prefix}.{link_type}'.format(prefix=ALLURE_LINK_PREFIX, link_type=link_type)
2023
pattern = dict(self.config.option.allure_link_pattern).get(link_type, u'{}')
2124
url = pattern.format(url)
2225
allure_link = getattr(pytest.mark, allure_link_marker)
23-
return allure_link(url, name=name)
26+
return allure_link(url, name=name, link_type=link_type)

allure-pytest/src/listener.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
from allure_commons.model2 import Parameter
1414
from allure_commons.model2 import Label, Link
1515
from allure_commons.model2 import Status
16+
from allure_commons.types import LabelType
1617

1718
from allure_pytest.utils import allure_parameters
18-
from allure_pytest.utils import allure_labels, allure_links
19+
from allure_pytest.utils import allure_labels, allure_links, pytest_markers
1920
from allure_pytest.utils import allure_full_name, allure_package
2021

2122

@@ -70,6 +71,8 @@ def pytest_runtest_protocol(self, item, nextitem):
7071

7172
test_case.labels += [Label(name, value) for name, value in allure_labels(item)]
7273
test_case.links += [Link(link_type, url, name) for link_type, url, name in allure_links(item)]
74+
test_case.labels += [Label(LabelType.TAG, value) for value in pytest_markers(item)]
75+
7376
test_case.fullName = allure_full_name(item.nodeid)
7477
test_case.historyId = md5(test_case.fullName)
7578
test_case.labels.append(Label('package', allure_package(item.nodeid)))

allure-pytest/src/utils.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
14
import os
2-
import sys
3-
import inspect
45
from itertools import product
6+
from allure_commons.utils import represent
57

68
ALLURE_UNIQUE_LABELS = ['severity', 'thread', 'host']
79
ALLURE_LABEL_PREFIX = 'allure_label'
@@ -40,7 +42,7 @@ def allure_parameters(fixturedef, request):
4042
if len(item['args']) == 1:
4143
parameters = {'name': ids, 'value': str(request.param)}
4244
else:
43-
param_name = u'{ids}::{param}'.format(ids=ids, param=param_name)
45+
param_name = '{ids}::{param}'.format(ids=ids, param=param_name)
4446
parameters = {'name': param_name, 'value': str(request.param)}
4547

4648
return parameters
@@ -62,12 +64,32 @@ def allure_links(item):
6264
for keyword in item.keywords.keys():
6365
if keyword.startswith(ALLURE_LINK_PREFIX):
6466
marker = item.get_marker(keyword)
65-
link_type = marker.name.split('.', 1)[-1]
67+
link_type = marker.kwargs['link_type']
6668
url = marker.args[0]
6769
name = marker.kwargs['name']
6870
yield (link_type, url, name)
6971

7072

73+
def pytest_markers(item):
74+
for keyword in item.keywords.keys():
75+
if not any((keyword.startswith(ALLURE_LINK_PREFIX),
76+
keyword.startswith(ALLURE_LABEL_PREFIX),
77+
keyword == 'parametrize')):
78+
marker = item.get_marker(keyword)
79+
if marker:
80+
yield mark_to_str(marker)
81+
82+
83+
def mark_to_str(marker):
84+
args = [represent(arg) for arg in marker.args]
85+
kwargs = ['{name}={value}'.format(name=key, value=represent(marker.kwargs[key])) for key in marker.kwargs]
86+
if args or kwargs:
87+
parameters = ', '.join(args + kwargs)
88+
return '@pytest.mark.{name}({parameters})'.format(name=marker.name, parameters=parameters)
89+
else:
90+
return '@pytest.mark.{name}'.format(name=marker.name)
91+
92+
7193
def allure_package(nodeid):
7294
parts = nodeid.split('::')
7395
path = parts[0].split('.')[0]
@@ -77,18 +99,6 @@ def allure_package(nodeid):
7799
def allure_full_name(nodeid):
78100
parts = nodeid.split('::')
79101
package = allure_package(nodeid)
80-
clazz = u'.{clazz}'.format(clazz=parts[1]) if len(parts) > 2 else ''
102+
clazz = '.{clazz}'.format(clazz=parts[1]) if len(parts) > 2 else ''
81103
test = parts[-1]
82-
return u'{package}{clazz}#{test}'.format(package=package, clazz=clazz, test=test)
83-
84-
85-
def step_parameters(func, *a, **kw):
86-
if sys.version_info.major < 3:
87-
all_names = inspect.getargspec(func).args
88-
defaults = inspect.getargspec(func).defaults
89-
else:
90-
all_names = inspect.getfullargspec(func).args
91-
defaults = inspect.getfullargspec(func).defaults
92-
args_part = [(n, str(v)) for n, v in zip(all_names, a)]
93-
kwarg_part = [(n, str(kw[n]) if n in kw else str(defaults[i])) for i, n in enumerate(all_names[len(a):])]
94-
return args_part + kwarg_part
104+
return '{package}{clazz}#{test}'.format(package=package, clazz=clazz, test=test)

allure-pytest/test/history/simple_history_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ def test_history_id():
2020
... )
2121
... )
2222
"""
23-
pytest.exit("Just test")
23+
pass # pytest.exit("Just test")

allure-pytest/test/labels/bdd/__init__.py

Whitespace-only changes.

allure-pytest/test/labels/mark/__init__.py

Whitespace-only changes.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
>>> allure_report = getfixture('allure_report')
4+
>>> assert_that(allure_report,
5+
... all_of(
6+
... has_property('test_cases', has_length(3)),
7+
... has_property('test_groups', has_length(0))
8+
... )) # doctest: +SKIP
9+
"""
10+
11+
import pytest
12+
13+
14+
@pytest.mark.cool
15+
@pytest.mark.stuff
16+
def test_pytest_marker():
17+
"""
18+
>>> allure_report = getfixture('allure_report')
19+
>>> assert_that(allure_report,
20+
... has_test_case('test_pytest_marker',
21+
... has_tag('@pytest.mark.cool'),
22+
... has_tag('@pytest.mark.stuff')
23+
... )
24+
... )
25+
"""
26+
pass
27+
28+
29+
@pytest.mark.marker('cool', 'stuff')
30+
def test_pytest_marker_with_args():
31+
"""
32+
>>> allure_report = getfixture('allure_report')
33+
>>> assert_that(allure_report,
34+
... has_test_case('test_pytest_marker_with_args',
35+
... has_tag("@pytest.mark.marker('cool', 'stuff')")
36+
... )
37+
... )
38+
"""
39+
pass
40+
41+
42+
@pytest.mark.marker(stuff='cool')
43+
def test_pytest_marker_with_kwargs():
44+
"""
45+
>>> allure_report = getfixture('allure_report')
46+
>>> assert_that(allure_report,
47+
... has_test_case('test_pytest_marker_with_kwargs',
48+
... has_tag("@pytest.mark.marker(stuff='cool')")
49+
... )
50+
... )
51+
"""
52+
pass
53+
54+
55+
@pytest.mark.marker(stuff='я')
56+
def test_pytest_marker_with_kwargs_native_encoding():
57+
"""
58+
>>> from allure_commons.utils import represent
59+
>>> allure_report = getfixture('allure_report')
60+
>>> assert_that(allure_report,
61+
... has_test_case('test_pytest_marker_with_kwargs_native_encoding',
62+
... has_tag("@pytest.mark.marker(stuff=%s)" % represent('я'))
63+
... )
64+
... )
65+
"""
66+
pass
67+
68+
69+
@pytest.mark.marker(stuff=u'я')
70+
def test_pytest_marker_with_kwargs_utf_encoding():
71+
"""
72+
>>> from allure_commons.utils import represent
73+
>>> allure_report = getfixture('allure_report')
74+
>>> assert_that(allure_report,
75+
... has_test_case('test_pytest_marker_with_kwargs_utf_encoding',
76+
... has_tag("@pytest.mark.marker(stuff=%s)" % represent('я'))
77+
... )
78+
... )
79+
"""
80+
pass
81+
82+
83+
@pytest.mark.marker('that', stuff='cool')
84+
def test_pytest_marker_with_args_and_kwargs():
85+
"""
86+
>>> allure_report = getfixture('allure_report')
87+
>>> assert_that(allure_report,
88+
... has_test_case('test_pytest_marker_with_args_and_kwargs',
89+
... has_tag("@pytest.mark.marker('that', stuff='cool')")
90+
... )
91+
... )
92+
"""
93+
pass

allure-pytest/test/labels/severity/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)