Skip to content

Commit 846b5c1

Browse files
authored
unicode ids (fixes #171 via #172)
1 parent 078bb40 commit 846b5c1

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

allure-pytest/src/listener.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from allure_commons.model2 import Status
1616
from allure_commons.types import LabelType
1717
from allure_pytest.utils import allure_labels, allure_links, pytest_markers
18-
from allure_pytest.utils import allure_full_name, allure_package
18+
from allure_pytest.utils import allure_full_name, allure_package, allure_name
1919
from allure_pytest.utils import get_status, get_status_details
2020
from allure_pytest.utils import get_outcome_status, get_outcome_status_details
2121

@@ -64,7 +64,7 @@ def pytest_runtest_protocol(self, item, nextitem):
6464
self.allure_logger.start_group(group_uuid, group)
6565
self.allure_logger.update_group(group_uuid, children=uuid)
6666

67-
test_case = TestResult(name=item.name, uuid=uuid)
67+
test_case = TestResult(name=allure_name(item), uuid=uuid)
6868
self.allure_logger.schedule_test(uuid, test_case)
6969

7070
if hasattr(item, 'function'):
@@ -84,9 +84,9 @@ def pytest_runtest_protocol(self, item, nextitem):
8484

8585
test_case.links += [Link(link_type, url, name) for link_type, url, name in allure_links(item)]
8686

87-
test_case.fullName = allure_full_name(item.nodeid)
87+
test_case.fullName = allure_full_name(item)
8888
test_case.historyId = md5(test_case.fullName)
89-
test_case.labels.append(Label('package', allure_package(item.nodeid)))
89+
test_case.labels.append(Label('package', allure_package(item)))
9090

9191
uuid = self._cache.pop(item.nodeid)
9292
self.allure_logger.close_test(uuid)

allure-pytest/src/utils.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import unicode_literals
33

44
import os
5+
import six
56
import pytest
67
from allure_commons.utils import represent
78
from allure_commons.utils import format_exception, format_traceback
@@ -56,18 +57,32 @@ def mark_to_str(marker):
5657
return '@pytest.mark.{name}'.format(name=marker.name)
5758

5859

59-
def allure_package(nodeid):
60-
parts = nodeid.split('::')
60+
def allure_package(item):
61+
parts = item.nodeid.split('::')
6162
path = parts[0].split('.')[0]
6263
return path.replace(os.sep, '.')
6364

6465

65-
def allure_full_name(nodeid):
66-
parts = nodeid.split('::')
67-
package = allure_package(nodeid)
66+
def allure_name(item):
67+
return escape_name(item.name)
68+
69+
70+
def allure_full_name(item):
71+
parts = item.nodeid.split('::')
72+
package = allure_package(item)
6873
clazz = '.{clazz}'.format(clazz=parts[1]) if len(parts) > 2 else ''
6974
test = parts[-1]
70-
return '{package}{clazz}#{test}'.format(package=package, clazz=clazz, test=test)
75+
full_name = '{package}{clazz}#{test}'.format(package=package, clazz=clazz, test=test)
76+
return escape_name(full_name)
77+
78+
79+
def escape_name(name):
80+
if six.PY2:
81+
try:
82+
name.decode('string_escape').encode('unicode_escape')
83+
except UnicodeDecodeError:
84+
return name.decode('string_escape').decode('utf-8')
85+
return name.encode('utf_8').decode('unicode_escape')
7186

7287

7388
def get_outcome_status(outcome):
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import pytest
4+
5+
6+
@pytest.mark.parametrize('param', [True], ids=['Болек'])
7+
def test_parametrization_native_ids_encoding(param):
8+
"""
9+
>>> import six
10+
>>> allure_report = getfixture('allure_report')
11+
>>> param = 'Болек'
12+
>>> test_case = 'test_parametrization_native_ids_encoding[{param}]'.format(param=param)
13+
>>> if six.PY2:
14+
... test_case = test_case.decode('utf-8')
15+
>>> assert_that(allure_report, has_test_case(test_case))
16+
"""
17+
pass
18+
19+
20+
@pytest.mark.parametrize('param', [True], ids=[u'Лёлек'])
21+
def test_parametrization_utf_ids_encoding(param):
22+
"""
23+
>>> import six
24+
>>> allure_report = getfixture('allure_report')
25+
>>> if six.PY2:
26+
... param = 'Лёлек'.decode('utf-8')
27+
... test_case = u'test_parametrization_utf_ids_encoding[{param}]'.format(param=param)
28+
... assert_that(allure_report, has_test_case(test_case))
29+
... else:
30+
... param = 'Лёлек'
31+
... test_case = 'test_parametrization_utf_ids_encoding[{param}]'.format(param=param)
32+
... assert_that(allure_report, has_test_case(test_case))
33+
"""
34+
pass

allure-pytest/tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ commands =
4444
# `tox -e demo -- -k test_single_feature_label` or
4545
# `tox -e demo -- ./test/steps/`
4646
[testenv:demo]
47+
basepython = python3.5
48+
4749
passenv = HOME
4850

4951
whitelist_externals = rm

0 commit comments

Comments
 (0)