我想对我的应用程序的一个组件进行单元测试。代码看起来有点像下面。
def read_content_generator(myfile):
for line in open(myfile):
# do some string manipulation.
yield result
我遇到的问题是我无法在open()
循环中模仿for
功能。
我的目标是unittest
这样:(我知道这段代码不对,但它只是我想要做的一个例子):
def test_openiteration(self):
with mock.patch('open') as my_openmock:
my_openmock.return_value = ['1','2','3']
response = myfunction()
self.assertEquals([1,2,3], response)
答案 0 :(得分:7)
您可以模拟open()
以返回StringIO对象。
<强> mymodule.py:强>
def read_content_generator(myfile):
with open(myfile) as f:
for line in f:
yield '<{}>'.format(line)
请注意,我在那里使用了with statement。
<强> test_mymodule.py:强>
import io
import unittest
import unittest.mock as mock
import mymodule
class Tests(unittest.TestCase):
def test_gen(self):
fake_file = io.StringIO('foo\nbar\n')
with mock.patch('mymodule.open', return_value=fake_file, create=True):
result = list(mymodule.read_content_generator('filename'))
self.assertEqual(result, ['<foo\n>' , '<bar\n>'])
适用于python3.4。
起初我尝试使用mock.mock_open(read_data='1\n2\n3\n')
,但迭代支持似乎已被打破。
答案 1 :(得分:1)
有两种简单的选择:
更改read_content_generator
以获取文件,而不是文件名,并使用io.StringIO
进行模拟。
制作一个临时文件。有很好的模块。