Last active
March 2, 2019 19:37
-
-
Save krak3n/5884562 to your computer and use it in GitHub Desktop.
Mocking open for ConfigParser.RawConfigParser.readfp() for unit testing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
import six | |
from mock import patch | |
from six.moves import configparser as ConfigParser | |
data = """ | |
[section] | |
key = value | |
""" | |
if six.PY3: | |
func = 'builtins.open' | |
else: | |
func = '__builtin__.open' | |
patcher = patch(func, return_value=io.BytesIO(data)) | |
patcher.start() | |
parser = ConfigParser.RawConfigParser(allow_no_value=True) | |
parser.readfp(open('im_not_real.cfg')) | |
parser.items('section') | |
>>> [('key', 'value')] | |
patcher.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment