Simplify IndexError test.

This commit is contained in:
Ryan Wooden 2016-02-26 08:14:55 -04:00
parent cf9a09e988
commit d98d655094
3 changed files with 22 additions and 40 deletions

View File

@ -69,6 +69,7 @@ Ralf Schmitt
Raphael Pierzina Raphael Pierzina
Ronny Pfannschmidt Ronny Pfannschmidt
Ross Lawley Ross Lawley
Ryan Wooden
Samuele Pedroni Samuele Pedroni
Tom Viner Tom Viner
Trevor Bekolay Trevor Bekolay

View File

@ -30,7 +30,9 @@
* *
* * catch IndexError exceptions when getting exception source location. This fixes
pytest internal error for dynamically generated code (fixtures and tests)
where source lines are fake by intention
* *
@ -526,10 +528,6 @@
directories created by this fixture (defaults to $TEMP/pytest-$USER). directories created by this fixture (defaults to $TEMP/pytest-$USER).
Thanks Bruno Oliveira for the PR. Thanks Bruno Oliveira for the PR.
- catch IndexError exceptions when getting exception source location. This fixes
pytest internal error for dynamically generated code (fixtures and tests)
where source lines are fake by intention
2.7.2 (compared to 2.7.1) 2.7.2 (compared to 2.7.1)
============================= =============================

View File

@ -568,47 +568,30 @@ def test_makereport_getsource(testdir):
result.stdout.fnmatch_lines(['*else: assert False*']) result.stdout.fnmatch_lines(['*else: assert False*'])
def test_makereport_getsource_dynamic_code(testdir): def test_makereport_getsource_dynamic_code(testdir, monkeypatch):
"""Test that exception in dynamically generated code doesn't break getting the source line.""" """Test that exception in dynamically generated code doesn't break getting the source line."""
sub = testdir.mkdir("sub") import inspect
sub.join("__init__.py").write("") original_findsource = inspect.findsource
sub.join("fixtures.py").write(py.std.textwrap.dedent( def findsource(obj, *args, **kwargs):
""" # Can be triggered by dynamically created functions
import sys if obj.__name__ == 'foo':
import pytest raise IndexError()
import inspect return original_findsource(obj, *args, **kwargs)
monkeypatch.setattr(inspect, 'findsource', findsource)
def get_caller_module(depth=2): testdir.makepyfile("""
frame = sys._getframe(depth) import pytest
module = inspect.getmodule(frame)
if module is None:
return get_caller_module(depth=depth)
return module
def inject(): @pytest.fixture
context = {} def foo(missing):
exec(''' pass
import pytest
@pytest.fixture def test_fix(foo):
def foo(request): assert False
request.getfuncargvalue('sone') """)
''', context)
module = get_caller_module()
foo = context['foo']
module.foo = foo
inject()
"""))
sub.join("conftest.py").write("""from fixtures import foo""")
sub.join("test_dynamic.py").write(py.std.textwrap.dedent(
"""
def test_foo(foo):
pass
"""))
result = testdir.runpytest('-vv') result = testdir.runpytest('-vv')
assert 'INTERNALERROR' not in result.stdout.str() assert 'INTERNALERROR' not in result.stdout.str()
result.stdout.fnmatch_lines(['*else: assert False*']) result.stdout.fnmatch_lines(["*test_fix*", "*fixture*'missing'*not found*"])
def test_store_except_info_on_eror(): def test_store_except_info_on_eror():