我想知道如何对以下模块进行单元测试.
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
Run Code Online (Sandbox Code Playgroud)
模糊的问题.如果您只是在寻找一个通常使用Python倾斜进行单元测试的底漆,我推荐Mark Pilgrim的"Dive Into Python",其中有一章用Python进行单元测试.否则,您需要清除测试该代码的具体问题.
单元测试命题器会告诉您单元测试应该是自包含的,也就是说,它们不应该访问网络或文件系统(特别是在写入模式下).网络和文件系统测试超出了单元测试的范围(尽管您可能会对它们进行集成测试).
一般来说,对于这种情况,我会将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
Run Code Online (Sandbox Code Playgroud)
并且,在测试文件上:
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...
Run Code Online (Sandbox Code Playgroud)
然后我提出丹尼尔的建议,你应该阅读一些关于单元测试的更深入的资料.
| 归档时间: |
|
| 查看次数: |
1751 次 |
| 最近记录: |