Python 2.7 如何在unitest框架中模拟创建文本文件python2.7?

Python 2.7 如何在unitest框架中模拟创建文本文件python2.7?,python-2.7,mocking,patch,python-unittest,Python 2.7,Mocking,Patch,Python Unittest,我有一个函数,首先检查txt文件是否存在,如果不存在,则创建一个。如果txt文件已经存在,它将读取信息。我试图编写单元测试来检查函数的逻辑是否正确。我想修补一些东西,比如文件的存在、文件的创建和文件的读取。 要测试的函数如下所示: import json import os.path def read_create_file(): filename = 'directory/filename.txt' info_from_file = [] if os.path.e

我有一个函数,首先检查txt文件是否存在,如果不存在,则创建一个。如果txt文件已经存在,它将读取信息。我试图编写单元测试来检查函数的逻辑是否正确。我想修补一些东西,比如文件的存在、文件的创建和文件的读取。 要测试的函数如下所示:

import json
import os.path

def read_create_file():

    filename = 'directory/filename.txt'
    info_from_file = []

    if os.path.exists(filename):

        with open(filename, 'r') as f:
            content = f.readlines()
            for i in range(len(content)):
                info_from_file.append(json.loads(content[i]))
        return info_from_file

    else:
        with open(filename, 'w') as f:
            pass

        return []
import unittest
import mock
from mock import patch


class TestReadCreateFile(unittest.TestCase):

    def setUp(self):
        pass

    def function(self):
        return read_create_file()

    @patch("os.path.exists", return_value=False)
    @mock.patch('directory/filename.txt.open', new=mock.mock_open())
    def test_file_does_not_exist(self, mock_existence, mock_open_patch):
        result = self.function()
        self.assertEqual(result, (True, []))
import unittest
import mock
from mock import patch

@patch("os.path.exists", return_value=False)
def test_file_not_exist_yet(self, mock_existence):
    m = mock.mock_open()
    with patch('__main__.open', m, create=True):
        handle = open('directory/filename.txt', 'w')
    result = self.function()

    self.assertEqual(result, (True, {}))
单元测试如下所示:

import json
import os.path

def read_create_file():

    filename = 'directory/filename.txt'
    info_from_file = []

    if os.path.exists(filename):

        with open(filename, 'r') as f:
            content = f.readlines()
            for i in range(len(content)):
                info_from_file.append(json.loads(content[i]))
        return info_from_file

    else:
        with open(filename, 'w') as f:
            pass

        return []
import unittest
import mock
from mock import patch


class TestReadCreateFile(unittest.TestCase):

    def setUp(self):
        pass

    def function(self):
        return read_create_file()

    @patch("os.path.exists", return_value=False)
    @mock.patch('directory/filename.txt.open', new=mock.mock_open())
    def test_file_does_not_exist(self, mock_existence, mock_open_patch):
        result = self.function()
        self.assertEqual(result, (True, []))
import unittest
import mock
from mock import patch

@patch("os.path.exists", return_value=False)
def test_file_not_exist_yet(self, mock_existence):
    m = mock.mock_open()
    with patch('__main__.open', m, create=True):
        handle = open('directory/filename.txt', 'w')
    result = self.function()

    self.assertEqual(result, (True, {}))
错误:导入错误:不支持按文件名导入

或者像这样:

import json
import os.path

def read_create_file():

    filename = 'directory/filename.txt'
    info_from_file = []

    if os.path.exists(filename):

        with open(filename, 'r') as f:
            content = f.readlines()
            for i in range(len(content)):
                info_from_file.append(json.loads(content[i]))
        return info_from_file

    else:
        with open(filename, 'w') as f:
            pass

        return []
import unittest
import mock
from mock import patch


class TestReadCreateFile(unittest.TestCase):

    def setUp(self):
        pass

    def function(self):
        return read_create_file()

    @patch("os.path.exists", return_value=False)
    @mock.patch('directory/filename.txt.open', new=mock.mock_open())
    def test_file_does_not_exist(self, mock_existence, mock_open_patch):
        result = self.function()
        self.assertEqual(result, (True, []))
import unittest
import mock
from mock import patch

@patch("os.path.exists", return_value=False)
def test_file_not_exist_yet(self, mock_existence):
    m = mock.mock_open()
    with patch('__main__.open', m, create=True):
        handle = open('directory/filename.txt', 'w')
    result = self.function()

    self.assertEqual(result, (True, {}))
错误: IOError:[Errno 2]没有这样的文件或目录:“directory/filename.txt”

作为一名新手,我似乎无法找到解决方案,非常感谢您的帮助


谢谢你

你在嘲笑os.path.exists是错误的。当您修补时,您将从测试文件中修补

@patch("path_to_method_under_test.path.exists", return_value=False)
def test_file_not_exist_yet(self, mock_existence):