使用pytest,当我运行示例测试(创建您的第一个测试)时,我得到了预期的正常输出。当我运行类测试(在一个类中对多个测试进行分组)时,我得到的输出似乎是pytest的调用跟踪。
创建您的第一个测试:
import pytest
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
$ pytest
============================= test session starts
platform win32 -- Python 3.6.4, pytest-3.9.1, py-1.7.0, pluggy-0.8.0
rootdir: C:\Users\xxx\dev\tests, inifile:
collected 1 item
mytest.py F [100%]
================================== FAILURES
_________________________________ test_answer
def test_answer():
> assert func(3) == 5
E assert 4 == 5
E + where 4 = func(3)
mytest.py:7: AssertionError
========================== 1 failed in 0.11 seconds
现在,当我将测试分组在班级中...
将多个测试分组在一个班级
import pytest
class TestMethod(object):
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
$ pytest
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.9.1, py-1.7.0, pluggy-0.8.0
rootdir: C:\Users\xxx\dev\tests, inifile:
collected 1 item
mytest.py F [100%]
================================== FAILURES ===================================
___________________________ TestMethod.test_answer ____________________________
self = <CallInfo when='call' exception: test_answer() takes 0 positional arguments but 1 was given>
func = <function call_runtest_hook.<locals>.<lambda> at 0x0000000003A0E158>
when = 'call', treat_keyboard_interrupt_as_exception = False
def __init__(self, func, when, treat_keyboard_interrupt_as_exception=False):
#: context of invocation: one of "setup", "call",
#: "teardown", "memocollect"
self.when = when
self.start = time()
try:
> self.result = func()
..\..\..\lib\site-packages\_pytest\runner.py:206:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\..\..\lib\site-packages\_pytest\runner.py:188: in <lambda>
lambda: ihook(item=item, **kwds),
..\..\..\lib\site-packages\pluggy\hooks.py:284: in __call__
return self._hookexec(self, self.get_hookimpls(), kwargs)
..\..\..\lib\site-packages\pluggy\manager.py:67: in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
..\..\..\lib\site-packages\pluggy\manager.py:61: in <lambda>
firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
..\..\..\lib\site-packages\_pytest\runner.py:116: in pytest_runtest_call
item.runtest()
..\..\..\lib\site-packages\_pytest\python.py:1435: in runtest
self.ihook.pytest_pyfunc_call(pyfuncitem=self)
..\..\..\lib\site-packages\pluggy\hooks.py:284: in __call__
return self._hookexec(self, self.get_hookimpls(), kwargs)
..\..\..\lib\site-packages\pluggy\manager.py:67: in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
..\..\..\lib\site-packages\pluggy\manager.py:61: in <lambda>
firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
pyfuncitem = <Function 'test_answer'>
@hookimpl(trylast=True)
def pytest_pyfunc_call(pyfuncitem):
testfunction = pyfuncitem.obj
if pyfuncitem._isyieldedfunction():
testfunction(*pyfuncitem._args)
else:
funcargs = pyfuncitem.funcargs
testargs = {}
for arg in pyfuncitem._fixtureinfo.argnames:
testargs[arg] = funcargs[arg]
> testfunction(**testargs)
E TypeError: test_answer() takes 0 positional arguments but 1 was given
..\..\..\lib\site-packages\_pytest\python.py:166: TypeError
========================== 1 failed in 0.21 seconds ===========================