Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mocking方法pandas.read_excel不适用于@patch Python_Python_Unit Testing_Mocking_Python Unittest_Patch - Fatal编程技术网

Mocking方法pandas.read_excel不适用于@patch Python

Mocking方法pandas.read_excel不适用于@patch Python,python,unit-testing,mocking,python-unittest,patch,Python,Unit Testing,Mocking,Python Unittest,Patch,我正在用Python为一个项目编写单元测试,最近遇到了decorator@patch的一个问题。我有以下方法需要测试 def _read_from_disk(self, excel_kwargs): """ Read excel file from disk and apply excel_kwargs. Args: excel_kwargs: Parameters for pandas.read_excel.

我正在用Python为一个项目编写单元测试,最近遇到了decorator@patch的一个问题。我有以下方法需要测试

def _read_from_disk(self, excel_kwargs):
        """
        Read excel file from disk and apply excel_kwargs.

        Args:
            excel_kwargs: Parameters for pandas.read_excel.

        Returns:
            DataFrame or dict of DataFrames.
        """
        return pd.read_excel(self.location, **excel_kwargs)
我的测试方法结构是

 @patch("program.data.excel.BaseExcelReader._read_from_disk.pd.read_excel")
    def test___read_from_disk(self, mock_df):
        mock_df.return_value = pd.DataFrame({"test_id": [1, 2, 3, 4, 5]})
        return_df = self.test_reader._read_from_disk(self.excel_kwargs_svd)
        pd.testing.assert_frame_equal(return_df, pd.DataFrame({"test_id": [1, 2, 3, 4, 5]}))
这给了我以下错误:

ModuleNotFoundError:没有名为“program.data.excel.BaseExcelReader”的模块program.data.excel不是一个包


请注意,测试方法只是一个示例。这个问题的目的是找到一种模仿熊猫的方法。用@patch阅读excel。谢谢

您可以直接修补“pandas.read\u excel”而不是“program.data.excel.BaseExcelReader.\u read\u from\u disk.pd.read\u excel”

代码:

@patch("pandas.read_excel")
def test___read_from_disk(self, mock_df):
    mock_df.return_value = pd.DataFrame({"test_id": [1, 2, 3, 4, 5]})
    return_df = self.test_reader._read_from_disk(self.excel_kwargs_svd)
    pd.testing.assert_frame_equal(return_df, pd.DataFrame({"test_id": [1, 2, 3, 4, 5]}))

那么您的文件名是
program/data/excel.py
还是其他什么?实际上,补丁肯定是错误的,因为它试图修补函数中定义的
pd
——我猜它是在模块中导入的。请显示测试代码和相关导入的文件名/结构。熊猫的导入为“导入熊猫为pd”,文件结构为补丁装饰程序“program.data.excel.BaseExcelReader.\u从磁盘读取”。我找不到如何使用@patch模拟熊猫的示例。文件名为program/data/excel.pyIt与@patch一起工作(“program.data.excel.pd.read_excel”)。非常感谢。你让我想到了文件中的路径。啊,这就是我所想的——很高兴我能帮上忙!