2010/10/7 Tomas Zulberti <tzulbe...@gmail.com>: > 2010/10/7 Manuel Jesús Recena Soto <rec...@gmail.com>: >> Hola, buenas tardes: >> >> ¿Alguien con experiencia en unittest? >> Estoy codificando un test y me gustaría darlo como bueno si se lanza >> una cierta excepción. Para eso tengo que usar assertRaises() pero no >> sé porqué, no funciona. Alguna referencia? >> > > Se usa asi: > self.assertRaises(Exception, metodo_sin_parantesis, valor1, valor2, valor3) > > donde > def metodo_sin_parantesis(param1, param2, param3): > pass > > es decir, en el assertRaises va la funcion, pero sin los parentesis y > los valores. El assertRaises lo va a ejecutar. >
Estilo tradicional (extraido de la doc stdlib) {{{ #!python class TestSequenceFunctions(unittest.TestCase): def setUp(self): self.seq = range(10) def test_shuffle(self): # make sure the shuffled sequence does not lose any elements random.shuffle(self.seq) self.seq.sort() self.assertEqual(self.seq, range(10)) def test_choice(self): element = random.choice(self.seq) self.assertTrue(element in self.seq) def test_sample(self): self.assertRaises(ValueError, random.sample, self.seq, 20) for element in random.sample(self.seq, 5): self.assertTrue(element in self.seq) }}} En versiones recientes también es possible utilizar esta función como context manager (extraido del blog de Michael Foord [2]_ ) e.g. {{{ #!python as context manager with self.assertRaises(TypeError): add(2, '3') # test message with a regex msg_re = "^You shouldn't Foo a Bar$" with self.assertRaisesRegexp(FooBarError, msg_re): foo_the_bar() # access the exception object with self.assertRaises(TypeError) as cm: do_something() exception = cm.exception self.assertEqual(exception.error_code, 3) }}} También es posible utilizar una versión modificada (TracRpcTestCase.failUnlessRaises) [3]_ que permite seguir el estilo tradicional, y al mismo realizar verificaciones sobre el objeto (i.e. excepción) que se ha lanzado. PD: El chivo que ven ahí [2]_ tiene poderes psíquicos sobre todos aquellos que escriban pruebas en Python [4]_ [5]_ [6]_ [7]_ . Tenganlo muy en cuenta, y sacrifiquen unos cuantos pollos antes de escribir sus pruebas, sino 80% asegurado a que no serán satisfactorias ... :D Sigan los hilos más abajo y se reirán un poco ... ;o) .. [1] No hay 1 (por qué siempre tiene que haber 1 ? ;o) .. [2] Coming changes to unittest in Python 2.7 & 3.2 (http://www.voidspace.org.uk/python/articles/unittest2.shtml#assertraises) .. [3] TracRpcTestCase.failUnlessRaises (http://bitbucket.org/osimons/trac-rpc/src/fa00f1619d65/trunk/tracrpc/tests/__init__.py) .. [4] [TIP] [OT] Goats of PyCon (http://lists.idyll.org/pipermail/testing-in-python/2010-February/002691.html) .. [5] [TIP] [OT] The Testing Goat's World Tour (http://lists.idyll.org/pipermail/testing-in-python/2010-March/002897.html) .. [6] Goats of PyCon - The Real Story (http://www.swordstyle.com/blog2/?p=1822) .. [7] TIP] [OT] Goats of PyCon - Doug Hellmann' comment -- :D (http://lists.idyll.org/pipermail/testing-in-python/2010-February/002695.html) -- Regards, Olemis. Blog ES: http://simelo-es.blogspot.com/ Blog EN: http://simelo-en.blogspot.com/ Featured article: _______________________________________________ Python-es mailing list Python-es@python.org http://mail.python.org/mailman/listinfo/python-es FAQ: http://python-es-faq.wikidot.com/