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

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -