python - Error while testing the raise of self-defined exceptions (using assertRaises()) -
i creating tests python project. normal tests work fine, want test if in condition function raises self-defined exception. therefor want use assertraises(exception, function). ideas?
the function raises exception is:
def connect(comp1, comp2): if comp1 == comp2: raise e.invalidconnectionerror(comp1, comp2) ...
the exception is:
class invalidconnectionerror(exception): def __init__(self, connection1, connection2): self._connection1 = connection1 self._connection2 = connection2 def __str__(self): string = '...' return string
the test method following:
class testconnections(u.testcase): def test_connect_error(self): comp = c.powerconsumer('bus', true, 1000) self.assertraises(e.invalidconnectionerror, c.connect(comp, comp))
however following error:
error traceback (most recent call last): file "c:\users\t5ycxk\pycharmprojects\electricpowerdesign\test_component.py", line 190, in test_connect_error self.assertraises(e.invalidconnectionerror, c.connect(comp, comp)) file "c:\users\t5ycxk\pycharmprojects\electricpowerdesign\component.py", line 428, in connect raise e.invalidconnectionerror(comp1, comp2) invalidconnectionerror: <unprintable invalidconnectionerror object>
assertraises
expects perform call. yet, perform yourself, thereby throwing error before assertraises
executes.
self.assertraises(e.invalidconnectionerror, c.connect(comp, comp)) # run ^ first static argument ^ , second argument ^ `c.connect(comp, comp)`
use either of instead:
self.assertraises(e.invalidconnectionerror, c.connect, comp, comp) self.assertraises(e.invalidconnectionerror): c.connect(comp, comp)
Comments
Post a Comment