1
功能對於測試覆蓋率我想也測試這個功能,那就是文件「signalC」中的異常塊:測試與模擬在Python中
class SignalC:
def readSignal(self, a):
try:
with open(os.path.join(self.newSubFolder, "my file" + '.csv'), 'a') as csvfile:
writer = csv.writer(csvfile, delimiter=',', quotechar='|',
quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
print 'Reading'
z = random.uniform(-0.1, 0.1)
readValue = z + setP[element]
writer.writerow([self.element + '-' + str(element+1),)
except IOError as message:
logging.error('Error in writing the csv file ' + str(message))
print(message.strerror)
raise IOError
到目前爲止,我這種方法試過,但仍然無法進入異常塊內:
def testReadSignal(self):
sc = signalC.SignalC()
a = [1, 1, 1]
with mock.patch("signalC.SignalC.readSignal", side_effect=IOError("IOError")):
self.assertRaises(IOError, sc.readSignal, a)
或者我應該不是引發異常與錯誤的輸入?任何人都可以舉例嗎?在此先感謝
感謝很多!我不知道爲什麼我沒有考慮公開提出錯誤 – Joe