python unittest howto


问题内容

我想知道如何对以下模块进行单元测试。

def download_distribution(url, tempdir):
    """ Method which downloads the distribution from PyPI """
    print "Attempting to download from %s" % (url,)

    try:
        url_handler = urllib2.urlopen(url)
        distribution_contents = url_handler.read()
        url_handler.close()

        filename = get_file_name(url)

        file_handler = open(os.path.join(tempdir, filename), "w")
        file_handler.write(distribution_contents)
        file_handler.close()
        return True

    except ValueError, IOError:
        return False

问题答案:

单元测试提议者会告诉您单元测试应该是自包含的,也就是说,他们不应访问网络或文件系统(尤其是不在写入模式下)。网络和文件系统测试不在单元测试的范围内(尽管您可能需要对其进行集成测试)。

一般来说,在这种情况下,我提取urllib和文件编写代码以分离函数(不会进行单元测试),并在单元测试期间注入模拟函数。

即(为便于阅读,略有缩写):

def get_web_content(url):
    # Extracted code
    url_handler = urllib2.urlopen(url)
    content = url_handler.read()
    url_handler.close()
    return content

def write_to_file(content, filename, tmpdir):
    # Extracted code
    file_handler = open(os.path.join(tempdir, filename), "w")
    file_handler.write(content)
    file_handler.close()

def download_distribution(url, tempdir):
    # Original code, after extractions
    distribution_contents = get_web_content(url)
    filename = get_file_name(url)
    write_to_file(distribution_contents, filename, tmpdir)
    return True

并且,在测试文件上:

import module_I_want_to_test

def mock_web_content(url):
    return """Some fake content, useful for testing"""
def mock_write_to_file(content, filename, tmpdir):
    # In this case, do nothing, as we don't do filesystem meddling while unit testing
    pass

module_I_want_to_test.get_web_content = mock_web_content
module_I_want_to_test.write_to_file = mock_write_to_file

class SomeTests(unittest.Testcase):
    # And so on...

然后我赞同Daniel的建议,您应该阅读一些有关单元测试的更深入的材料。