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

Commit

Permalink
fix incompatibility with pytest 3.2.0 (via #126)
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Rudakov authored and sseliverstov committed Aug 4, 2017
1 parent f3ac662 commit fc0684c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 39 deletions.
85 changes: 50 additions & 35 deletions allure/common.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
# -*- coding: utf-8 -*-

"""
Created on Feb 23, 2014
@author: pupssman
"""
from six import text_type, iteritems
from contextlib import contextmanager
from functools import wraps
import os
import uuid
from contextlib import contextmanager
from distutils.version import StrictVersion
from functools import wraps

from _pytest.runner import Skipped
from _pytest.skipping import XFailed

from lxml import etree
import py
from _pytest import __version__ as pytest_version
from lxml import etree
from six import text_type, iteritems

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

if StrictVersion(pytest_version) >= StrictVersion("3.2.0"):
from _pytest.outcomes import Skipped, XFailed
else:
from _pytest.runner import Skipped
from _pytest.skipping import XFailed

class StepContext:

class StepContext:
def __init__(self, allure, title):
self.allure = allure
self.title = title
Expand Down Expand Up @@ -51,15 +54,16 @@ def __call__(self, func):
Pretend that we are a decorator -- wrap the ``func`` with self.
FIXME: may fail if evil dude will try to reuse ``pytest.allure.step`` instance.
"""

@wraps(func)
def impl(*a, **kw):
with StepContext(self.allure, self.title.format(*a, **kw)):
return func(*a, **kw)

return impl


class AllureImpl(object):

"""
Allure test-flow implementation that handles test data creation.
Devised to be used as base layer for allure adaptation to arbitrary test frameworks.
Expand Down Expand Up @@ -88,7 +92,8 @@ class AllureImpl(object):
"""

def __init__(self, logdir):
self.logdir = os.path.normpath(os.path.abspath(os.path.expanduser(os.path.expandvars(logdir))))
self.logdir = os.path.normpath(
os.path.abspath(os.path.expanduser(os.path.expandvars(logdir))))

# Delete all files in report directory
if not os.path.exists(self.logdir):
Expand All @@ -110,21 +115,19 @@ def attach(self, title, contents, attach_type):
"""
Attaches ``contents`` with ``title`` and ``attach_type`` to the current active thing
"""
attach = Attach(source=self._save_attach(contents, attach_type=attach_type),
title=title,
type=attach_type.mime_type)
attach = Attach(
source=self._save_attach(contents, attach_type=attach_type),
title=title,
type=attach_type.mime_type)
self.stack[-1].attachments.append(attach)

def start_step(self, name):
"""
Starts an new :py:class:`allure.structure.TestStep` with given ``name``,
pushes it to the ``self.stack`` and returns the step.
"""
step = TestStep(name=name,
title=name,
start=now(),
attachments=[],
steps=[])
step = TestStep(
name=name, title=name, start=now(), attachments=[], steps=[])
self.stack[-1].steps.append(step)
self.stack.append(step)
return step
Expand All @@ -140,12 +143,13 @@ def start_case(self, name, description=None, labels=None):
"""
Starts a new :py:class:`allure.structure.TestCase`
"""
test = TestCase(name=name,
description=description,
start=now(),
attachments=[],
labels=labels or [],
steps=[])
test = TestCase(
name=name,
description=description,
start=now(),
attachments=[],
labels=labels or [],
steps=[])
self.stack.append(test)

def stop_case(self, status, message=None, trace=None):
Expand Down Expand Up @@ -173,12 +177,13 @@ def start_suite(self, name, description=None, title=None, labels=None):
"""
Starts a new Suite with given ``name`` and ``description``
"""
self.testsuite = TestSuite(name=name,
title=title,
description=description,
tests=[],
labels=labels or [],
start=now())
self.testsuite = TestSuite(
name=name,
title=title,
description=description,
tests=[],
labels=labels or [],
start=now())

def stop_suite(self):
"""
Expand All @@ -193,9 +198,13 @@ def store_environment(self):
if not self.environment:
return

environment = Environment(id=uuid.uuid4(), name="Allure environment parameters", parameters=[])
environment = Environment(
id=uuid.uuid4(),
name="Allure environment parameters",
parameters=[])
for key, value in iteritems(self.environment):
environment.parameters.append(EnvParameter(name=key, key=key, value=value))
environment.parameters.append(
EnvParameter(name=key, key=key, value=value))

with self._reportfile('environment.xml') as f:
self._write_xml(f, environment)
Expand All @@ -206,7 +215,8 @@ 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?)
"""
with self._attachfile("%s-attachment.%s" % (uuid.uuid4(), attach_type.extension)) as f:
with self._attachfile("%s-attachment.%s" %
(uuid.uuid4(), attach_type.extension)) as f:
if isinstance(body, text_type):
f.write(body.encode('utf-8'))
else:
Expand Down Expand Up @@ -239,4 +249,9 @@ def _reportfile(self, filename):
logfile.close()

def _write_xml(self, logfile, xmlfied):
logfile.write(etree.tostring(xmlfied.toxml(), pretty_print=True, xml_declaration=False, encoding=text_type))
logfile.write(
etree.tostring(
xmlfied.toxml(),
pretty_print=True,
xml_declaration=False,
encoding=text_type))
6 changes: 3 additions & 3 deletions tests/test_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import pytest

from hamcrest import is_, assert_that, contains, has_property, all_of, has_entry, greater_than, less_than, \
has_entries, contains_inanyorder, is_not, has_items, starts_with
has_entries, contains_inanyorder, is_not, has_items, starts_with, less_than_or_equal_to
from allure.constants import Status
from .matchers import has_float

Expand Down Expand Up @@ -88,12 +88,12 @@ def test():

assert_that(report.get('start'), has_float(all_of(
greater_than(start * 1000),
less_than(float(report.get('stop')))
less_than_or_equal_to(float(report.get('stop')))
)))

assert_that(report.get('stop'), has_float(all_of(
greater_than(float(report.get('start'))),
less_than(stop * 1000),
less_than_or_equal_to(stop * 1000),
)))


Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ envlist=py26,py27,py33,py34,py35,static_check
deps=
pytest
pytest-cov
pytest-xdist
pytest-xdist <= 1.15
pyhamcrest

commands=
Expand Down

0 comments on commit fc0684c

Please sign in to comment.