您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python unittest howto

python unittest howto

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

一般来说,在这种情况下,我提取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的建议,您应该阅读一些有关单元测试的更深入的材料。

python 2022/1/1 18:51:21 有390人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶