我正在编写一个方法,该方法使用 lxml.etree 中 ElementTree 类的 write 方法。在编写我的测试时,我想模拟一下,这样单元测试就不会向我的驱动器写一堆东西。
我文件中的代码看起来像这样
# myapp\gla.py
from lxml.etree import Element, ElementTree
def my_func(element):
root = Element(element)
xml = ElementTree(root)
xml.write('path_to_file')
测试看起来像这样:
# tests\test_gla.py
from unittest import patch
from myapp.gla import my_func
@patch('myapp.gla.ElementTree.write')
def test_my_func(self, mock_write):
my_func('rootElement')
mock_write.assert_called_once()
我明白了
Traceback (most recent call last):
File "C:\Anaconda2\envs\py36\lib\unittest\mock.py", line 1171, in patched
arg = patching.__enter__()
File "C:\Anaconda2\envs\py36\lib\unittest\mock.py", line 1243, in __enter__
original, local = self.get_original()
File "C:\Anaconda2\envs\py36\lib\unittest\mock.py", line 1217, in get_original
"%s does not have the attribute %r" % (target, name)
AttributeError: <cyfunction ElementTree at 0x000001FFB4430BC8> does not have the attribute 'write'
桃花长相依
动漫人物
相关分类