| 1 |
""" run test suites written for nose. """ |
| 2 |
|
| 3 |
import sys |
| 4 |
|
| 5 |
import py |
| 6 |
import pytest |
| 7 |
from _pytest import unittest |
| 8 |
|
| 9 |
|
| 10 |
def get_skip_exceptions(): |
| 11 |
skip_classes = set() |
| 12 |
for module_name in ('unittest', 'unittest2', 'nose'): |
| 13 |
mod = sys.modules.get(module_name) |
| 14 |
if hasattr(mod, 'SkipTest'): |
| 15 |
skip_classes.add(mod.SkipTest) |
| 16 |
return tuple(skip_classes) |
| 17 |
|
| 18 |
|
| 19 |
def pytest_runtest_makereport(item, call): |
| 20 |
if call.excinfo and call.excinfo.errisinstance(get_skip_exceptions()): |
| 21 |
|
| 22 |
call2 = call.__class__(lambda: |
| 23 |
pytest.skip(str(call.excinfo.value)), call.when) |
| 24 |
call.excinfo = call2.excinfo |
| 25 |
|
| 26 |
|
| 27 |
@pytest.hookimpl(trylast=True) |
| 28 |
def pytest_runtest_setup(item): |
| 29 |
if is_potential_nosetest(item): |
| 30 |
if isinstance(item.parent, pytest.Generator): |
| 31 |
gen = item.parent |
| 32 |
if not hasattr(gen, '_nosegensetup'): |
| 33 |
call_optional(gen.obj, 'setup') |
| 34 |
if isinstance(gen.parent, pytest.Instance): |
| 35 |
call_optional(gen.parent.obj, 'setup') |
| 36 |
gen._nosegensetup = True |
| 37 |
if not call_optional(item.obj, 'setup'): |
| 38 |
|
| 39 |
call_optional(item.parent.obj, 'setup') |
| 40 |
|
| 41 |
item.session._setupstate.addfinalizer((lambda: teardown_nose(item)), item) |
| 42 |
|
| 43 |
def teardown_nose(item): |
| 44 |
if is_potential_nosetest(item): |
| 45 |
if not call_optional(item.obj, 'teardown'): |
| 46 |
call_optional(item.parent.obj, 'teardown') |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
def pytest_make_collect_report(collector): |
| 53 |
if isinstance(collector, pytest.Generator): |
| 54 |
call_optional(collector.obj, 'setup') |
| 55 |
|
| 56 |
|
| 57 |
def is_potential_nosetest(item): |
| 58 |
|
| 59 |
|
| 60 |
return isinstance(item, pytest.Function) and \ |
| 61 |
not isinstance(item, unittest.TestCaseFunction) |
| 62 |
|
| 63 |
|
| 64 |
def call_optional(obj, name): |
| 65 |
method = getattr(obj, name, None) |
| 66 |
isfixture = hasattr(method, "_pytestfixturefunction") |
| 67 |
if method is not None and not isfixture and py.builtin.callable(method): |
| 68 |
|
| 69 |
|
| 70 |
method() |
| 71 |
return True |