【发布时间】:2019-08-09 22:41:03
【问题描述】:
我正在使用 pytest 编写一些单元测试,并且想知道测试“依赖”函数的最佳方法是什么。假设我有两个功能:
def set_file(filename, filecontents):
# stores file as key in memcache
def get_file(filename):
# returns the contents of the filename if it exists in cache
目前,我有一个看起来像这样的“快乐路径”单元测试:
def test_happy_path():
assert not get_file('unit test') # check that return of non-existent file is None
set_file('unit test', 'test content') # set file contents
assert get_file('unit test') == 'test content' # check that return matches input
我的问题是这种方法是否有效?在测试get_file 时,我是否应该尝试模拟set_file 的数据以进行没有set file 创建的依赖项的单元测试?如果是这样,我将如何模拟它,尤其是因为 set_file 正在使用 pymemcached?
【问题讨论】:
-
对我来说,你的
test_happy_path似乎是有效的......
标签: python unit-testing mocking pytest