This is my first time using py.test. All feedback for this test case is much appreciated.
import pytest
from mock import mock_open, patch
def get_file_contents(file_data):
with patch.object('builtins.open', mock_open(read_data=file_data)) as mock:
with open('mocked_file') as f:
return (line for line in f.readlines())
@pytest.fixture(scope="module")
def text_parser(request):
from tparse import TextParser
file_data = getattr(request.module, 'file_contents')
tparse = TextParser(file_data)
def fin():
tparse.close()
request.addfinalizer(fin)
return tparse
def test_get_system_entry(self):
file_data = 'dc nyc server server001 ipaddress 10.10.10.10'
file_iterable = get_file_contents(file_data)
assert file_iterable == file_data