python:如何模拟os.statvfs返回posix.statvfs_结果数据类型

2024-04-29 15:08:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个单元测试用例,下面是原始函数的部分代码:

def disk_space(something, otherthing):
    """
    Comments
    """
    dir_map = something['dir_map']
    for dir, expected_free_gb in dir_map.items():
        try:
            result = os.statvfs(dir)
            block_size = result.f_frsize
            avail_blocks = result.f_bavail
            giga = 1024 * 1024 * 1024
            free_size = avail_blocks * block_size / giga
            if free_size >= float(expected_free_gb):
                print "No need to clear space"
            else:
                print "Clear some space"

单元测试功能:

@patch('os.statvfs', return_value=posix.statvfs_result(f_frsize=4096, f_bavail=2599495))
def test_disk_space(self, os_statvfs):
    something
    something

现在,如果我在posix.statvfs_result数据类型中返回值,它会给出错误:

TypeError: Required argument 'sequence' (pos 1) not found

如果我给return_value=(f_frsize=4096, f_bavail=2599495)它说:

str() is not having f_frsize.


Tags: freemapsizeosdefdirspaceresult
1条回答
网友
1楼 · 发布于 2024-04-29 15:08:54

使用下面的函数

@patch('os.statvfs', return_value=os.statvfs_result((4096, 4096, 1909350, 1491513, 1394521, 971520, 883302, 883302, 0, 255)))
def test_disk_space(self, os_statvfs):
    something
    something

这些值与下面的字段相对应,因此请相应地进行更改。另外,请注意,值应位于双大括号内

posix.statvfs_result(f_bsize=4096, f_frsize=4096, f_blocks=1909350L, f_bfree=1491513L,
f_bavail=1394521L, f_files=971520L, f_ffree=883302L, f_fvail=883302L, f_flag=0,
f_namemax=255)

相关问题 更多 >