Skip to content

Commit

Permalink
fixup nose/pytest plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
RonnyPfannschmidt committed Mar 16, 2017
1 parent 909ed38 commit 21c2dcf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
18 changes: 9 additions & 9 deletions _pytest/nose.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import sys

import py
import pytest
from _pytest import unittest
from _pytest import unittest, runner, python
from _pytest.config import hookimpl


def get_skip_exceptions():
Expand All @@ -19,19 +19,19 @@ def get_skip_exceptions():
def pytest_runtest_makereport(item, call):
if call.excinfo and call.excinfo.errisinstance(get_skip_exceptions()):
# let's substitute the excinfo with a pytest.skip one
call2 = call.__class__(lambda:
pytest.skip(str(call.excinfo.value)), call.when)
call2 = call.__class__(
lambda: runner.skip(str(call.excinfo.value)), call.when)
call.excinfo = call2.excinfo


@pytest.hookimpl(trylast=True)
@hookimpl(trylast=True)
def pytest_runtest_setup(item):
if is_potential_nosetest(item):
if isinstance(item.parent, pytest.Generator):
if isinstance(item.parent, python.Generator):
gen = item.parent
if not hasattr(gen, '_nosegensetup'):
call_optional(gen.obj, 'setup')
if isinstance(gen.parent, pytest.Instance):
if isinstance(gen.parent, python.Instance):
call_optional(gen.parent.obj, 'setup')
gen._nosegensetup = True
if not call_optional(item.obj, 'setup'):
Expand All @@ -50,14 +50,14 @@ def teardown_nose(item):


def pytest_make_collect_report(collector):
if isinstance(collector, pytest.Generator):
if isinstance(collector, python.Generator):
call_optional(collector.obj, 'setup')


def is_potential_nosetest(item):
# extra check needed since we do not do nose style setup/teardown
# on direct unittest style classes
return isinstance(item, pytest.Function) and \
return isinstance(item, python.Function) and \
not isinstance(item, unittest.TestCaseFunction)


Expand Down
46 changes: 25 additions & 21 deletions _pytest/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import sys
import traceback

import pytest
# for transferring markers
import _pytest._code
from _pytest.python import transfer_markers
from _pytest.skipping import MarkEvaluator
from _pytest.config import hookimpl
from _pytest.runner import fail, skip
from _pytest.python import transfer_markers, Class, Module, Function
from _pytest.skipping import MarkEvaluator, xfail


def pytest_pycollect_makeitem(collector, name, obj):
Expand All @@ -22,11 +23,11 @@ def pytest_pycollect_makeitem(collector, name, obj):
return UnitTestCase(name, parent=collector)


class UnitTestCase(pytest.Class):
class UnitTestCase(Class):
# marker for fixturemanger.getfixtureinfo()
# to declare that our children do not support funcargs
nofuncargs = True

def setup(self):
cls = self.obj
if getattr(cls, '__unittest_skip__', False):
Expand All @@ -46,7 +47,7 @@ def collect(self):
return
self.session._fixturemanager.parsefactories(self, unittest=True)
loader = TestLoader()
module = self.getparent(pytest.Module).obj
module = self.getparent(Module).obj
foundsomething = False
for name in loader.getTestCaseNames(self.obj):
x = getattr(self.obj, name)
Expand All @@ -65,7 +66,7 @@ def collect(self):
yield TestCaseFunction('runTest', parent=self)


class TestCaseFunction(pytest.Function):
class TestCaseFunction(Function):
_excinfo = None

def setup(self):
Expand Down Expand Up @@ -110,36 +111,37 @@ def _addexcinfo(self, rawexcinfo):
try:
l = traceback.format_exception(*rawexcinfo)
l.insert(0, "NOTE: Incompatible Exception Representation, "
"displaying natively:\n\n")
pytest.fail("".join(l), pytrace=False)
except (pytest.fail.Exception, KeyboardInterrupt):
"displaying natively:\n\n")
fail("".join(l), pytrace=False)
except (fail.Exception, KeyboardInterrupt):
raise
except:
pytest.fail("ERROR: Unknown Incompatible Exception "
"representation:\n%r" %(rawexcinfo,), pytrace=False)
fail("ERROR: Unknown Incompatible Exception "
"representation:\n%r" % (rawexcinfo,), pytrace=False)
except KeyboardInterrupt:
raise
except pytest.fail.Exception:
except fail.Exception:
excinfo = _pytest._code.ExceptionInfo()
self.__dict__.setdefault('_excinfo', []).append(excinfo)

def addError(self, testcase, rawexcinfo):
self._addexcinfo(rawexcinfo)

def addFailure(self, testcase, rawexcinfo):
self._addexcinfo(rawexcinfo)

def addSkip(self, testcase, reason):
try:
pytest.skip(reason)
except pytest.skip.Exception:
skip(reason)
except skip.Exception:
self._evalskip = MarkEvaluator(self, 'SkipTest')
self._evalskip.result = True
self._addexcinfo(sys.exc_info())

def addExpectedFailure(self, testcase, rawexcinfo, reason=""):
try:
pytest.xfail(str(reason))
except pytest.xfail.Exception:
xfail(str(reason))
except xfail.Exception:
self._addexcinfo(sys.exc_info())

def addUnexpectedSuccess(self, testcase, reason=""):
Expand Down Expand Up @@ -179,13 +181,14 @@ def runtest(self):
self._testcase.debug()

def _prunetraceback(self, excinfo):
pytest.Function._prunetraceback(self, excinfo)
Function._prunetraceback(self, excinfo)
traceback = excinfo.traceback.filter(
lambda x:not x.frame.f_globals.get('__unittest'))
lambda x: not x.frame.f_globals.get('__unittest'))
if traceback:
excinfo.traceback = traceback

@pytest.hookimpl(tryfirst=True)

@hookimpl(tryfirst=True)
def pytest_runtest_makereport(item, call):
if isinstance(item, TestCaseFunction):
if item._excinfo:
Expand All @@ -197,7 +200,8 @@ def pytest_runtest_makereport(item, call):

# twisted trial support

@pytest.hookimpl(hookwrapper=True)

@hookimpl(hookwrapper=True)
def pytest_runtest_protocol(item):
if isinstance(item, TestCaseFunction) and \
'twisted.trial.unittest' in sys.modules:
Expand Down

0 comments on commit 21c2dcf

Please sign in to comment.