Skip to content

Commit 0fa45a6

Browse files
authored
ability to set title, description and html description for test (fixes allure-framework#145 via allure-framework#192)
1 parent a605a10 commit 0fa45a6

File tree

13 files changed

+288
-48
lines changed

13 files changed

+288
-48
lines changed

allure-pytest/src/helper.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import pytest
55
import allure_commons
6+
from allure_pytest.utils import ALLURE_TITLE
7+
from allure_pytest.utils import ALLURE_DESCRIPTION, ALLURE_DESCRIPTION_HTML
68
from allure_pytest.utils import ALLURE_LABEL_PREFIX, ALLURE_LINK_PREFIX
79

810

@@ -11,6 +13,21 @@ class AllureTestHelper(object):
1113
def __init__(self, config):
1214
self.config = config
1315

16+
@allure_commons.hookimpl
17+
def decorate_as_title(self, test_title):
18+
allure_title = getattr(pytest.mark, ALLURE_TITLE)
19+
return allure_title(test_title)
20+
21+
@allure_commons.hookimpl
22+
def decorate_as_description(self, test_description):
23+
allure_description = getattr(pytest.mark, ALLURE_DESCRIPTION)
24+
return allure_description(test_description)
25+
26+
@allure_commons.hookimpl
27+
def decorate_as_description_html(self, test_description_html):
28+
allure_description_html = getattr(pytest.mark, ALLURE_DESCRIPTION_HTML)
29+
return allure_description_html(test_description_html)
30+
1431
@allure_commons.hookimpl
1532
def decorate_as_label(self, label_type, labels):
1633
allure_label_marker = '{prefix}.{label_type}'.format(prefix=ALLURE_LABEL_PREFIX, label_type=label_type)

allure-pytest/src/listener.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from allure_commons.model2 import Label, Link
1515
from allure_commons.model2 import Status
1616
from allure_commons.types import LabelType
17+
from allure_pytest.utils import allure_description, allure_description_html
1718
from allure_pytest.utils import allure_labels, allure_links, pytest_markers
1819
from allure_pytest.utils import allure_full_name, allure_package, allure_name
1920
from allure_pytest.utils import get_status, get_status_details
@@ -65,11 +66,10 @@ def pytest_runtest_protocol(self, item, nextitem):
6566
self.allure_logger.update_group(group_uuid, children=uuid)
6667

6768
test_case = TestResult(name=allure_name(item), uuid=uuid)
68-
self.allure_logger.schedule_test(uuid, test_case)
69-
70-
if hasattr(item, 'function'):
71-
test_case.description = item.function.__doc__
69+
test_case.description = allure_description(item)
70+
test_case.descriptionHtml = allure_description_html(item)
7271

72+
self.allure_logger.schedule_test(uuid, test_case)
7373
yield
7474

7575
for name, value in item.callspec.params.items() if hasattr(item, 'callspec') else ():
@@ -192,6 +192,24 @@ def attach_data(self, body, name, attachment_type, extension):
192192
def attach_file(self, source, name, attachment_type, extension):
193193
self.allure_logger.attach_file(uuid4(), source, name=name, attachment_type=attachment_type, extension=extension)
194194

195+
@allure_commons.hookimpl
196+
def add_title(self, test_title):
197+
test_result = self.allure_logger.get_test(None)
198+
if test_result:
199+
test_result.name = test_title
200+
201+
@allure_commons.hookimpl
202+
def add_description(self, test_description):
203+
test_result = self.allure_logger.get_test(None)
204+
if test_result:
205+
test_result.description = test_description
206+
207+
@allure_commons.hookimpl
208+
def add_description_html(self, test_description_html):
209+
test_result = self.allure_logger.get_test(None)
210+
if test_result:
211+
test_result.descriptionHtml = test_description_html
212+
195213
@allure_commons.hookimpl
196214
def add_link(self, url, link_type, name):
197215
test_result = self.allure_logger.get_test(None)

allure-pytest/src/utils.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,33 @@
88
from allure_commons.model2 import Status
99
from allure_commons.model2 import StatusDetails
1010

11-
12-
ALLURE_UNIQUE_LABELS = ['severity', 'thread', 'host']
11+
ALLURE_TITLE = 'allure_title'
12+
ALLURE_DESCRIPTION = 'allure_description'
13+
ALLURE_DESCRIPTION_HTML = 'allure_description_html'
1314
ALLURE_LABEL_PREFIX = 'allure_label'
1415
ALLURE_LINK_PREFIX = 'allure_link'
16+
ALLURE_UNIQUE_LABELS = ['severity', 'thread', 'host']
17+
18+
19+
def get_marker_value(item, keyword):
20+
marker = item.keywords.get(keyword)
21+
return marker.args[0] if marker and marker.args else None
22+
23+
24+
def allure_title(item):
25+
return get_marker_value(item, ALLURE_TITLE)
26+
27+
28+
def allure_description(item):
29+
description = get_marker_value(item, ALLURE_DESCRIPTION)
30+
if description:
31+
return description
32+
elif hasattr(item, 'function'):
33+
return item.function.__doc__
34+
35+
36+
def allure_description_html(item):
37+
return get_marker_value(item, ALLURE_DESCRIPTION_HTML)
1538

1639

1740
def allure_labels(item):
@@ -38,8 +61,7 @@ def allure_links(item):
3861

3962
def pytest_markers(item):
4063
for keyword in item.keywords.keys():
41-
if not any((keyword.startswith(ALLURE_LINK_PREFIX),
42-
keyword.startswith(ALLURE_LABEL_PREFIX),
64+
if not any((keyword.startswith('allure_'),
4365
keyword == 'parametrize')):
4466
marker = item.get_marker(keyword)
4567
if marker:
@@ -63,7 +85,9 @@ def allure_package(item):
6385

6486

6587
def allure_name(item):
66-
return escape_name(item.name)
88+
name = escape_name(item.name)
89+
title = allure_title(item)
90+
return title if title else name
6791

6892

6993
def allure_full_name(item):
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import allure
4+
5+
6+
@allure.description("""
7+
This is a test description
8+
with new lines..
9+
""")
10+
def test_dynamic_description():
11+
"""
12+
>>> allure_report = getfixture('allure_report')
13+
>>> assert_that(allure_report,
14+
... has_test_case('test_dynamic_description',
15+
... has_description(
16+
... starts_with('It is a finally description')
17+
... )
18+
... )
19+
... )
20+
"""
21+
allure.dynamic.description("It is a finally description")
22+
23+
24+
@allure.description_html("""
25+
<h1>This is a test description</h1>
26+
<i>with new lines...</i>
27+
""")
28+
def test_dynamic_description_html():
29+
"""
30+
>>> allure_report = getfixture('allure_report')
31+
>>> assert_that(allure_report,
32+
... has_test_case('test_dynamic_description_html',
33+
... has_description_html(
34+
... starts_with('<p>It is a finally description html</p>')
35+
... )
36+
... )
37+
... )
38+
"""
39+
allure.dynamic.description_html("<p>It is a finally description html</p>")
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
>>> allure_report = getfixture('allure_report')
5+
"""
6+
7+
import allure
8+
9+
10+
@allure.description("""
11+
This is a test description
12+
with new lines..
13+
""")
14+
def test_description():
15+
"""
16+
>>> allure_report = getfixture('allure_report')
17+
>>> assert_that(allure_report,
18+
... has_test_case('test_description',
19+
... has_description(
20+
... starts_with('\\nThis is a test description')
21+
... )
22+
... )
23+
... )
24+
"""
25+
pass
26+
27+
28+
@allure.description_html("""
29+
<h1>This is a test description</h1>
30+
<i>with new lines...</i>
31+
""")
32+
def test_description_html():
33+
"""
34+
>>> allure_report = getfixture('allure_report')
35+
>>> assert_that(allure_report,
36+
... has_test_case('test_description_html',
37+
... has_description_html(
38+
... starts_with('\\n<h1>This is a test description</h1>')
39+
... )
40+
... )
41+
... )
42+
"""
43+
pass
44+
45+
46+
def test_docstring_description():
47+
"""
48+
>>> allure_report = getfixture('allure_report')
49+
>>> assert_that(allure_report,
50+
... has_test_case('test_docstring_description',
51+
... has_description(
52+
... starts_with('\\n >>> allure_report = ')
53+
... )
54+
... )
55+
... )
56+
"""
57+
pass
58+
59+
60+
def test_unicode_docstring_description():
61+
u"""
62+
>>> # рекурсия
63+
>>> allure_report = getfixture('allure_report')
64+
>>> assert_that(allure_report,
65+
... has_test_case('test_unicode_docstring_description',
66+
... has_description(
67+
... starts_with(u'\\n >>> # рекурсия')
68+
... )
69+
... )
70+
... )
71+
"""
72+
pass

allure-pytest/test/description/text_description_test.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

allure-pytest/test/title/__init__.py

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import allure
2+
3+
4+
@allure.title("A some test tile")
5+
def test_dynamic_title():
6+
"""
7+
>>> allure_report = getfixture('allure_report')
8+
>>> assert_that(allure_report,
9+
... has_test_case('test_dynamic_title',
10+
... has_title("It is renamed test")
11+
... )
12+
... )
13+
"""
14+
allure.dynamic.title("It is renamed test")
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# -*- coding: utf-8 -*-
2+
import allure
3+
4+
5+
@allure.title("A some test tile")
6+
def test_title():
7+
"""
8+
>>> allure_report = getfixture('allure_report')
9+
>>> assert_that(allure_report,
10+
... has_test_case('test_title',
11+
... has_title("A some test tile")
12+
... )
13+
... )
14+
"""
15+
pass
16+
17+
18+
@allure.title(u"Лунтик")
19+
def test_unicode_title():
20+
u"""
21+
>>> allure_report = getfixture('allure_report')
22+
>>> assert_that(allure_report,
23+
... has_test_case('test_unicode_title',
24+
... has_title(u"Лунтик")
25+
... )
26+
... )
27+
"""
28+
pass

allure-python-commons-test/src/result.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,19 @@
6565
from hamcrest import all_of, anything
6666
from hamcrest import equal_to, not_none
6767
from hamcrest import has_entry, has_item
68-
from hamcrest import contains_string
68+
from hamcrest import contains_string, starts_with
69+
70+
71+
def has_title(title):
72+
return has_entry('name', title)
73+
74+
75+
def has_description(*matchers):
76+
return has_entry('description', all_of(*matchers))
77+
78+
79+
def has_description_html(*matchers):
80+
return has_entry('descriptionHtml', all_of(*matchers))
6981

7082

7183
def has_step(name, *matchers):
@@ -136,6 +148,3 @@ def with_status_message(message):
136148
def has_history_id():
137149
return has_entry('historyId', anything())
138150

139-
140-
def has_description(*matchers):
141-
return has_entry('description', all_of(*matchers))

0 commit comments

Comments
 (0)