From fc0684c874005e5762e93e63d0683f612bbbd4d4 Mon Sep 17 00:00:00 2001 From: Roman Rudakov Date: Fri, 4 Aug 2017 16:52:52 +0300 Subject: [PATCH] fix incompatibility with pytest 3.2.0 (via #126) --- allure/common.py | 85 ++++++++++++++++++++++++++------------------- tests/test_smoke.py | 6 ++-- tox.ini | 2 +- 3 files changed, 54 insertions(+), 39 deletions(-) diff --git a/allure/common.py b/allure/common.py index 911b983..f5ccb4a 100644 --- a/allure/common.py +++ b/allure/common.py @@ -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 @@ -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. @@ -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): @@ -110,9 +115,10 @@ 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): @@ -120,11 +126,8 @@ 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 @@ -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): @@ -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): """ @@ -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) @@ -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: @@ -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)) diff --git a/tests/test_smoke.py b/tests/test_smoke.py index 2a41b35..f8077ff 100644 --- a/tests/test_smoke.py +++ b/tests/test_smoke.py @@ -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 @@ -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), ))) diff --git a/tox.ini b/tox.ini index 1107a55..af4b8bc 100644 --- a/tox.ini +++ b/tox.ini @@ -6,7 +6,7 @@ envlist=py26,py27,py33,py34,py35,static_check deps= pytest pytest-cov - pytest-xdist + pytest-xdist <= 1.15 pyhamcrest commands=