Python ã® unittest ã§ã¯ assertRaises ã Exception ãè¿ãã¦ãããªã
ã¡ãªã¿ã« Python ä»å±ã® unittest ã§ã¯ assertRaises ã®å®ç¾©ã¯ä»¥ä¸ã®éããè¦ãã°ããããã©ãexception ãè¿ãã¦ãããªãã
## assertRails 㯠failUnlessRaises ã® alias def failUnlessRaises(self, excClass, callableObj, *args, **kwargs): try: callableObj(*args, **kwargs) except excClass: ## ããã except excClass, ex: return ex return ## ã«ãã¦ã»ããã£ã else: if hasattr(excClass,'__name__'): excName = excClass.__name__ else: excName = str(excClass) raise self.failureException, "%s not raised" % excName
ãã®ãããçºçãã exception ãåãåºãã®ã«ã¯ããããªãµãã«æ¸ããªãããããªããããããã©ãããã
def test1(self): L = [] def dummy(): try: open("not_exist_file") except Exception, ex: L[0] = ex raise self.assertRaises(IOError, dummy) ex = L[0] msg = "[Errno 2] No such file or directory: not_exist_file" self.assertEquals(msg, str(ex))
Ruby ã® Test::Unit ã¿ãããassertRaises() ã exception ãè¿ãã¦ãããã°ãããªã«ç°¡åã«æ¸ããã®ã«ã
def test1(self): def dummy(): open("not_exist_file") ex = self.assertRaises(IOError, dummy) msg = "[Errno 2] No such file or directory: not_exist_file" self.assertEquals(msg, str(ex))
ã¤ãã§ã«ããã¨ãassertRaises() ã¯ããã®ã« assertNotRaise() ã¯è¦ã¤ãããªããä»æ¹ãªãã®ã§èªåã§å®ç¾©ãããããªããããããã©ãããã
def assertNotRaise(self, callable_, *args, **kwargs): try: callable_(*args, **kwargs) except Exception, ex: self.fail("unexpected exception raised: " + repr(ex)) import unittest unittest.assertNotRaise = assertNotRaise