有一个tmpdir固定装置,它将为您创建每个测试的临时目录。因此测试看起来像这样:
def writetoafile(fname): with open(fname, 'w') as fp: fp.write('Hellon')def test_writetofile(tmpdir): file = tmpdir.join('output.txt') writetoafile(file.strpath) # or use str(file) assert file.read() == 'Hellon'
在这里,您将代码重构为也不会进行硬编码,这是测试代码如何使您设计得更好的一个很好的例子。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)