所以我正在练习一些单元测试,我正在尝试检查For循环内的输出。这是我的运行代码
def main():
for i in range(100):
print("Argh!")
非常基础,现在这是我的测试代码。
import unittest
from unittest import mock # possibly "from unittest import mock" depending on version.
from RunFile import main
class TestMain(unittest.TestCase):
def test_main(self):
with mock.patch.object(main(), 'print') as mock_print:
main()
expected_calls = [mock.call('Argh!') for _ in range(100)]
mock_print.assert_has_calls(expected_calls)
if __name__ == '__main__':
unittest.main()
以下是我收到的错误消息。我不确定如何解决这个问题。 更新:这是完整的追溯
======================================================================
ERROR: test_main (__main__.TestMain)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:/Users/jsalce/Documents/Testsuites/IfStatements/Testsuite.py", line 9, in test_main
with mock.patch.object(RunFile, 'print') as mock_print:
File "C:\Python33\lib\unittest\mock.py", line 1148, in __enter__
original, local = self.get_original()
File "C:\Python33\lib\unittest\mock.py", line 1122, in get_original
"%s does not have the attribute %r" % (target, name)
AttributeError: <module 'RunFile' from 'C:\\Users\\jsalce\\Documents\\Testsuites\\IfStatements\\RunFile.py'> does not have the attribute 'print'
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
提前谢谢大家!
答案 0 :(得分:2)
一般来说,对于mock.patch.object
,您希望修补一些您可以轻松处理的内容 - 例如模块或类。通常,您需要修补比您想要替换的更高级别的内容。例如,如果要修补模块foo
中的bar
功能,则需要mock.patch.object(bar, 'foo')
。
在您的情况下,从技术上讲,print
是builtin
,但您可以在您使用它的模块上进行修补。这将添加一个RunFile.print
“方法”(实际上是一个模拟),您可以测试断言。显然,由于print
实际上并不存在于模块中,因此我们需要添加create=True
以告知mock
创建RunFile.print
,因为它尚不存在。考虑到这一点,我会将unittest重新编写为:
import RunFile
class TestMain(unittest.TestCase):
def test_main(self):
with mock.patch.object(RunFile, 'print', create=True) as mock_print:
RunFile.main()
expected_calls = [mock.call('Argh!') for _ in range(100)]
mock_print.assert_has_calls(expected_calls)
if __name__ == '__main__':
unittest.main()