aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/pytest/py3/_pytest/unittest.py
diff options
context:
space:
mode:
authordeshevoy <deshevoy@yandex-team.ru>2022-02-10 16:46:57 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:46:57 +0300
commit28148f76dbfcc644d96427d41c92f36cbf2fdc6e (patch)
treeb83306b6e37edeea782e9eed673d89286c4fef35 /contrib/python/pytest/py3/_pytest/unittest.py
parente988f30484abe5fdeedcc7a5d3c226c01a21800c (diff)
downloadydb-28148f76dbfcc644d96427d41c92f36cbf2fdc6e.tar.gz
Restoring authorship annotation for <deshevoy@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/python/pytest/py3/_pytest/unittest.py')
-rw-r--r--contrib/python/pytest/py3/_pytest/unittest.py308
1 files changed, 154 insertions, 154 deletions
diff --git a/contrib/python/pytest/py3/_pytest/unittest.py b/contrib/python/pytest/py3/_pytest/unittest.py
index f4471b79227..55f15efe4b7 100644
--- a/contrib/python/pytest/py3/_pytest/unittest.py
+++ b/contrib/python/pytest/py3/_pytest/unittest.py
@@ -1,6 +1,6 @@
"""Discover and run std-library "unittest" style tests."""
-import sys
-import traceback
+import sys
+import traceback
import types
from typing import Any
from typing import Callable
@@ -12,29 +12,29 @@ from typing import Tuple
from typing import Type
from typing import TYPE_CHECKING
from typing import Union
-
-import _pytest._code
+
+import _pytest._code
import pytest
-from _pytest.compat import getimfunc
+from _pytest.compat import getimfunc
from _pytest.compat import is_async_function
-from _pytest.config import hookimpl
+from _pytest.config import hookimpl
from _pytest.fixtures import FixtureRequest
from _pytest.nodes import Collector
from _pytest.nodes import Item
from _pytest.outcomes import exit
-from _pytest.outcomes import fail
-from _pytest.outcomes import skip
-from _pytest.outcomes import xfail
-from _pytest.python import Class
-from _pytest.python import Function
+from _pytest.outcomes import fail
+from _pytest.outcomes import skip
+from _pytest.outcomes import xfail
+from _pytest.python import Class
+from _pytest.python import Function
from _pytest.python import PyCollector
from _pytest.runner import CallInfo
from _pytest.skipping import skipped_by_mark_key
from _pytest.skipping import unexpectedsuccess_key
-
+
if TYPE_CHECKING:
import unittest
-
+
from _pytest.fixtures import _Scope
_SysExcInfoType = Union[
@@ -47,54 +47,54 @@ def pytest_pycollect_makeitem(
collector: PyCollector, name: str, obj: object
) -> Optional["UnitTestCase"]:
# Has unittest been imported and is obj a subclass of its TestCase?
- try:
+ try:
ut = sys.modules["unittest"]
# Type ignored because `ut` is an opaque module.
if not issubclass(obj, ut.TestCase): # type: ignore
return None
- except Exception:
+ except Exception:
return None
# Yes, so let's collect it.
item: UnitTestCase = UnitTestCase.from_parent(collector, name=name, obj=obj)
return item
-
-
-class UnitTestCase(Class):
+
+
+class UnitTestCase(Class):
# Marker for fixturemanger.getfixtureinfo()
# to declare that our children do not support funcargs.
- nofuncargs = True
-
+ nofuncargs = True
+
def collect(self) -> Iterable[Union[Item, Collector]]:
- from unittest import TestLoader
-
- cls = self.obj
- if not getattr(cls, "__test__", True):
- return
+ from unittest import TestLoader
+
+ cls = self.obj
+ if not getattr(cls, "__test__", True):
+ return
skipped = _is_skipped(cls)
if not skipped:
self._inject_setup_teardown_fixtures(cls)
self._inject_setup_class_fixture()
- self.session._fixturemanager.parsefactories(self, unittest=True)
- loader = TestLoader()
- foundsomething = False
- for name in loader.getTestCaseNames(self.obj):
- x = getattr(self.obj, name)
- if not getattr(x, "__test__", True):
- continue
- funcobj = getimfunc(x)
+ self.session._fixturemanager.parsefactories(self, unittest=True)
+ loader = TestLoader()
+ foundsomething = False
+ for name in loader.getTestCaseNames(self.obj):
+ x = getattr(self.obj, name)
+ if not getattr(x, "__test__", True):
+ continue
+ funcobj = getimfunc(x)
yield TestCaseFunction.from_parent(self, name=name, callobj=funcobj)
- foundsomething = True
-
- if not foundsomething:
- runtest = getattr(self.obj, "runTest", None)
- if runtest is not None:
- ut = sys.modules.get("twisted.trial.unittest", None)
+ foundsomething = True
+
+ if not foundsomething:
+ runtest = getattr(self.obj, "runTest", None)
+ if runtest is not None:
+ ut = sys.modules.get("twisted.trial.unittest", None)
# Type ignored because `ut` is an opaque module.
if ut is None or runtest != ut.TestCase.runTest: # type: ignore
yield TestCaseFunction.from_parent(self, name="runTest")
-
+
def _inject_setup_teardown_fixtures(self, cls: type) -> None:
"""Injects a hidden auto-use fixture to invoke setUpClass/setup_method and corresponding
teardown functions (#517)."""
@@ -108,7 +108,7 @@ class UnitTestCase(Class):
)
if class_fixture:
cls.__pytest_class_setup = class_fixture # type: ignore[attr-defined]
-
+
method_fixture = _make_xunit_fixture(
cls,
"setup_method",
@@ -182,63 +182,63 @@ def _make_xunit_fixture(
return fixture
-class TestCaseFunction(Function):
- nofuncargs = True
+class TestCaseFunction(Function):
+ nofuncargs = True
_excinfo: Optional[List[_pytest._code.ExceptionInfo[BaseException]]] = None
_testcase: Optional["unittest.TestCase"] = None
-
+
def setup(self) -> None:
# A bound method to be called during teardown() if set (see 'runtest()').
self._explicit_tearDown: Optional[Callable[[], None]] = None
assert self.parent is not None
self._testcase = self.parent.obj(self.name) # type: ignore[attr-defined]
- self._obj = getattr(self._testcase, self.name)
- if hasattr(self, "_request"):
- self._request._fillfixtures()
-
+ self._obj = getattr(self._testcase, self.name)
+ if hasattr(self, "_request"):
+ self._request._fillfixtures()
+
def teardown(self) -> None:
if self._explicit_tearDown is not None:
self._explicit_tearDown()
self._explicit_tearDown = None
- self._testcase = None
- self._obj = None
-
+ self._testcase = None
+ self._obj = None
+
def startTest(self, testcase: "unittest.TestCase") -> None:
- pass
-
+ pass
+
def _addexcinfo(self, rawexcinfo: "_SysExcInfoType") -> None:
# Unwrap potential exception info (see twisted trial support below).
- rawexcinfo = getattr(rawexcinfo, "_rawexcinfo", rawexcinfo)
- try:
+ rawexcinfo = getattr(rawexcinfo, "_rawexcinfo", rawexcinfo)
+ try:
excinfo = _pytest._code.ExceptionInfo(rawexcinfo) # type: ignore[arg-type]
# Invoke the attributes to trigger storing the traceback
# trial causes some issue there.
excinfo.value
excinfo.traceback
- except TypeError:
- try:
- try:
- values = traceback.format_exception(*rawexcinfo)
- values.insert(
- 0,
- "NOTE: Incompatible Exception Representation, "
- "displaying natively:\n\n",
- )
- fail("".join(values), pytrace=False)
- except (fail.Exception, KeyboardInterrupt):
- raise
+ except TypeError:
+ try:
+ try:
+ values = traceback.format_exception(*rawexcinfo)
+ values.insert(
+ 0,
+ "NOTE: Incompatible Exception Representation, "
+ "displaying natively:\n\n",
+ )
+ fail("".join(values), pytrace=False)
+ except (fail.Exception, KeyboardInterrupt):
+ raise
except BaseException:
- fail(
- "ERROR: Unknown Incompatible Exception "
- "representation:\n%r" % (rawexcinfo,),
- pytrace=False,
- )
- except KeyboardInterrupt:
- raise
- except fail.Exception:
+ fail(
+ "ERROR: Unknown Incompatible Exception "
+ "representation:\n%r" % (rawexcinfo,),
+ pytrace=False,
+ )
+ except KeyboardInterrupt:
+ raise
+ except fail.Exception:
excinfo = _pytest._code.ExceptionInfo.from_current()
- self.__dict__.setdefault("_excinfo", []).append(excinfo)
-
+ self.__dict__.setdefault("_excinfo", []).append(excinfo)
+
def addError(
self, testcase: "unittest.TestCase", rawexcinfo: "_SysExcInfoType"
) -> None:
@@ -247,42 +247,42 @@ class TestCaseFunction(Function):
exit(rawexcinfo[1].msg)
except TypeError:
pass
- self._addexcinfo(rawexcinfo)
-
+ self._addexcinfo(rawexcinfo)
+
def addFailure(
self, testcase: "unittest.TestCase", rawexcinfo: "_SysExcInfoType"
) -> None:
- self._addexcinfo(rawexcinfo)
-
+ self._addexcinfo(rawexcinfo)
+
def addSkip(self, testcase: "unittest.TestCase", reason: str) -> None:
- try:
- skip(reason)
- except skip.Exception:
+ try:
+ skip(reason)
+ except skip.Exception:
self._store[skipped_by_mark_key] = True
- self._addexcinfo(sys.exc_info())
-
+ self._addexcinfo(sys.exc_info())
+
def addExpectedFailure(
self,
testcase: "unittest.TestCase",
rawexcinfo: "_SysExcInfoType",
reason: str = "",
) -> None:
- try:
- xfail(str(reason))
- except xfail.Exception:
- self._addexcinfo(sys.exc_info())
-
+ try:
+ xfail(str(reason))
+ except xfail.Exception:
+ self._addexcinfo(sys.exc_info())
+
def addUnexpectedSuccess(
self, testcase: "unittest.TestCase", reason: str = ""
) -> None:
self._store[unexpectedsuccess_key] = reason
-
+
def addSuccess(self, testcase: "unittest.TestCase") -> None:
- pass
-
+ pass
+
def stopTest(self, testcase: "unittest.TestCase") -> None:
- pass
-
+ pass
+
def _expecting_failure(self, test_method) -> bool:
"""Return True if the given unittest method (or the entire class) is marked
with @expectedFailure."""
@@ -291,7 +291,7 @@ class TestCaseFunction(Function):
)
expecting_failure_class = getattr(self, "__unittest_expecting_failure__", False)
return bool(expecting_failure_class or expecting_failure_method)
-
+
def runtest(self) -> None:
from _pytest.debugging import maybe_wrap_pytest_function_for_tracing
@@ -303,7 +303,7 @@ class TestCaseFunction(Function):
if is_async_function(self.obj):
# Type ignored because self acts as the TestResult, but is not actually one.
self._testcase(result=self) # type: ignore[arg-type]
- else:
+ else:
# When --pdb is given, we want to postpone calling tearDown() otherwise
# when entering the pdb prompt, tearDown() would have probably cleaned up
# instance variables, which makes it difficult to debug.
@@ -313,7 +313,7 @@ class TestCaseFunction(Function):
if self.config.getoption("usepdb") and not _is_skipped(self.obj):
self._explicit_tearDown = self._testcase.tearDown
setattr(self._testcase, "tearDown", lambda *args: None)
-
+
# We need to update the actual bound method with self.obj, because
# wrap_pytest_function_for_tracing replaces self.obj by a wrapper.
setattr(self._testcase, self.name, self.obj)
@@ -325,24 +325,24 @@ class TestCaseFunction(Function):
def _prunetraceback(
self, excinfo: _pytest._code.ExceptionInfo[BaseException]
) -> None:
- Function._prunetraceback(self, excinfo)
- traceback = excinfo.traceback.filter(
- lambda x: not x.frame.f_globals.get("__unittest")
- )
- if traceback:
- excinfo.traceback = traceback
-
-
-@hookimpl(tryfirst=True)
+ Function._prunetraceback(self, excinfo)
+ traceback = excinfo.traceback.filter(
+ lambda x: not x.frame.f_globals.get("__unittest")
+ )
+ if traceback:
+ excinfo.traceback = traceback
+
+
+@hookimpl(tryfirst=True)
def pytest_runtest_makereport(item: Item, call: CallInfo[None]) -> None:
- if isinstance(item, TestCaseFunction):
- if item._excinfo:
- call.excinfo = item._excinfo.pop(0)
- try:
- del call.result
- except AttributeError:
- pass
-
+ if isinstance(item, TestCaseFunction):
+ if item._excinfo:
+ call.excinfo = item._excinfo.pop(0)
+ try:
+ del call.result
+ except AttributeError:
+ pass
+
unittest = sys.modules.get("unittest")
if (
unittest
@@ -355,49 +355,49 @@ def pytest_runtest_makereport(item: Item, call: CallInfo[None]) -> None:
lambda: pytest.skip(str(excinfo.value)), call.when
)
call.excinfo = call2.excinfo
-
+
# Twisted trial support.
-
-
-@hookimpl(hookwrapper=True)
+
+
+@hookimpl(hookwrapper=True)
def pytest_runtest_protocol(item: Item) -> Generator[None, None, None]:
- if isinstance(item, TestCaseFunction) and "twisted.trial.unittest" in sys.modules:
+ if isinstance(item, TestCaseFunction) and "twisted.trial.unittest" in sys.modules:
ut: Any = sys.modules["twisted.python.failure"]
- Failure__init__ = ut.Failure.__init__
- check_testcase_implements_trial_reporter()
-
- def excstore(
- self, exc_value=None, exc_type=None, exc_tb=None, captureVars=None
- ):
- if exc_value is None:
- self._rawexcinfo = sys.exc_info()
- else:
- if exc_type is None:
- exc_type = type(exc_value)
- self._rawexcinfo = (exc_type, exc_value, exc_tb)
- try:
- Failure__init__(
- self, exc_value, exc_type, exc_tb, captureVars=captureVars
- )
- except TypeError:
- Failure__init__(self, exc_value, exc_type, exc_tb)
-
- ut.Failure.__init__ = excstore
- yield
- ut.Failure.__init__ = Failure__init__
- else:
- yield
-
-
+ Failure__init__ = ut.Failure.__init__
+ check_testcase_implements_trial_reporter()
+
+ def excstore(
+ self, exc_value=None, exc_type=None, exc_tb=None, captureVars=None
+ ):
+ if exc_value is None:
+ self._rawexcinfo = sys.exc_info()
+ else:
+ if exc_type is None:
+ exc_type = type(exc_value)
+ self._rawexcinfo = (exc_type, exc_value, exc_tb)
+ try:
+ Failure__init__(
+ self, exc_value, exc_type, exc_tb, captureVars=captureVars
+ )
+ except TypeError:
+ Failure__init__(self, exc_value, exc_type, exc_tb)
+
+ ut.Failure.__init__ = excstore
+ yield
+ ut.Failure.__init__ = Failure__init__
+ else:
+ yield
+
+
def check_testcase_implements_trial_reporter(done: List[int] = []) -> None:
- if done:
- return
- from zope.interface import classImplements
- from twisted.trial.itrial import IReporter
-
- classImplements(TestCaseFunction, IReporter)
- done.append(1)
+ if done:
+ return
+ from zope.interface import classImplements
+ from twisted.trial.itrial import IReporter
+
+ classImplements(TestCaseFunction, IReporter)
+ done.append(1)
def _is_skipped(obj) -> bool: