Skip to content

Commit 524beb8

Browse files
authored
robot test plan (via #538)
1 parent 5923e5c commit 524beb8

File tree

10 files changed

+103
-21
lines changed

10 files changed

+103
-21
lines changed

allure-python-commons/src/mapping.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
from itertools import chain, islice
22
import attr
3-
3+
import re
44
from allure_commons.types import Severity, LabelType, LinkType
55
from allure_commons.types import ALLURE_UNIQUE_LABELS
66
from allure_commons.model2 import Label, Link
77

88

99
TAG_PREFIX = "allure"
1010

11+
semi_sep = re.compile(r"allure[\.\w]+:")
12+
eq_sep = re.compile(r"allure[\.\w]+=")
13+
14+
15+
def allure_tag_sep(tag):
16+
if semi_sep.search(tag):
17+
return ":"
18+
if eq_sep.search(tag):
19+
return "="
20+
1121

1222
def __is(kind, t):
1323
return kind in [v for k, v in t.__dict__.items() if not k.startswith('__')]
@@ -39,7 +49,8 @@ def parse_tag(tag, issue_pattern=None, link_pattern=None):
3949
>>> parse_tag("allure.foo:1")
4050
Label(name='tag', value='allure.foo:1')
4151
"""
42-
schema, value = islice(chain(tag.split(':', 1), [None]), 2)
52+
sep = allure_tag_sep(tag)
53+
schema, value = islice(chain(tag.split(sep, 1), [None]), 2)
4354
prefix, kind, name = islice(chain(schema.split('.'), [None], [None]), 3)
4455

4556
if tag in [severity for severity in Severity]:
@@ -57,6 +68,9 @@ def parse_tag(tag, issue_pattern=None, link_pattern=None):
5768
if __is(kind, LabelType):
5869
return Label(name=kind, value=value)
5970

71+
if kind == "id":
72+
return Label(name=LabelType.ID, value=value)
73+
6074
if kind == "label" and name is not None:
6175
return Label(name=name, value=value)
6276

allure-robotframework/examples/label/__init__.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
.. code:: robotframework
33
44
*** Settings ***
5-
Force Tags epic:Tag
5+
Force Tags allure.epic:Tag

allure-robotframework/examples/label/testcase_bdd_label.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
.. code:: robotframework
33
44
*** Settings ***
5-
Force Tags feature:Label
5+
Force Tags allure.feature:Label
66
77
*** Test Case ***
88
Test Cases With BDD Labels
9-
[Tags] story:Test case BDD labels
9+
[Tags] allure.story:Test case BDD labels
1010
No Operation
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
.. code:: robotframework
3+
4+
*** Test Cases ***
5+
First testcase
6+
No Operation
7+
8+
Second testcase
9+
No Operation
10+
11+
12+
.. code:: robotframework
13+
14+
*** Test Cases ***
15+
Test case with allure id
16+
[Tags] allure.id=123
17+
No Operation
18+
19+
One more case with allure id
20+
[Tags] allure.id=777
21+
No Operation
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from allure_robotframework.robot_listener import allure_robotframework
2+
from allure_robotframework.allure_testplan import allure_testplan as testplan
23

3-
__all__ = ['allure_robotframework']
4+
__all__ = ['allure_robotframework', "testplan"]

allure-robotframework/src/listener/allure_listener.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,14 @@ def stop_test(self, _, attributes, messages):
149149
test_result.labels.append(Label(name=LabelType.HOST, value=self._host))
150150
test_result.labels.append(Label(name=LabelType.THREAD, value=pool_id()))
151151
test_result.labels.extend(allure_tags(attributes))
152+
tags = attributes.get('tags', ())
153+
test_result.labels.extend(allure_labels(tags))
152154
test_result.statusDetails = StatusDetails(message=attributes.get('message'),
153155
trace=self._current_tb)
154156

155157
if attributes.get('critical') == 'yes':
156158
test_result.labels.append(Label(name=LabelType.SEVERITY, value=Severity.CRITICAL))
157159

158-
for label_type in (LabelType.EPIC, LabelType.FEATURE, LabelType.STORY):
159-
test_result.labels.extend(allure_labels(attributes, label_type))
160-
161160
for link_type in (LinkType.ISSUE, LinkType.TEST_CASE, LinkType.LINK):
162161
test_result.links.extend(allure_links(attributes, link_type))
163162

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from robot.api import SuiteVisitor
2+
from allure_commons.utils import get_testplan
3+
from allure_robotframework.utils import allure_labels
4+
from allure_commons.types import LabelType
5+
6+
7+
# noinspection PyPep8Naming
8+
class allure_testplan(SuiteVisitor):
9+
def __init__(self):
10+
self.testplan = get_testplan()
11+
12+
def start_suite(self, suite):
13+
if self.testplan:
14+
# included_tests = [test["selector"] for test in self.testplan]
15+
suite.filter(included_tests=self.included_tests(suite))
16+
17+
def included_tests(self, suite):
18+
included_tests = [""]
19+
for test in suite.tests:
20+
allure_id = None
21+
for label in allure_labels(test.tags):
22+
if label.name == LabelType.ID:
23+
allure_id = str(label.value)
24+
if allure_id and any([allure_id == item.get("id", None) for item in self.testplan]):
25+
included_tests.append(test.name)
26+
27+
return included_tests or [test["selector"] for test in self.testplan]

allure-robotframework/src/listener/utils.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from allure_commons.model2 import Status, Label, Parameter, Link
44
from allure_commons.types import LabelType
55
from allure_robotframework.types import RobotStatus
6+
from allure_commons.mapping import parse_tag, labels_set, allure_tag_sep
67

78

89
def get_allure_status(status):
@@ -40,19 +41,12 @@ def get_allure_suites(longname):
4041

4142

4243
def allure_tags(attributes):
43-
return [Label(LabelType.TAG, tag) for tag in attributes.get('tags', ())]
44+
return [Label(LabelType.TAG, tag) for tag in attributes.get('tags', ()) if not allure_tag_sep(tag)]
4445

4546

46-
def allure_labels(attributes, prefix):
47-
tags = attributes.get('tags', ())
48-
49-
def is_label(label):
50-
return label.startswith("{label}:".format(label=prefix))
51-
52-
def label_value(label):
53-
return label.split(':')[1] or 'unknown'
54-
55-
return [Label(name=prefix, value=label_value(tag)) for tag in tags if is_label(tag)]
47+
def allure_labels(tags):
48+
parsed = [parse_tag(item) for item in tags]
49+
return labels_set(list(filter(lambda x: isinstance(x, Label), parsed)))
5650

5751

5852
def allure_links(attributes, prefix):

allure-robotframework/test/run_robot_library.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from tempfile import mkdtemp
2+
from tempfile import mkdtemp, mkstemp
33
from robot import run
44
from multiprocessing import Process
55
from allure_commons_test.report import AllureReport
@@ -10,6 +10,14 @@ def run_robot_with_allure(*args, **kwargs):
1010
targets = map(lambda target: os.path.join(root, target), args)
1111
tmp_path = mkdtemp(dir=os.environ.get('TEST_TMP', '/tmp'))
1212

13+
if "testplan" in kwargs:
14+
# kwargs.pop("testplan")
15+
kwargs["prerunmodifier"] = "allure_robotframework.testplan"
16+
file, filename = mkstemp(suffix=".json", dir=tmp_path)
17+
os.environ["ALLURE_TESTPLAN_PATH"] = filename
18+
with os.fdopen(file, 'w') as tmp:
19+
tmp.write(kwargs["testplan"])
20+
1321
def run_robot(path, **kw):
1422

1523
# ToDo: fix it (_core not works correctly with multiprocessing)
@@ -38,4 +46,6 @@ def run_robot(path, **kw):
3846
robot_process.start()
3947
robot_process.join()
4048

49+
os.environ.pop("ALLURE_TESTPLAN_PATH", None)
50+
4151
return AllureReport(tmp_path)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
*** Settings ***
2+
Library ../run_robot_library.py
3+
Library ../test_allure_library.py
4+
5+
6+
*** Variables **
7+
${PLAN_A} \{
8+
... "version":"1.0",
9+
... "tests": [
10+
... { "id": "123", "selector": "Second testcase"}
11+
... ]
12+
... \}
13+
14+
*** Test Case ***
15+
Failed Test Case With Message
16+
${allure_report} Run Robot With Allure examples/testplan/testplan.rst testplan=${PLAN_A}

0 commit comments

Comments
 (0)