I have a script that checks if a line in a given file starts with a key word and returns the rest of the line if so:
class Check():
def check_file(file, varible):
file.seek(0)
for line in file:
if varible.strip() in line.strip():
return line.strip()[len(varible):].strip()
Now I want to write a test it. I found this post suggesting to use mock_open and patch: How do I mock an open used in a with statement (using the Mock framework in Python)?
Somewhere else I found the suggesting to put multiple Mocks in a list and use that. I tried both:
import pytest
from unittest.mock import patch, mock_open, Mock
from check_file import Check
def test_check_file():
with patch("builtins.open", mock_open(read_data="flying flying")) as mock_file:
with open("some_file", "r") as file:
print(Check.check_file(file, "flying"))
assert Check.check_file(file, "flying") == "flying"
def test_check_file2():
my_mock1 = Mock()
my_mock1.return_value = "monthy monthy"
my_mock2 = Mock()
my_mock2.return_value = "python's python's"
my_mock3 = Mock()
my_mock3.return_value = "flying flying"
my_mock4 = Mock()
my_mock4.return_value = "circus circus"
my_mock = [ my_mock1, my_mock2, my_mock3, my_mock4 ]
print(Check.check_file(my_mock, "flying"))
And got this error messages:
test_check_file.py::test_check_file None
FAILED
test_check_file.py::test_check_file2 FAILED
====================== FAILURES ========================
______________________ test_check_file _______________________
def test_check_file():
with patch("builtins.open", mock_open(read_data="flying flying")) as mock_file:
with open("some_file", "r") as file:
print(Check.check_file(file, "flying"))
> assert Check.check_file(file, "flying") == "flying"
E AssertionError: assert None == 'flying'
E -None
E +'flying'
test_check_file.py:11: AssertionError
________________________ test_check_file2 _________________________
def test_check_file2():
my_mock1 = Mock()
my_mock1.return_value = "monthy monthy"
my_mock2 = Mock()
my_mock2.return_value = "python's python's"
my_mock3 = Mock()
my_mock3.return_value = "flying flying"
my_mock4 = Mock()
my_mock4.return_value = "circus circus"
my_mock = [ my_mock1, my_mock2, my_mock3, my_mock4 ]
> print(Check.check_file(my_mock, "flying"))
test_check_file.py:24:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
file = [<Mock id='140386010077000'>, <Mock id='140386010077056'>, <Mock id='140386010077224'>, <Mock id='140386010077112'>], varible = 'flying'
def check_file(file, varible):
> file.seek(0)
E AttributeError: 'list' object has no attribute 'seek'
check_file.py:4: AttributeError
====================== 2 failed in 0.54 seconds =========================
Is there a way that mock_open does not produce None values if called in a loop?
The approach with the list full of Mock object may have worked if I did not need seek(0).