/ Published in: Python

method 'E' (in fact a decorator) catch the execption and return it, so it's possible to use the good old assert statement.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
import sys def E(m): def _c(*a,**k): try: return m(*a,**k) except: return sys.exc_info()[0] return _c def methodToTest(x): return 2/x if __name__ == "__main__": assert methodToTest(2)==1 assert methodToTest(1)==2 assert E(methodToTest)(0)==ZeroDivisionError # alternative way : methodToTest = E(methodToTest) # force decoration assert methodToTest(1)==2 assert methodToTest(2)==1 assert methodToTest(0)==ZeroDivisionError
Comments
