Merge pull request #4090 from asottile/faster_tests

Improve performance of ~3 of the slowest tests
This commit is contained in:
Anthony Sottile 2018-10-08 17:07:10 -07:00 committed by GitHub
commit e8c10d4a98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 27 deletions

View File

@ -31,6 +31,14 @@ failsonjython = pytest.mark.xfail("sys.platform.startswith('java')")
pytest_version_info = tuple(map(int, pytest.__version__.split(".")[:3])) pytest_version_info = tuple(map(int, pytest.__version__.split(".")[:3]))
@pytest.fixture
def limited_recursion_depth():
before = sys.getrecursionlimit()
sys.setrecursionlimit(150)
yield
sys.setrecursionlimit(before)
class TWMock(object): class TWMock(object):
WRITE = object() WRITE = object()
@ -239,7 +247,7 @@ class TestTraceback_f_g_h(object):
raise RuntimeError("hello") raise RuntimeError("hello")
f(n - 1) f(n - 1)
excinfo = pytest.raises(RuntimeError, f, 100) excinfo = pytest.raises(RuntimeError, f, 25)
monkeypatch.delattr(excinfo.traceback.__class__, "recursionindex") monkeypatch.delattr(excinfo.traceback.__class__, "recursionindex")
repr = excinfo.getrepr() repr = excinfo.getrepr()
assert "RuntimeError: hello" in str(repr.reprcrash) assert "RuntimeError: hello" in str(repr.reprcrash)
@ -1341,11 +1349,13 @@ def test_cwd_deleted(testdir):
assert "INTERNALERROR" not in result.stdout.str() + result.stderr.str() assert "INTERNALERROR" not in result.stdout.str() + result.stderr.str()
@pytest.mark.usefixtures("limited_recursion_depth")
def test_exception_repr_extraction_error_on_recursion(): def test_exception_repr_extraction_error_on_recursion():
""" """
Ensure we can properly detect a recursion error even Ensure we can properly detect a recursion error even
if some locals raise error on comparison (#2459). if some locals raise error on comparison (#2459).
""" """
from _pytest.pytester import LineMatcher
class numpy_like(object): class numpy_like(object):
def __eq__(self, other): def __eq__(self, other):
@ -1361,15 +1371,10 @@ def test_exception_repr_extraction_error_on_recursion():
def b(x): def b(x):
return a(numpy_like()) return a(numpy_like())
try: with pytest.raises(RuntimeError) as excinfo:
a(numpy_like()) a(numpy_like())
except: # noqa
from _pytest._code.code import ExceptionInfo
from _pytest.pytester import LineMatcher
exc_info = ExceptionInfo() matcher = LineMatcher(str(excinfo.getrepr()).splitlines())
matcher = LineMatcher(str(exc_info.getrepr()).splitlines())
matcher.fnmatch_lines( matcher.fnmatch_lines(
[ [
"!!! Recursion error detected, but an error occurred locating the origin of recursion.", "!!! Recursion error detected, but an error occurred locating the origin of recursion.",
@ -1379,22 +1384,17 @@ def test_exception_repr_extraction_error_on_recursion():
) )
@pytest.mark.usefixtures("limited_recursion_depth")
def test_no_recursion_index_on_recursion_error(): def test_no_recursion_index_on_recursion_error():
""" """
Ensure that we don't break in case we can't find the recursion index Ensure that we don't break in case we can't find the recursion index
during a recursion error (#2486). during a recursion error (#2486).
""" """
try:
class RecursionDepthError(object): class RecursionDepthError(object):
def __getattr__(self, attr): def __getattr__(self, attr):
return getattr(self, "_" + attr) return getattr(self, "_" + attr)
with pytest.raises(RuntimeError) as excinfo:
RecursionDepthError().trigger RecursionDepthError().trigger
except: # noqa assert "maximum recursion" in str(excinfo.getrepr())
from _pytest._code.code import ExceptionInfo
exc_info = ExceptionInfo()
assert "maximum recursion" in str(exc_info.getrepr())
else:
assert 0

View File

@ -45,7 +45,7 @@ def test_change_level_undo(testdir):
assert 0 assert 0
""" """
) )
result = testdir.runpytest_subprocess() result = testdir.runpytest()
result.stdout.fnmatch_lines(["*log from test1*", "*2 failed in *"]) result.stdout.fnmatch_lines(["*log from test1*", "*2 failed in *"])
assert "log from test2" not in result.stdout.str() assert "log from test2" not in result.stdout.str()