简体   繁体   中英

Mock for line in open(file):

I am wanting to unittest a component of my application. The code looks a little like below.

def read_content_generator(myfile):
    for line in open(myfile):
        # do some string manipulation.
        yield result

The problem I am having is that I cannot mock the open() functionality within a for loop.

What I am aiming for is a unittest like this: (I know this code is not right but its just an example of what I am trying to do):

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)

You can mock open() to return StringIO object.

mymodule.py:

def read_content_generator(myfile):
    with open(myfile) as f:
        for line in f:
            yield '<{}>'.format(line)

Note that I've used with statement there.

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>'])

Works for python3.4.

At first I tried to use mock.mock_open(read_data='1\\n2\\n3\\n') but iteration support seems to be broken.

There are two easy options available:

  • Change read_content_generator to take a file, not a file name, and mock that with an io.StringIO .

  • Make a temporary file. There are good modules for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM