我有以下MWE(实际代码更大,但这是我要测试的部分):
def processFile():
fileToProcess = "localFile.txt"
try:
with open(fileToProcess, "rb") as f:
# Process file here
except EnvironmentError:
# Handle Exception
我的问题是:
我如何测试上面的功能,以测试是否抛出异常。我对测试的理解(非常有限)是,人们在断言的同时正在测试中调用该函数并导致“失败”的发生。
def testProcessFile(self):
self.assertRaises(???)
但是,如何在不实际删除文件的情况下使文件的存在无效?谢谢!
最佳答案
只需将文件的路径更改为您知道该文件不存在的目录
def processFile():
try:
with open("fake_dir/localFile.txt", "rb") as f:
# Process file here
except EnvironmentError:
# Handle Exception