Python 3.x Pytest模拟for循环使用的内容管理器
我有一个脚本,它检查给定文件中的一行是否以关键字开头,如果是,则返回该行的其余部分:Python 3.x Pytest模拟for循环使用的内容管理器,python-3.x,file,for-loop,pytest,Python 3.x,File,For Loop,Pytest,我有一个脚本,它检查给定文件中的一行是否以关键字开头,如果是,则返回该行的其余部分: 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() 现在
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()
现在我想写一个测试它。我发现这篇文章建议使用mock_open和patch:
在其他地方,我发现了建议将多个模拟放在一个列表中并使用它的建议。
我尝试了两种方法:
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"))
并收到以下错误消息:
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 =========================
test\u check\u file.py::test\u check\u file无
失败
test\u check\u file.py::test\u check\u file2失败
==================================故障========================
______________________测试检查文件_______________________
def test_check_文件():
使用补丁(“builtins.open”,mock_open(read_data=“flying flying”))作为mock_文件:
打开(“某些文件”、“r”)作为文件:
打印(Check.Check_文件(文件“flying”))
>断言检查。检查文件(文件“flying”)=“flying”
E AssertionError:assertnone=='flying'
无
E+‘飞行’
test_check_file.py:11:AssertionError
________________________测试检查文件2_________________________
def测试检查文件2():
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_mock 4.return_value=“circus circus”
my_mock=[my_mock1,my_mock2,my_mock3,my_mock4]
>打印(Check.Check文件(我的mock,“flying”))
测试检查文件。py:24:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
文件=[,],变量='flying'
def check_文件(文件,可变):
>file.seek(0)
E AttributeError:“list”对象没有属性“seek”
检查\u file.py:4:AttributeError
===============================2在0.54秒内失败=========================
有没有一种方法可以使mock_open在循环中调用时不产生None值
如果我不需要seek(0),那么使用满是Mock对象的列表的方法可能会起作用