假设我有一个看起来像这样的方法:
from otherModule import B
def A():
for pair in [[1, 2], [3, 4]]:
B(*pair)
我有一个测试,看起来像:
class TestA(unittest.TestCase):
@patch("moduleA.B")
def test_A(self, mockB):
A()
mockB.assert_has_calls([
call(1, 2),
call(3, 4)
])
出于某种原因,我得到了一个AssertionError: Calls not found.
,因为它只注册了两次3,4
的呼叫。我正在做的事情看起来正确吗?
它工作正常,无法重现错误。
例如
moduleA.py
:
from otherModule import B
def A():
for pair in [[1, 2], [3, 4]]:
B(*pair)
otherModule.py
:
def B(x, y):
print(x, y)
test_moduleA.py
:
import unittest
from unittest.mock import patch, call
from moduleA import A
class TestA(unittest.TestCase):
@patch("moduleA.B")
def test_A(self, mockB):
A()
mockB.assert_has_calls([
call(1, 2),
call(3, 4)
])
if __name__ == '__main__':
unittest.main()
带有覆盖率报告的单元测试结果:
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Name Stmts Miss Cover Missing
--------------------------------------------------------------------------
src/stackoverflow/59179990/moduleA.py 4 0 100%
src/stackoverflow/59179990/otherModule.py 2 1 50% 2
src/stackoverflow/59179990/test_moduleA.py 9 0 100%
--------------------------------------------------------------------------
TOTAL 15 1 93%
蟒蛇版本:Python 3.7.5