Skip to content

Commit

Permalink
robot test plan (via #538)
Browse files Browse the repository at this point in the history
  • Loading branch information
sseliverstov authored Dec 21, 2020
1 parent 5923e5c commit 524beb8
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 21 deletions.
18 changes: 16 additions & 2 deletions allure-python-commons/src/mapping.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
from itertools import chain, islice
import attr

import re
from allure_commons.types import Severity, LabelType, LinkType
from allure_commons.types import ALLURE_UNIQUE_LABELS
from allure_commons.model2 import Label, Link


TAG_PREFIX = "allure"

semi_sep = re.compile(r"allure[\.\w]+:")
eq_sep = re.compile(r"allure[\.\w]+=")


def allure_tag_sep(tag):
if semi_sep.search(tag):
return ":"
if eq_sep.search(tag):
return "="


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

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

if kind == "id":
return Label(name=LabelType.ID, value=value)

if kind == "label" and name is not None:
return Label(name=name, value=value)

Expand Down
2 changes: 1 addition & 1 deletion allure-robotframework/examples/label/__init__.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
.. code:: robotframework
*** Settings ***
Force Tags epic:Tag
Force Tags allure.epic:Tag
4 changes: 2 additions & 2 deletions allure-robotframework/examples/label/testcase_bdd_label.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
.. code:: robotframework
*** Settings ***
Force Tags feature:Label
Force Tags allure.feature:Label
*** Test Case ***
Test Cases With BDD Labels
[Tags] story:Test case BDD labels
[Tags] allure.story:Test case BDD labels
No Operation
21 changes: 21 additions & 0 deletions allure-robotframework/examples/testplan/testplan.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

.. code:: robotframework
*** Test Cases ***
First testcase
No Operation
Second testcase
No Operation
.. code:: robotframework
*** Test Cases ***
Test case with allure id
[Tags] allure.id=123
No Operation
One more case with allure id
[Tags] allure.id=777
No Operation
3 changes: 2 additions & 1 deletion allure-robotframework/src/listener/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from allure_robotframework.robot_listener import allure_robotframework
from allure_robotframework.allure_testplan import allure_testplan as testplan

__all__ = ['allure_robotframework']
__all__ = ['allure_robotframework', "testplan"]
5 changes: 2 additions & 3 deletions allure-robotframework/src/listener/allure_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,14 @@ def stop_test(self, _, attributes, messages):
test_result.labels.append(Label(name=LabelType.HOST, value=self._host))
test_result.labels.append(Label(name=LabelType.THREAD, value=pool_id()))
test_result.labels.extend(allure_tags(attributes))
tags = attributes.get('tags', ())
test_result.labels.extend(allure_labels(tags))
test_result.statusDetails = StatusDetails(message=attributes.get('message'),
trace=self._current_tb)

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

for label_type in (LabelType.EPIC, LabelType.FEATURE, LabelType.STORY):
test_result.labels.extend(allure_labels(attributes, label_type))

for link_type in (LinkType.ISSUE, LinkType.TEST_CASE, LinkType.LINK):
test_result.links.extend(allure_links(attributes, link_type))

Expand Down
27 changes: 27 additions & 0 deletions allure-robotframework/src/listener/allure_testplan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from robot.api import SuiteVisitor
from allure_commons.utils import get_testplan
from allure_robotframework.utils import allure_labels
from allure_commons.types import LabelType


# noinspection PyPep8Naming
class allure_testplan(SuiteVisitor):
def __init__(self):
self.testplan = get_testplan()

def start_suite(self, suite):
if self.testplan:
# included_tests = [test["selector"] for test in self.testplan]
suite.filter(included_tests=self.included_tests(suite))

def included_tests(self, suite):
included_tests = [""]
for test in suite.tests:
allure_id = None
for label in allure_labels(test.tags):
if label.name == LabelType.ID:
allure_id = str(label.value)
if allure_id and any([allure_id == item.get("id", None) for item in self.testplan]):
included_tests.append(test.name)

return included_tests or [test["selector"] for test in self.testplan]
16 changes: 5 additions & 11 deletions allure-robotframework/src/listener/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from allure_commons.model2 import Status, Label, Parameter, Link
from allure_commons.types import LabelType
from allure_robotframework.types import RobotStatus
from allure_commons.mapping import parse_tag, labels_set, allure_tag_sep


def get_allure_status(status):
Expand Down Expand Up @@ -40,19 +41,12 @@ def get_allure_suites(longname):


def allure_tags(attributes):
return [Label(LabelType.TAG, tag) for tag in attributes.get('tags', ())]
return [Label(LabelType.TAG, tag) for tag in attributes.get('tags', ()) if not allure_tag_sep(tag)]


def allure_labels(attributes, prefix):
tags = attributes.get('tags', ())

def is_label(label):
return label.startswith("{label}:".format(label=prefix))

def label_value(label):
return label.split(':')[1] or 'unknown'

return [Label(name=prefix, value=label_value(tag)) for tag in tags if is_label(tag)]
def allure_labels(tags):
parsed = [parse_tag(item) for item in tags]
return labels_set(list(filter(lambda x: isinstance(x, Label), parsed)))


def allure_links(attributes, prefix):
Expand Down
12 changes: 11 additions & 1 deletion allure-robotframework/test/run_robot_library.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from tempfile import mkdtemp
from tempfile import mkdtemp, mkstemp
from robot import run
from multiprocessing import Process
from allure_commons_test.report import AllureReport
Expand All @@ -10,6 +10,14 @@ def run_robot_with_allure(*args, **kwargs):
targets = map(lambda target: os.path.join(root, target), args)
tmp_path = mkdtemp(dir=os.environ.get('TEST_TMP', '/tmp'))

if "testplan" in kwargs:
# kwargs.pop("testplan")
kwargs["prerunmodifier"] = "allure_robotframework.testplan"
file, filename = mkstemp(suffix=".json", dir=tmp_path)
os.environ["ALLURE_TESTPLAN_PATH"] = filename
with os.fdopen(file, 'w') as tmp:
tmp.write(kwargs["testplan"])

def run_robot(path, **kw):

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

os.environ.pop("ALLURE_TESTPLAN_PATH", None)

return AllureReport(tmp_path)
16 changes: 16 additions & 0 deletions allure-robotframework/test/testplan/testplan.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
*** Settings ***
Library ../run_robot_library.py
Library ../test_allure_library.py


*** Variables **
${PLAN_A} \{
... "version":"1.0",
... "tests": [
... { "id": "123", "selector": "Second testcase"}
... ]
... \}

*** Test Case ***
Failed Test Case With Message
${allure_report} Run Robot With Allure examples/testplan/testplan.rst testplan=${PLAN_A}

0 comments on commit 524beb8

Please sign in to comment.