py.test INTERNALERROR> IndexError:列表索引超出范围

时间:2015-12-05 16:14:22

标签: python python-3.x pytest

我正在研究Ubuntu 15.04:

平台linux - Python 3.4.3,pytest-2.8.3,py-1.4.31,pluggy-0.3.1 rootdir:/ home / barrios / code / testpytest,inifile:

在一个新的virtualenv中,我构建了以下项目结构:

myapp/
    __init__.py
    myapp.py
tests/
    tests.py
setup.py

在这里发表各种帖子后,我发现要么将PYTHONPATH设置为我的项目根目录,要么调用py.test:

PYTHONPATH=. py.test tests/tests.py

myapp.py看起来像这样:

class Foo:
    def __init__(self):
        self.bar = None

    def set_bar(self, val):
        self.bar = val

tests.py:

from myapp import Foo

class TestFoo:
    def setup_method(self, method):
        self.foo = Foo()

    def teardown_method(self):
        self.foo.bar = None

    def test_set_bar(self):
        self.foo.set_bar(7)
        assert self.foo.bar is 7

阅读this post我也把它放在__init __。py:

from .myapp import Foo

__all__ = ['Foo']

我能够做到

>>> from myapp import Foo

在python控制台和

$> pip install -e .

来自终端。

但是,py.test抱怨抛出一个内部错误:

(venv)barrios@tuxedo:~/code/testpytest$ PYTHONPATH=. py.test tests/tests.py
=============================================================== test session starts ===============================================================
platform linux -- Python 3.4.3, pytest-2.8.3, py-1.4.31, pluggy-0.3.1
rootdir: /home/barrios/code/testpytest, inifile:
collected 1 items

tests/tests.py .
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/main.py", line 90, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/main.py", line 121, in _main
INTERNALERROR>     config.hook.pytest_runtestloop(session=session)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/vendored_packages/pluggy.py", line 724, in __call__
INTERNALERROR>     return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/vendored_packages/pluggy.py", line 338, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/vendored_packages/pluggy.py", line 333, in <lambda>
INTERNALERROR>     _MultiCall(methods, kwargs, hook.spec_opts).execute()
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/vendored_packages/pluggy.py", line 596, in execute
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/main.py", line 146, in pytest_runtestloop
INTERNALERROR>     item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/vendored_packages/pluggy.py", line 724, in __call__
INTERNALERROR>     return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/vendored_packages/pluggy.py", line 338, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/vendored_packages/pluggy.py", line 333, in <lambda>
INTERNALERROR>     _MultiCall(methods, kwargs, hook.spec_opts).execute()
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/vendored_packages/pluggy.py", line 595, in execute
INTERNALERROR>     return _wrapped_call(hook_impl.function(*args), self.execute)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/vendored_packages/pluggy.py", line 253, in _wrapped_call
INTERNALERROR>     return call_outcome.get_result()
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/vendored_packages/pluggy.py", line 278, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/vendored_packages/pluggy.py", line 264, in __init__
INTERNALERROR>     self.result = func()
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/vendored_packages/pluggy.py", line 596, in execute
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/runner.py", line 65, in pytest_runtest_protocol
INTERNALERROR>     runtestprotocol(item, nextitem=nextitem)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/runner.py", line 77, in runtestprotocol
INTERNALERROR>     nextitem=nextitem))
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/runner.py", line 121, in call_and_report
INTERNALERROR>     report = hook.pytest_runtest_makereport(item=item, call=call)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/vendored_packages/pluggy.py", line 724, in __call__
INTERNALERROR>     return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/vendored_packages/pluggy.py", line 338, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/vendored_packages/pluggy.py", line 333, in <lambda>
INTERNALERROR>     _MultiCall(methods, kwargs, hook.spec_opts).execute()
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/vendored_packages/pluggy.py", line 595, in execute
INTERNALERROR>     return _wrapped_call(hook_impl.function(*args), self.execute)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/vendored_packages/pluggy.py", line 249, in _wrapped_call
INTERNALERROR>     wrap_controller.send(call_outcome)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/skipping.py", line 170, in pytest_runtest_makereport
INTERNALERROR>     rep = outcome.get_result()
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/vendored_packages/pluggy.py", line 278, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/vendored_packages/pluggy.py", line 264, in __init__
INTERNALERROR>     self.result = func()
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/vendored_packages/pluggy.py", line 596, in execute
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/runner.py", line 227, in pytest_runtest_makereport
INTERNALERROR>     style=item.config.option.tbstyle)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/python.py", line 740, in _repr_failure_py
INTERNALERROR>     style=style)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/_pytest/main.py", line 404, in _repr_failure_py
INTERNALERROR>     style=style, tbfilter=tbfilter)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/py/_code/code.py", line 412, in getrepr
INTERNALERROR>     return fmt.repr_excinfo(self)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/py/_code/code.py", line 590, in repr_excinfo
INTERNALERROR>     reprtraceback = self.repr_traceback(excinfo)
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/py/_code/code.py", line 577, in repr_traceback
INTERNALERROR>     last = traceback[-1]
INTERNALERROR>   File "/home/barrios/code/testpytest/venv/lib/python3.4/site-packages/py/_code/code.py", line 284, in __getitem__
INTERNALERROR>     val = super(Traceback, self).__getitem__(key)
INTERNALERROR> IndexError: list index out of range

我不太相信在py.test bugtracker上报告一个问题,或者我是否仍然做错了什么?

1 个答案:

答案 0 :(得分:2)

您的拆解方法缺少第二个参数,即method

def teardown_method(self, method):

仔细阅读文档: https://pytest.org/latest/xunit_setup.html