Ho il seguente test-codice verifica un innalzamento eccezione in una funzione. Mi aspetto che il test passi, ma viene invece indicato un errore. Ecco il codice di prova:assertRaises fallisce, anche la richiamabile solleva l'eccezione desiderata (pitone, UNITEST)
import unittest
# define a user-defined exception
class MyException(Exception):
def __str__(self):
return repr("ERROR: Just raised my exception!")
# this is my main class with a method raising this exception
class MyMainObject(object):
def func(self):
raise MyException()
# the test class
class TestConfig(unittest.TestCase):
def test_1(self):
other = MyMainObject()
self.assertRaises(MyException, other.func())
# calling the test
if __name__ == '__main__':
unittest.main()
Quando other.func()
viene chiamato nella dichiarazione assert, MyException
è sollevato (può essere controllato facilmente). Così, il test assertRaises
deve superare la prova, come other.func()
failes con MyException
, MA:
....
MyException: 'ERROR: Just raised my exception!'
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
Non vedo qualcosa di sbagliato, quindi vorrei apprezzare alcuni input su questo problema.
si dovrebbe notare che è un errore, non un fallimento! la tua eccezione non è stata nemmeno catturata. –