Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.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
Python 单元测试覆盖率报告不包括模拟方法_Python_Unit Testing_Mocking_Code Coverage_Coverage.py - Fatal编程技术网

Python 单元测试覆盖率报告不包括模拟方法

Python 单元测试覆盖率报告不包括模拟方法,python,unit-testing,mocking,code-coverage,coverage.py,Python,Unit Testing,Mocking,Code Coverage,Coverage.py,我正在用Python为一个公司项目编写单元测试。我们使用Django作为框架,使用coverage.py跟踪进度。最近,我开始添加-m,看看我遗漏了哪些行。所以我使用的命令是: “覆盖率运行--source='app1,app2'manage.py测试和覆盖率报告-m” 其中一种方法的示例: def _read_from_disk(self, excel_kwargs): """ Read excel file from disk and apply excel_kwargs.

我正在用Python为一个公司项目编写单元测试。我们使用Django作为框架,使用coverage.py跟踪进度。最近,我开始添加-m,看看我遗漏了哪些行。所以我使用的命令是:

“覆盖率运行--source='app1,app2'manage.py测试和覆盖率报告-m”

其中一种方法的示例:

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("data.excel.BaseExcelReader._read_from_disk")
def test__read_from_disk_compare_data_frames(self, df_mock_svd):
    df_mock_svd.return_value = pd.read_excel(data_frame_to_excel_test(self.excel_data, "SvD Data"))

    df = self.test_reader._read_from_disk(self.excel_kwargs_svd)
    df_mock_svd.assert_called_once_with(self.excel_kwargs_svd)
    pd.testing.assert_frame_equal(df, pd.DataFrame({"test_id": [1, 2, 3, 4, 5]}))

问题是,当我运行该命令时,覆盖率报告不会将该方法计算为已覆盖。我想这是我在这里的第一个问题,因此如果我没有正确说明问题,我很抱歉。

这是因为它没有涵盖在内。您基本上用另一个实现替换它,并测试替换。如果你改为补丁
read\u excel
,它就会被覆盖。也许我需要将问题改为如何@patch for pd.excel\u read.@patch(“raven.data.excel.BaseExcelReader.\u read_from_disk.read\u excel”)或@patch(“raven.data.excel.BaseExcelReader.\u read_from_disk.pd.read\u excel”)如果我以这种方式尝试,我会收到错误:AttributeError:没有“read_excel”属性。如果您有其他问题(例如,如何模拟xyz),请写一个新问题-当前问题已在评论中得到回答(由@NedBatchelder和我自己回答,例如,报道报告是正确的)。评论中没有单独的问题,也不太可读。