For the test coverage I want to test also the exception block of this function that is in the file 'signalC':
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
So far I tried with this approach, but still cant go inside the exception block:
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)
Or I should instead raise the exception with a wrong input? Can anyone give any example? Thanks in advance