Skip to content
This repository has been archived by the owner on Jan 14, 2019. It is now read-only.

Commit

Permalink
Merge pull request #37 from mavlyutov/master
Browse files Browse the repository at this point in the history
adapted to allure-core version 1.4.x
  • Loading branch information
pupssman committed Oct 3, 2014
2 parents f96c55d + b565372 commit 7552233
Show file tree
Hide file tree
Showing 15 changed files with 308 additions and 282 deletions.
2 changes: 1 addition & 1 deletion allure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


# providing decorators via allure.xxx instead of pytest.allure.xxx
__methods_to_provide = ['step', 'attach', 'single_step', 'label', 'feature', 'story']
__methods_to_provide = ['step', 'attach', 'single_step', 'label', 'feature', 'story', 'severity']

for method in __methods_to_provide:
globals()[method] = getattr(MASTER_HELPER, method)
16 changes: 6 additions & 10 deletions allure/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from lxml import etree
import py

from allure.constants import AttachmentType, Severity, Status
from allure.constants import AttachmentType, Status
from allure.structure import Attach, TestStep, TestCase, TestSuite, Failure
from allure.utils import now

Expand All @@ -27,7 +27,7 @@ def __enter__(self):
if self.allure:
self.step = self.allure.start_step(self.title)

def __exit__(self, exc_type, exc_val, exc_tb): # @UnusedVariable
def __exit__(self, exc_type, exc_val, exc_tb):
if self.allure:
if exc_type is not None:
if exc_type == Skipped:
Expand Down Expand Up @@ -102,7 +102,7 @@ def attach(self, title, contents, attach_type):
"""
attach = Attach(source=self._save_attach(contents, attach_type=attach_type),
title=title,
type=attach_type)
type=attach_type.mime_type)
self.stack[-1].attachments.append(attach)

def start_step(self, name):
Expand All @@ -126,14 +126,12 @@ def stop_step(self):
step = self.stack.pop()
step.stop = now()

def start_case(self, name, description=None, severity=Severity.NORMAL,
labels=None):
def start_case(self, name, description=None, labels=None):
"""
Starts a new :py:class:`allure.structure.TestCase`
"""
test = TestCase(name=name,
description=description,
severity=severity,
start=now(),
attachments=[],
labels=labels or [],
Expand Down Expand Up @@ -187,9 +185,7 @@ def _save_attach(self, body, attach_type=AttachmentType.TEXT):
:arg body: str or unicode with contents. str is written as-is in byte stream, unicode is written as utf-8 (what do you expect else?)
"""

# FIXME: we should generate attachment name properly
with self._attachfile("%s-attachment.%s" % (uuid.uuid4(), attach_type)) as f:
with self._attachfile("%s-attachment.%s" % (uuid.uuid4(), attach_type.extension)) as f:
if isinstance(body, unicode):
f.write(body.encode('utf-8'))
else:
Expand All @@ -214,7 +210,7 @@ def _reportfile(self, filename):
reportpath = os.path.join(self.logdir, filename)
encoding = 'utf-8'

logfile = py.std.codecs.open(reportpath, 'w', encoding=encoding) # @UndefinedVariable
logfile = py.std.codecs.open(reportpath, 'w', encoding=encoding)

try:
yield logfile
Expand Down
52 changes: 29 additions & 23 deletions allure/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,46 @@
@author: pupssman
'''


class Severity:
BLOCKER = 'blocker'
CRITICAL = 'critical'
NORMAL = 'normal'
MINOR = 'minor'
TRIVIAL = 'trivial'
from enum import Enum


class Status:
FAILED = 'failed'
BROKEN = 'broken'
PASSED = 'passed'
SKIPPED = 'skipped'
SKIPPED = 'canceled'
CANCELED = 'canceled'
PENDING = 'pending'


FAILED_STATUSES = [Status.FAILED, Status.BROKEN]
class Label:
DEFAULT = 'allure_label'
FEATURE = 'feature'
STORY = 'story'
SEVERITY = 'severity'


class AttachmentType:
TEXT = "txt"
HTML = "html"
XML = "xml"
PNG = "png"
JPG = "jpg"
JSON = "json"
OTHER = "other"
class Severity:
BLOCKER = 'blocker'
CRITICAL = 'critical'
NORMAL = 'normal'
MINOR = 'minor'
TRIVIAL = 'trivial'


ALLURE_NAMESPACE = "urn:model.allure.qatools.yandex.ru"
class AttachmentType(Enum):
def __init__(self, mime_type, extension):
self.mime_type = mime_type
self.extension = extension

TEXT = ("text/plain", "txt")
HTML = ("application/html", "html")
XML = ("application/xml", "xml")
PNG = ("image/png", "png")
JPG = ("image/jpg", "jpg")
JSON = ("application/json", "json")
OTHER = ("other", "other")

class Label:
DEFAULT = 'allure_label'
FEATURE = 'feature'
STORY = 'story'

ALLURE_NAMESPACE = "urn:model.allure.qatools.yandex.ru"
FAILED_STATUSES = [Status.FAILED, Status.BROKEN]
38 changes: 16 additions & 22 deletions allure/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from allure.utils import LabelsList
from allure.constants import Status, AttachmentType, Severity, \
FAILED_STATUSES, Label
from allure.utils import parent_module, parent_down_from_module, severity_of, \
labels_of, all_of, get_exception_message
from allure.utils import parent_module, parent_down_from_module, labels_of, \
all_of, get_exception_message
from allure.structure import TestLabel


Expand All @@ -23,12 +23,12 @@ def pytest_addoption(parser):

severities = [v for (_, v) in all_of(Severity)]

def severity_type(string):
entries = [x.strip() for x in string.split(',')]
def severity_label_type(string):
entries = LabelsList([TestLabel(name=Label.SEVERITY, value=x) for x in string.split(',')])

for entry in entries:
if entry not in severities:
raise argparse.ArgumentTypeError('Illegal severity value [%s], only values from [%s] are allowed.' % (entry, ', '.join(severities)))
if entry.value not in severities:
raise argparse.ArgumentTypeError('Illegal severity value [%s], only values from [%s] are allowed.' % (entry.value, ', '.join(severities)))

return entries

Expand All @@ -42,8 +42,8 @@ def stories_label_type(string):
action="store",
dest="allureseverities",
metavar="SEVERITIES_LIST",
default=None,
type=severity_type,
default=LabelsList(),
type=severity_label_type,
help="""Comma-separated list of severity names.
Tests only with these severities will be run.
Possible values are:%s.""" % ', '.join(severities))
Expand Down Expand Up @@ -77,15 +77,10 @@ def pytest_configure(config):


def pytest_runtest_setup(item):
severity = severity_of(item)
item_labels = labels_of(item)

option = item.config.option
if option.allureseverities and severity not in \
option.allureseverities:
pytest.skip("Not running test of severity %s." % severity)

arg_labels = option.allurefeatures + option.allurestories
arg_labels = option.allurefeatures + option.allurestories + option.allureseverities

if arg_labels and not item_labels & arg_labels:
pytest.skip('Not suitable with selected labels: %s.' % arg_labels)
Expand Down Expand Up @@ -123,12 +118,6 @@ def attach(self, name, contents, type=AttachmentType.TEXT): # @ReservedAssignme
if self._allurelistener:
self._allurelistener.attach(name, contents, type)

def severity(self, level):
"""
A decorator factory that returns ``pytest.mark`` for a given allure ``level``.
"""
return pytest.mark.allure_severity(level)

def label(self, name, *value):
"""
A decorator factory that returns ``pytest.mark`` for a given label.
Expand All @@ -138,6 +127,12 @@ def label(self, name, *value):
(Label.DEFAULT, name.encode('utf-8', 'ignore')))
return allure_label(*value)

def severity(self, severity):
"""
A decorator factory that returns ``pytest.mark`` for a given allure ``level``.
"""
return self.label(Label.SEVERITY, severity)

def feature(self, *features):
"""
A decorator factory that returns ``pytest.mark`` for a given features.
Expand Down Expand Up @@ -277,8 +272,7 @@ def pytest_runtest_protocol(self, __multicall__, item, nextitem):
self.testsuite = 'Yes'

name = '.'.join(mangle_testnames([x.name for x in parent_down_from_module(item)]))
self.impl.start_case(name, description=item.function.__doc__, severity=severity_of(item),
labels=labels_of(item))
self.impl.start_case(name, description=item.function.__doc__, labels=labels_of(item))
result = __multicall__.execute()

if not nextitem or parent_module(item) != parent_module(nextitem):
Expand Down
3 changes: 1 addition & 2 deletions allure/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
labels=Many(Nested()),
status=Attribute(),
start=Attribute(),
stop=Attribute(),
severity=Attribute())
stop=Attribute())


TestSuite = xmlfied('test-suite',
Expand Down
10 changes: 1 addition & 9 deletions allure/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from _pytest.python import Module

from allure.constants import Severity, Label
from allure.constants import Label
from allure.structure import TestLabel


Expand Down Expand Up @@ -60,14 +60,6 @@ def now():
return sec2ms(time.time())


def severity_of(item):
severity_marker = item.get_marker('allure_severity')
if severity_marker:
return severity_marker.args[0]
else:
return Severity.NORMAL


def labels_of(item):
"""
Returns list of TestLabel elements.
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-

PACKAGE = "pytest-allure-adaptor"
VERSION = "1.4.1"
VERSION = "1.5.0"

import os
from setuptools import setup
Expand All @@ -24,5 +24,6 @@ def read(fname):
install_requires=[
"lxml>=3.2.0",
"pytest>=2.4.1",
"recordtype"]
"recordtype",
"enum34"]
)
Loading

0 comments on commit 7552233

Please sign in to comment.