如何对与块设备交互的程序进行单元测试 [英] How to unit test program interacting with block devices

查看:27
本文介绍了如何对与块设备交互的程序进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序可以与 linux 上的块设备(/dev/sda 等)进行交互和更改.我正在使用各种外部命令(主要是来自 fdisk 和 GNU fdisk 包的命令)来控制设备.我已经创建了一个类,用作块设备的大多数基本操作的接口(有关信息,例如:它的大小?它安装在哪里?等)

I have a program that interacts with and changes block devices (/dev/sda and such) on linux. I'm using various external commands (mostly commands from the fdisk and GNU fdisk packages) to control the devices. I have made a class that serves as the interface for most of the basic actions with block devices (for information like: What size is it? Where is it mounted? etc.)

这是一种查询分区大小的方法:

Here is one such method querying the size of a partition:

def get_drive_size(device):
    """Returns the maximum size of the drive, in sectors.

    :device the device identifier (/dev/sda and such)"""

    query_proc = subprocess.Popen(["blockdev", "--getsz", device], stdout=subprocess.PIPE) 
    #blockdev returns the number of 512B blocks in a drive
    output, error = query_proc.communicate()
    exit_code = query_proc.returncode
    if exit_code != 0:
        raise Exception("Non-zero exit code", str(error, "utf-8")) #I have custom exceptions, this is slight pseudo-code

    return int(output) #should always be valid

所以这个方法接受一个块设备路径,并返回一个整数.测试将以 root 身份运行,因为整个程序最终都必须以 root 身份运行.

So this method accepts a block device path, and returns an integer. The tests will run as root, since this entire program will end up having to run as root anyway.

我应该尝试测试诸如这些方法之类的代码吗?如果是这样,如何?我可以尝试为每个测试创建和挂载图像文件,但这似乎是很多开销,并且本身可能容易出错.它需要块设备,所以我不能直接操作文件系统中的图像文件.

Should I try and test code such as these methods? If so, how? I could try and create and mount image files for each test, but this seems like a lot of overhead, and is probably error-prone itself. It expects block devices, so I cannot operate directly on image files in the file system.

我可以尝试嘲笑,正如一些答案所暗示的那样,但这感觉不够.如果我模拟 Popen 对象而不是输出,我似乎开始测试该方法的实现.在这种情况下,这是对正确的单元测试方法的正确评估吗?

I could try mocking, as some answers suggest, but this feels inadequate. It seems like I start to test the implementation of the method, if I mock the Popen object, rather than the output. Is this a correct assessment of proper unit-testing methodology in this case?

我在这个项目中使用了 python3,我还没有选择单元测试框架.在没有其他原因的情况下,我可能只会使用 Python 中包含的默认单元测试框架.

I am using python3 for this project, and I have not yet chosen a unit-testing framework. In the absence of other reasons, I will probably just use the default unittest framework included in Python.

推荐答案

您应该查看 mock 模块(我认为它现在是 Python 3 中 unittest 模块的一部分).

You should look into the mock module (I think it's part of the unittest module now in Python 3).

它使您无需依赖任何外部资源即可运行测试,同时让您可以控制模拟与代码的交互方式.

It enables you to run tests without the need to depened in any external resources while giving you control over how the mocks interact with your code.

我将从 Voidspace

这是一个例子:

import unittest2 as unittest
import mock

class GetDriveSizeTestSuite(unittest.TestCase):

  @mock.patch('path/to/original/file.subprocess.Popen')
  def test_a_scenario_with_mock_subprocess(self, mock_popen):
    mock_popen.return_value.communicate.return_value = ('Expected_value', '')
    mock_popen.return_value.returncode = '0'
    self.assertEqual('expected_value', get_drive_size('some device'))

这篇关于如何对与块设备交互的程序进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆