Pytest模拟for循环使用的Content Manager

我有一个脚本,用于检查给定文件中的一行是否以关键字开头,如果是,则返回该行的其余部分:

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()

现在我要编写一个测试。我发现这篇文章建议使用模仿_开放和补丁: How do I mock an open used in a with statement (using the Mock framework in Python)?

在其他地方,我发现了将多个Mocks放在列表中并使用它的建议。 我都尝试过:

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_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 =========================

如果在循环中调用,mock_open是否不会产生None值?

如果我不需要seek(0),则使用充满了Mock对象的列表的方法可能行得通。

ccwysxbf1 回答:Pytest模拟for循环使用的Content Manager

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3125638.html

大家都在问